Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / May 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sath123 - 12 May 2008 03:00 GMT
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();

       }
   }
}
Nathan - 12 May 2008 12:51 GMT
> Hi,
>
[quoted text clipped - 98 lines]
>    }
> }

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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.