无法将byte []保存到gridControl DevExpress

时间:2014-05-22 06:35:06

标签: c# devexpress xtragrid gridcontrol

我有来自文件的byte []流我想将此数组插入gridControl列

  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 მეგაბაიტს");
                }

            }

我收到错误&#34; objec必须实现iconvertible&#34;我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果它是绑定列,那么您可以将byte []插入到基础数据源中。在这个例子中,我使用RowItem类来表示网格中的一行,然后在选择文件后将byte []放入选定的RowItem中,网格将自动显示图像。要尝试它只需打开一个新项目,在表单上放置一个按钮和一个Xtragrid控件,并使用下面的代码或下载working project

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; }
}