CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2002
    Posts
    206

    Help. Getting exception when I rebind grid to another dataset and change column names

    Hi,

    I am using a datagrid and two dataset with different data. Both views of the grid contains 5 columns. I toggle between two different views of the grid which display different types of data. When I toggle, I change the last 3 columns to different name and bind a different dataset. When I startup the application, the first dataset when I click on any empty cells, it works find. However, when I switch for the first time to the alternative dataset and change the name, it will throw an exception


    Index was out of range. Must be non-negative and less than th esize of the collection.
    Parameter name: index


    When I keep the column names the same between the two grid views, it works fine. However, I need to change the column names to indentify the column type of data.

    Any help is appreciated.

  2. #2
    Join Date
    Apr 2005
    Posts
    576

    Re: Help. Getting exception when I rebind grid to another dataset and change column names

    I tried this code and it worked fine:

    Code:
    private void button1_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("T1", typeof(int)));
        dt.Columns.Add(new DataColumn("T2", typeof(string)));
        dt.Columns.Add(new DataColumn("T3", typeof(double)));
        dataGridView1.DataSource = dt;
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("X1", typeof(string)));
        dt.Columns.Add(new DataColumn("X2", typeof(string)));
        dt.Columns.Add(new DataColumn("X3", typeof(int)));
        dataGridView1.DataSource = dt;
    }
    If you are changing the data grid columns, I would try with first setting the data source to null, then change columns, then set the new data source.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured