【问题标题】:can't save byte[] to gridControl DevExpress无法将 byte[] 保存到 gridControl DevExpress
【发布时间】:2014-05-22 06:35:06
【问题描述】:

我有来自文件的 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 მეგაბაიტს");
                }

            }

我收到错误“对象必须实现 iconvertible”我该如何解决这个问题?

【问题讨论】:

  • 也许该列的类型不同于可以表示byte[] 的类型。该列是绑定的还是未绑定的?如果未绑定,未绑定类型是什么?
  • 它是绑定类型我认为问题是它不能从字节[]转换为对象
  • 您基本上想要做的是将图像加载到网格中的单元格中?我之前所做的是在我设置为数据源的类型中有一个 byte[] 属性。然后,您可以使用内置的上下文菜单将图像加载到给定的单元格中。

标签: c# devexpress xtragrid gridcontrol


【解决方案1】:

如果它是一个绑定列,那么您可以将 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; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多