10
Answers

views Code for binding gridview works in Single Form app.

Riyaz Kalva

Riyaz Kalva

11y
1.4k
1
One of the child forms in my application is having the grid in which data is bounded in the program.
My code is :


private void SearchForm_Load(object sender, EventArgs e) { dataGridView1.AutoGenerateColumns = false; 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(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } 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(); } ds.Dispose(); cmd.Dispose(); da.Dispose(); cn.Close(); }

I debugged the program and checked that each cell is assigned a value from "Immediate window" but records are not displayed in the grid when the from is loaded.

I ran the same code in the Single Form application and it works successfully.
What kind of changes I should make to apply the same code to the MDI Forms application.
Please help.
Answers (10)