A DataTable-bound DataGridView throws a low-level Exception whencolumns are sorted, removed and re-a

S

Sath123

Hi,

I am experiencing this issue when I am working on creating a dynamic
datagrid. Please let me know if there is any work around for this
problem. If I am posting this in the wrong newsgroup please point me
to the right newsgroup.

Issue: A DataTable-bound DataGridView throws a low-level Exception
when columns are sorted, removed and re-added to the underlying
DataTable.

Steps to replicate the issue:
1. Create a DataTable.
2. Create a DataGridView and bind it to the DataTable.
3. Populate the datatable with data
4. SORT a column of the DataView (Exception is not thrown without this
step).
5. Remove the column of DataTable that was sorted.
6. Add a new column to the DataTable.
7. Try to add data to that column - Exception is thrown.

Sample Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class UserControl1 : UserControl
{
DataGridViewTextBoxColumn col1;
DataGridViewTextBoxColumn col2;
DataGridViewTextBoxColumn col3;
DataColumn datacol1;
DataColumn datacol2;
DataColumn datacol3;


public UserControl1()
{
InitializeComponent();

col1 = new DataGridViewTextBoxColumn();
col1.Name = "col1";
col1.HeaderText = "col1";
col1.DataPropertyName = "col1";
col1.SortMode = DataGridViewColumnSortMode.Automatic;

col2 = new DataGridViewTextBoxColumn();
col2.Name = "col2";
col2.HeaderText = "col2";
col2.DataPropertyName = "col2";

col3 = new DataGridViewTextBoxColumn();
col3.Name = "col2";
col3.HeaderText = "col2";
col3.DataPropertyName = "col2";

datacol1 = new DataColumn("col1",
System.Type.GetType("System.String"));
datacol2 = new DataColumn("col2",
System.Type.GetType("System.String"));
datacol3 = new DataColumn("col3",
System.Type.GetType("System.String"));
}

private void button2_Click(object sender, EventArgs e)
{
// add col1 and col2 columns to datagridview
dataGridView1.Columns.Add(col1);
dataGridView1.Columns.Add(col2);

// add columns to the datatable
dataTable1.Columns.Add(datacol1);
dataTable1.Columns.Add(datacol2);

// add rows
dataTable1.Rows.Add(new object[] { "a", "b" });
dataTable1.AcceptChanges();

// sort
dataGridView1.Sort(col1, ListSortDirection.Ascending);

// remove sorted column col1
dataGridView1.Columns.Remove("col1");
dataTable1.Columns.Remove("col1");

//add new column col3
dataGridView1.Columns.Add(col3);
dataTable1.Columns.Add(datacol3);

// try to add rows - exception is thrown here
dataTable1.Rows.Add(new object[] { "z", "p" });
dataTable1.AcceptChanges();

}
}
}
 
N

Nathan

Sath123 said:
Hi,

I am experiencing this issue when I am working on creating a dynamic
datagrid. Please let me know if there is any work around for this
problem. If I am posting this in the wrong newsgroup please point me
to the right newsgroup.

Issue: A DataTable-bound DataGridView throws a low-level Exception
when columns are sorted, removed and re-added to the underlying
DataTable.

Steps to replicate the issue:
1. Create a DataTable.
2. Create a DataGridView and bind it to the DataTable.
3. Populate the datatable with data
4. SORT a column of the DataView (Exception is not thrown without this
step).
5. Remove the column of DataTable that was sorted.
6. Add a new column to the DataTable.
7. Try to add data to that column - Exception is thrown.

Sample Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class UserControl1 : UserControl
{
DataGridViewTextBoxColumn col1;
DataGridViewTextBoxColumn col2;
DataGridViewTextBoxColumn col3;
DataColumn datacol1;
DataColumn datacol2;
DataColumn datacol3;


public UserControl1()
{
InitializeComponent();

col1 = new DataGridViewTextBoxColumn();
col1.Name = "col1";
col1.HeaderText = "col1";
col1.DataPropertyName = "col1";
col1.SortMode = DataGridViewColumnSortMode.Automatic;

col2 = new DataGridViewTextBoxColumn();
col2.Name = "col2";
col2.HeaderText = "col2";
col2.DataPropertyName = "col2";

col3 = new DataGridViewTextBoxColumn();
col3.Name = "col2";
col3.HeaderText = "col2";
col3.DataPropertyName = "col2";

datacol1 = new DataColumn("col1",
System.Type.GetType("System.String"));
datacol2 = new DataColumn("col2",
System.Type.GetType("System.String"));
datacol3 = new DataColumn("col3",
System.Type.GetType("System.String"));
}

private void button2_Click(object sender, EventArgs e)
{
// add col1 and col2 columns to datagridview
dataGridView1.Columns.Add(col1);
dataGridView1.Columns.Add(col2);

// add columns to the datatable
dataTable1.Columns.Add(datacol1);
dataTable1.Columns.Add(datacol2);

// add rows
dataTable1.Rows.Add(new object[] { "a", "b" });
dataTable1.AcceptChanges();

// sort
dataGridView1.Sort(col1, ListSortDirection.Ascending);

// remove sorted column col1
dataGridView1.Columns.Remove("col1");
dataTable1.Columns.Remove("col1");

//add new column col3
dataGridView1.Columns.Add(col3);
dataTable1.Columns.Add(datacol3);

// try to add rows - exception is thrown here
dataTable1.Rows.Add(new object[] { "z", "p" });
dataTable1.AcceptChanges();

}
}
}

Without being able to explain the intricacies of how the Sort() method
works, it looks like the error is basically due to the fact that the grid's
sort column is set to a column that no longer exists as part of the grid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top