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();
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();
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With