Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have a gridview in my MDI Application which displays the list of customers and the columns are defined at the design time.

My code for binding the grid in Form_Load() is:

C#
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();


The data is assigned to the each cell of grid but not displayed in the form when it is loaded.

I tried the same code in the Single form application and it works perfectly.

what should I do ? Please help. Thanks in advance.
Posted
Comments
[no name] 17-Apr-13 8:28am    
Check the answer in C Sharp Corner.
Sandeep Mewara 17-Apr-13 13:19pm    
???

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