ytba92 ytba92 - 2 months ago 25
ASP.NET (C#) Question

How to set image source of BinaryImageColumnin RadGrid from byte[] array in C#?

I have the following ASP.NET:



<Columns>
<telerik:GridClientSelectColumn UniqueName="GridClientSelectColumn1" />
<telerik:GridBinaryImageColumn UniqueName="BinaryImageColumn" />
<telerik:GridBoundColumn DataField="PRODUCT_NAME" SortExpression="PRODUCT_NAME" HeaderText="<%$ Resources:Strings, ProductName %>" HeaderButtonType="TextButton" />
<telerik:GridBoundColumn DataField="STORE_NAME" SortExpression="STORE_NAME" HeaderText="<%$ Resources:Strings, Supplier %>" HeaderButtonType="TextButton" />
<telerik:GridBoundColumn DataField="PRODUCT_PRICE" SortExpression="PRODUCT_PRICE" HeaderText="<%$ Resources:Strings, UnitPrice %>" HeaderButtonType="TextButton" />
<telerik:GridBoundColumn DataField="PRODUCT_QUANTITY" SortExpression="PRODUCT_QUANTITY" HeaderText="<%$ Resources:Strings, Quantity %>" HeaderButtonType="TextButton" />
<telerik:GridBoundColumn DataField="TRANS_TOTAL" SortExpression="TRANS_TOTAL" HeaderText="<%$ Resources:Strings, TotalPrice %>" HeaderButtonType="TextButton" />
</Columns>





Code Behind:



protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) {
SQLHelper a = new SQLHelper(SQLHelper.ConnectionStrings.KernelConnectionString);
DataTable newdt = DataHelper.GetProductAdditionalObjects(11);
for (int i = 0; i < newdt.Rows.Count; i++) {
if (int.Parse(newdt.Rows[i]["PROD_OBJECT_SPECS_ID"].ToString()) == 1) {
byte[] img = Converter.ObjectToByteArray(newdt.Rows[i]["OBJECT_BINARIES"]); //Convert Object to Byte Array
}
}
GridDataItem item = e.Item as GridDataItem;
if (item != null) {
(item["BinaryImageColumn"].Controls[0] as RadBinaryImage).DataValue = img;
}
}





The problem is that it gives me an error:
The name 'img' does not exist in the current context, although it is the byte array that I want to use in this case

Answer

Image byte array could be handled in RadGrid's ItemDataBound event.

<Columns>
    <telerik:GridBinaryImageColumn UniqueName="BinaryImageColumn" />
    <telerik:GridBoundColumn DataField="PRODUCT_NAME" SortExpression="PRODUCT_NAME" HeaderText="<%$ Resources:Strings, ProductName %>" HeaderButtonType="TextButton" />
</Columns>

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
   GridDataItem item = e.Item as GridDataItem;
   if(item != null)
   {
      (item["BinaryImageColumn"].Controls[0] as RadBinaryImage).DataValue = yourImgByteArray;
   }
}

Each cell in a GridBinaryImageColumn contains an image streamed from a binary image source field (specified through the DataField property of the column). When used, this column will show a RadBinaryImage control in view mode and RadUpload or RadAsyncUpload in edit mode to upload an image. The type of upload is determined by the UploadControlType property. Additionally, you can persist the binary data when an item is opened for edit by setting the PersistBinaryDataOnEdit property to true. This will force the control to pass the old binary image to the data source so it could be persisted and not deleted.

More details can be found in the following link.

http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/columns/column-types#gridbinaryimagecolumn