Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have assigned a DataTable to DataGridView.DataSource. When I try to customize DGV columns with new header text and width, an Index out of range exception is thrown for the column index provided. I debug to check if DataTable contains no columns. But, the table was populated with desired 4 columns. Then, I went on checking for column count of DGV after datasource was assigned. It was 0.

A day before everything was working fine. Today, same code is not working.
Here is the code snippet:

C#
private void FormPlay_Load(object sender, EventArgs e)
        {
            DGVCreate();
            FillDGV();
            this.Controls.Add(_dgv);
        }



C#
void DGVCreate()
        {
            _dgv = new DataGridView();
            _dgv.Location = new Point(5, 20);
            _dgv.AutoSize = true;
            _dgv.ForeColor = Color.Black;            
            _dgv.CellContentClick += _dgv_CellContentClick;
        }


C#
void FillDGV()
       {

           _dataSet = ClassDBOperations.DatabaseAdapter("select CCode,CName,Gender,Address from ContestantProfile", "ContestantProfile");
           try
           {
               _tbl = _dataSet.Tables[0];
               if (_tbl.Rows.Count != 0)
               {
                   _dgv.DataSource = _tbl;
                   _dgv.Columns[0].HeaderText = "Code";
                   _dgv.Columns[1].HeaderText = "Name";
                   _dgv.Columns[2].HeaderText = "Gender";
                   _dgv.Columns[3].HeaderText = "Address";

                   _dgv.Columns[0].Width = 100;
                   _dgv.Columns[1].Width = 200;
                   _dgv.Columns[2].Width = 50;
                   _dgv.Columns[3].Width = 400;
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
Posted

1 solution

add the columns first and finally set the data source
C#
dataSet = ClassDBOperations.DatabaseAdapter("select CCode,CName,Gender,Address from ContestantProfile", "ContestantProfile");
dgv.AutoGenerateColumns = false;
_dgv.ColumnCount = 4;
_dgv.Columns[0].Width  = 100;
_dgv.Columns[0].HeaderText = "Code";
_dgv.Columns[0].DataPropertyName = "CCode";

_dgv.Columns[1].Width = 200;
_dgv.Columns[1].HeaderText = "Name";
_dgv.Columns[1].DataPropertyName = "CName";

_dgv.Columns[2].Width = 50;
_dgv.Columns[2].HeaderText = "Gender";
_dgv.Columns[2].DataPropertyName = "Gender";

_dgv.Columns[3].Width = 400;
_dgv.Columns[3].HeaderText = "Address ";
_dgv.Columns[3].DataPropertyName = "Address ";

_dgv.DataSource = _dataSet.Tables[0];
 
Share this answer
 
v2
Comments
Member 11040029 27-Dec-14 5:18am    
Thnx for the solution but I would like to know that why do I need to explicitly add columns to DGV when AutoGenerateColumns property is set to true. DGV must receive column count and generate the same number of columns without specifying columns explicitly.
DamithSL 27-Dec-14 5:23am    
in case of dataSet is null, after you bind it to datagrid there will not be any columns. you can't access columns by index
Member 11040029 27-Dec-14 5:25am    
During debugging my dataset is not null. So, again why do I need to do it?
DamithSL 27-Dec-14 6:18am    
add datagrid to form first and then load the data
this.Controls.Add(_dgv);
FillDGV();
Member 11040029 27-Dec-14 6:25am    
This works!! Thnx DamithSL, but can you let me know the reason behind it? Why do I need to add DGV first?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900