Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

column count property cannot be set on a databound datagridview control c#

In My form I have a DataGridView. and controls for input and output data. while displaying data in the gird view. I am calling BindGrid in two occasions. One while formload and other is After adding a new record.

 public formAccounts()
    {
        InitializeComponent();
        BindGrid();           
    }

 private void BindGrid()
    {
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= G:\Sanjeev\TESTDB\DB.mdb;Jet OLEDB:Database Password=Test123; Jet OLEDB:Engine Type=5";
        conn.Open();
        string constring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\Sanjeev\TESTDB\DB.mdb;Jet OLEDB:Database Password=Test123; Jet OLEDB:Engine Type=5";

        using (OleDbConnection con = new OleDbConnection(constring))
        {
            using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM tblAccounts", con))
            {
                cmd.CommandType = CommandType.Text;
                using (OleDbDataAdapter sda = new OleDbDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        //Set AutoGenerateColumns False
                        dtGrdAccounts.AutoGenerateColumns = false;
                        //Set Columns Count
                        ***dtGrdAccounts.ColumnCount = 3;***
                        //Add Columns
                        dtGrdAccounts.Columns[0].Name = "AccountID";
                        dtGrdAccounts.Columns[0].HeaderText = "Id";
                        dtGrdAccounts.Columns[0].DataPropertyName = "AccID";

                        dtGrdAccounts.Columns[1].HeaderText = "Account name";
                        dtGrdAccounts.Columns[1].Name = "Account name";
                        dtGrdAccounts.Columns[1].DataPropertyName = "AccName";

                        dtGrdAccounts.Columns[2].Name = "AccountNumber";
                        dtGrdAccounts.Columns[2].HeaderText = "Account number";
                        dtGrdAccounts.Columns[2].DataPropertyName = "AccNumber";
                        dtGrdAccounts.DataSource = dt;
                    }
                }
            }
        }
    }

If I remove the line dtGrdAccounts.ColumnCount = 3; I am getting below error. Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

After adding the a new record , On button save I am calling BindGrid() method to get the record in the GridiView.

string cmdText = "prAddAccounts";
OleDbCommand cmd = new OleDbCommand(cmdText, conn);            
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("AccName", txtAccName.Text.ToString());
 //..... other parameters  to insert record .....
OleDbCommand cmd = new OleDbCommand(cmdText, conn);            
cmd.CommandType = CommandType.StoredProcedure;
OleDbDataReader reader = cmd.ExecuteReader();
BindGrid();
like image 467
Sanjeev4evr Avatar asked Oct 12 '25 06:10

Sanjeev4evr


2 Answers

The issue is when you call the BindGrid() again you have to clear it first:

string cmdText = "prAddAccounts";
OleDbCommand cmd = new OleDbCommand(cmdText, conn);            
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("AccName", txtAccName.Text.ToString());
 //..... other parameters  to insert record .....

//Clear the binding.
dtGrdAccounts.DataSource = null;
//Bind the new data.
BindGrid();
like image 76
Racil Hilan Avatar answered Oct 14 '25 22:10

Racil Hilan


if you are calling datagridview colum design code more than once then before setting column property you have to make Gridview data source to null as shown below

dataGridView1.DataSource = null;

using (DataTable dt = new DataTable())
                {
                    //add this line of code
                    dtGrdAccounts.DataSource = null;

                    sda.Fill(dt);
                    //Set AutoGenerateColumns False
                    dtGrdAccounts.AutoGenerateColumns = false;
                    //Set Columns Count
                    ***dtGrdAccounts.ColumnCount = 3;***
                    //Add Columns
                    dtGrdAccounts.Columns[0].Name = "AccountID";
                    dtGrdAccounts.Columns[0].HeaderText = "Id";
                    dtGrdAccounts.Columns[0].DataPropertyName = "AccID";

                    dtGrdAccounts.Columns[1].HeaderText = "Account name";
                    dtGrdAccounts.Columns[1].Name = "Account name";
                    dtGrdAccounts.Columns[1].DataPropertyName = "AccName";

                    dtGrdAccounts.Columns[2].Name = "AccountNumber";
                    dtGrdAccounts.Columns[2].HeaderText = "Account number";
                    dtGrdAccounts.Columns[2].DataPropertyName = "AccNumber";
                    dtGrdAccounts.DataSource = dt;
                }
like image 36
Mahadi Hassan Avatar answered Oct 14 '25 20:10

Mahadi Hassan