C# can';t将字节[]保存到gridControl DevExpress
我有来自文件的byte[]流,我想将此数组插入gridControl列C# can';t将字节[]保存到gridControl DevExpress,c#,devexpress,xtragrid,gridcontrol,C#,Devexpress,Xtragrid,Gridcontrol,我有来自文件的byte[]流,我想将此数组插入gridControl列 if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (new FileInfo(openFileDialog1.FileName).Length < 10485760) { byte[]
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (new FileInfo(openFileDialog1.FileName).Length < 10485760)
{
byte[] st = Converter.streamToArray(openFileDialog1.OpenFile());
GridManipulator.GridView.SetRowCellValue(GridManipulator.GridView.FocusedRowHandle,GridManipulator.FILESTREAM,
st);
GridManipulator.GridView.SetRowCellValue(GridManipulator.GridView.FocusedRowHandle,GridManipulator.FILENAME,
Path.GetFileName(openFileDialog1.FileName));
}
else
{
XtraMessageBox.Show("ფაილი აჭარბებს 10 მეგაბაიტს");
}
}
if(openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
if(新文件信息(openFileDialog1.FileName).Length<10485760)
{
字节[]st=Converter.streamToArray(openFileDialog1.OpenFile());
GridManipular.GridView.SetRowCellValue(GridManipular.GridView.FocusedRowHandle、GridManipular.FILESTREAM、,
st);
GridManipular.GridView.SetRowCellValue(GridManipular.GridView.FocusedRowHandle,GridManipular.FILENAME,
GetFileName(openFileDialog1.FileName));
}
其他的
{
XtraMessageBox.Show(“ფაილი აჭარბებს 10მეგაბაიტს");
}
}
我得到了一个错误“objec必须实现iconvertible”我如何解决这个问题?如果它是绑定列,那么您可以将byte[]插入基础数据源。在这个示例中,我使用了一个RowItem类来表示网格中的一行,然后在选择一个文件后放入byte[]在选定的行项目中,网格将自动显示图像。要尝试此操作,只需打开一个新项目,在表单上放置一个按钮和一个Xtragrid控件,然后使用下面的代码或下载
public分部类MainForm:Form
{
//这将保存网格的数据
列表项=新列表();
公共表格(
{
初始化组件();
gridControl1.DataSource=项目;
Add(newrowItem(){ID=1,Caption=“First”});
Add(newrowItem(){ID=2,Caption=“Second”});
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
使用(OpenFileDialog ofd=new OpenFileDialog())
{
if(ofd.ShowDialog()==DialogResult.OK)
{
byte[]filecontents=File.ReadAllBytes(ofd.FileName);
//获取由选定行表示的项对象
RowItem selecteditem=gridView1.GetFocusedRow()作为RowItem;
如果(selecteditem==null)返回;
选择editem.Bytes=filecontents;
选择editem.FileName=ofd.FileName;
gridView1.RefreshData();
}
}
}
}
类行项目
{
公共int ID{get;set;}
公共字符串标题{get;set;}
公共字节[]字节{get;set;}
公共字符串文件名{get;set;}
}
可能该列的类型与表示字节[]的列的类型不同。该列是绑定的还是未绑定的?如果未绑定,什么是未绑定的类型?它是绑定类型?我认为问题在于它无法从字节[]转换object您基本上想做的是将图像加载到网格中的单元格中?我前面做的是在我设置为数据源的类型中有一个byte[]属性。然后您可以使用内置上下文菜单将图像加载到给定的单元格中。
public partial class MainForm : Form
{
// this will hold the data for the grid
List<RowItem> Items = new List<RowItem>();
public MainForm()
{
InitializeComponent();
gridControl1.DataSource = Items;
Items.Add(new RowItem() { ID = 1, Caption = "First" });
Items.Add(new RowItem() { ID = 2, Caption = "Second" });
}
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() == DialogResult.OK)
{
byte[] filecontents = File.ReadAllBytes(ofd.FileName);
// Get the Item object represented by the selected row
RowItem selecteditem = gridView1.GetFocusedRow() as RowItem;
if (selecteditem == null) return;
selecteditem.Bytes = filecontents;
selecteditem.FileName = ofd.FileName;
gridView1.RefreshData();
}
}
}
}
class RowItem
{
public int ID { get; set; }
public string Caption { get; set; }
public byte[] Bytes { get; set; }
public string FileName { get; set; }
}