如何从C#中的byte[]数组设置RadGrid中BinaryImageColumn的图像源
本文关键字:RadGrid 设置 BinaryImageColumn 图像 数组 中的 byte | 更新日期: 2023-09-27 17:57:36
我有以下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>
代码背后:
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;
}
}
问题是它给了我一个错误:名称"img"在当前上下文中不存在,尽管它是我想在本例中使用的字节数组
图像字节数组可以在RadGrid的ItemDataBound
事件中处理。
<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;
}
}
GridBinaryImageColumn中的每个单元格都包含一个从二进制图像源字段(通过的DataField属性指定列)。使用时,此列将显示RadBinaryImage控件在视图模式下,RadUpload或RadAsyncUpload在编辑模式下上载形象上传类型由UploadControlType决定所有物此外,当项目通过将PersistBinaryDataOnEdit属性设置为真。这将强制控件将旧的二进制图像传递给数据源,以便它可以持久化而不被删除。
更多详细信息可在以下链接中找到。
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/columns/column-types#gridbinaryimagecolumn
我不知道我的答案是否对你有帮助。第一个RadGridview列添加(image或txt.etc)。
RadGridView1.Rows.AddNew();
RadGridView1.Rows[RadGridView1.Rows.Count - 1].Cells["Data1"].Value = byteArrayToImage(your Byte Array)
//My First Column(Data1) is ImageColumn.
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}