0

I have byte[] stream from file I want to insert this array to the gridControl column

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

            }

and I am getting error "objec must implement iconvertible" how can i solve this issue?

scheien
  • 2,457
  • 1
  • 19
  • 22
user3150998
  • 37
  • 1
  • 8
  • Maybe the column has a different type than one that can represent `byte[]`. Is the column bound or unbound? If unbound, what is the unbound type? – scheien May 22 '14 at 06:52
  • it is bound type I think the problem is that it can't convert from byte[] to object – user3150998 May 22 '14 at 06:56
  • What you basically want to do is to load an image into a cell in a grid? What I have done earlier is to have a byte[] property in the type that I set as the datasource. Then you can use the built-in context menu to load an image into that given cell. – scheien May 22 '14 at 07:23

1 Answers1

0

If it's a bound column then you can insert byte[] into the underlying datasource. In this example I've used a RowItem class to represent a row in the grid, and then after selecting a file put the byte[] into the selected RowItem and the grid will automagically show the image. To try it just open a new project put one button and one Xtragrid control on the form and use the code below or download a 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; }
}
OttO
  • 419
  • 4
  • 9