DataGridView throwing Exception in Default Error Dialog due to DataGridViewImageColumn

前端 未结 3 2209
孤独总比滥情好
孤独总比滥情好 2021-02-13 18:09

I am putting this up because it took far too long to find the answer on the web and this is probably a common problem - it is the second time i have experienced it on my app.

相关标签:
3条回答
  • 2021-02-13 18:51

    This problems is caused by some image and varbinary column types, my solution was creating other data datatable and setting all columns and cell values to string type:

                var clonedDT = dt.Clone();
    
                for (int i = 0; i < clonedDT.Columns.Count; i++)
                {
                    clonedDT.Columns[i].DataType = typeof(String);
                }
    
    
                for (int j = 0; j < dt.Rows.Count;j++ )
                {
                    var newRow = clonedDT.NewRow();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {                        
                        newRow.SetField(i, Convert.ToString(item.Value.Rows[j]  [i]));
                    }
    
                    clonedDT.Rows.Add(newRow); 
                }
    
    0 讨论(0)
  • 2021-02-13 19:07

    I was getting this issue because some of my images we JPEGS and some were PNGs.

    All the PNGs were causing an error to be thrown.

    I removed all the PNGs and I get no errors when I scroll down the grid.

    0 讨论(0)
  • 2021-02-13 19:09

    My solution: remove the column right after it is added (detailed reason at the end). The following code removes all potential image columns, you may want to customize this if your schema is not dynamic and you know what you want to leave out:

    public Form1()
    {
        InitializeComponent();
        dataGridView1.ColumnAdded += dataGrid_ColumnAdded;
    }
    
    void dataGrid_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
    {
        if (e.Column.CellType == typeof(DataGridViewImageCell))
            dataGridView1.Columns.Remove(e.Column);
    }
    

    so when it comes to the actual binding

    DataTable table = dataTableCombo.SelectedItem as DataTable;
    dataGridView1.DataSource = table;
    

    populating the cells will happen after adding (and the removing correction) of the columns. And the exception doesn't happen this way.

    Also, in your dataGridView1_RowsAdded event handler watch out: there's not only e.RowIndex but there's e.RowCount too, and it can be e.RowCount > 1! So first I tried:

    void dataGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
        for (int i = e.RowIndex; i < e.RowIndex + e.RowCount; i++)
        {
            foreach (DataGridViewCell cell in dataGridView1.Rows[i].Cells)
            {
                if (cell.GetType() == typeof(DataGridViewImageCell))
                {
                    cell.Value = DBNull.Value;
                }
            }
        }
    }
    

    but I still got some exceptions. Plus if your binding is two-way, watch out because cell.Value = DBNull.Value; causes a change in the business object! That's the reason I advise to just rather remove the column.

    0 讨论(0)
提交回复
热议问题