2013-04-17 128 views
1

我正在使用网格通过其在设计时定义列的代码进行绑定。为什么我没有在我的网格中显示数据?

我给在Form_Load结合网格()的代码是:

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

我调试的程序和数据被分配到从在立即窗口调试时所观察到的每个字段但当形式显示数据不会出现。并创建从数据集中获取的空白行数。

我该如何解决这个问题?请帮忙。

+0

where dataGridView1.DataBind(); – Sajeetharan

回答

0

试试这个: dataGridView1.DataSource = ds; //数据集 dataGridView1.DataMember =“TableName”; //表名

希望它能起作用。

相关问题