I have a grid view which lists all the customers.
I am binding it in the load time of Form which is placed in the child of MDI.
Columns in the grid view is predefined at the design time.
My code for the Form_Load()
event is:
try
{
cn = db.createConnection();
if (cn.State == System.Data.ConnectionState.Open)
{
cn.Close();
}
cn.Open();
cmd = new OleDbCommand("Select BillNo,PartyName,City,State,FORMAT(BillDt,'dd-mm-yyyy') as BillDt from BillMaster", cn);
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
cn.Close();
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = ds.Tables[0];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i]["BillNo"].ToString();
dataGridView1.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i]["PartyName"].ToString();
dataGridView1.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i]["City"].ToString();
dataGridView1.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i]["State"].ToString();
dataGridView1.Rows[i].Cells[4].Value = ds.Tables[0].Rows[i]["BillDt"].ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
cn.Close();
da.Dispose();
ds.Dispose();
cmd.Dispose();
}
}
I checked the program execution by putting the breakpoints. The data is fetched exactly as database in the DataSet and Immediate Window the value of particular cell of grid shows the exact value but the problem is when the form is loaded the grid remains empty. And creates the number of blank rows same as the number of rows fetched from the database.
What should I do to tackle this error.
Please help.