Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
105
Setting cell.value Throws NullReferenceException
posted

I have an UltraGrid that list a bunch of services my company offers. One of the fields is an Icon.

The database will keep a column of Type Image for this Icon. The Datasource of the Grid is a .Net BindingSource The Bindingsource is set to a bindable BusinessObject

The best way that I've found to add an Image to the database is to do it in the application. However when I load up the grid, which is set to a Bindable Business Object The value for this Image Field is Null (Naturally, as I've not been able to add it to the database yet) So I set the InitializeRow Event to detect if the paticular Icon Cell is Null if it is I want to Display a button and if it's not I want to display the Icon.

 So far everything works.

Now when the image is null I press the button in the application I get an OpenFile Dialog box and I select the image that I want to load in the database. I then parse the data as I should and Set the style of the cell to Image, I then set the Value of the Cell to the byte[ array of the Image. when this happens I get a NullReferenceException on cell.value

 I'm confused because of course it's null right now I want to set it and it keeps coming up with the exception. Anybody have any idea's on how to fix it?

private void ugAdminServices_ClickCellButton(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs e)

{

//Convert Sender to UltraGrid Object

UltraGrid gSender = (UltraGrid)sender;

OpenFileDialog ofLoadIcon = new OpenFileDialog();

ofLoadIcon.Filter = "All Pictures|*.bmp;*.gif;*.jpg;*.png|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg|PNGs|*.png";if (ofLoadIcon.ShowDialog() == DialogResult.OK)

{

Bitmap icon = new Bitmap(ofLoadIcon.FileName);

MemoryStream mstr = new MemoryStream();

icon.Save(mstr, icon.RawFormat);

byte[ file = mstr.GetBuffer();e.Cell.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Image;

e.Cell.Value = file;

e.Cell.Appearance.ImageBackground = icon;

e.Cell.Band.Layout.Override.RowSizing =
RowSizing.Free;

e.Cell.Column.MinWidth = icon.Width;

}

}

Parents Reply Children