PC Review


Reply
Thread Tools Rate Thread

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

 
 
Sath123
Guest
Posts: n/a
 
      12th May 2008
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();

}
}
}


 
Reply With Quote
 
 
 
 
Nathan
Guest
Posts: n/a
 
      12th May 2008
"Sath123" <(E-Mail Removed)> wrote in message
news:3369e298-ffcc-469a-9d06-(E-Mail Removed)...
> 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.

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Will a row added to a DataView automatically be added to the underlying DataTable? Ronald S. Cook Microsoft VB .NET 1 7th Jul 2007 06:44 PM
Bound DataGridView & DataTable - Which should be updated? Liddle Feesh Microsoft ADO .NET 0 18th Aug 2006 11:37 PM
Updating DataTable bound to a DataGridView Carlos Cruz Microsoft VB .NET 3 28th Jul 2006 10:27 AM
DataTable.PrimaryKey throws exception on an empty table MuZZy Microsoft C# .NET 0 22nd May 2005 12:28 AM
How to find newly added row in sorted/filtered DataTable? Piotr Olech Microsoft ADO .NET 3 15th Jan 2004 09:11 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:17 PM.