Not able to bind the grid view in code behind desk

2019-07-18 19:07发布

问题:

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.

回答1:

Change

dataGridView1.AutoGenerateColumns = false;

to true. Remove

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();
    }


回答2:

first thing and maybe I am missing something but:

this is redundant, I would remove it, it could be part of the problem

 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();
    }

this should already take care of that

dataGridView1.DataSource = ds.Tables[0];


回答3:

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 = true;
   dataGridView1.DataSource = ds.Tables[0];

   //Or you can use
   //dataGridView1.DataSource = ds.Tables[0].DefaultView;    


   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.Message.ToString());
   }
   finally
   {
   cn.Close();
   da.Dispose();
   ds.Dispose();
   cmd.Dispose();
  }

  }

Hope it helps you