在Windows窗体应用程序中,我有两个文本框,一个按钮和一个数据网格视图。
我正在按下按钮单击以根据文本框中给出的数字在dataGridView中显示数据
我没有收到任何错误,我在数据网格视图中看不到任何数据。
这是我的代码:
private void Button1_Click(object sender, EventArgs e)
{
SqlDataAdapter da_for_Branch;
DataSet ds_forBranch = new DataSet();
SqlConnection con;
con = new SqlConnection("Data Source=Test ;Initial Catalog=combit_cRM_Solution3;Integrated Security=True");
da_for_Branche = new SqlDataAdapter("SELECT Distinct(BusinessSectorID),Description_DE " +
" FROM BusinessSector5 " +
" WHERE BusinessSectorID>= '"+ TxtBoxFrom.Text + "' " +
" AND BusinessSectorID<='" + TxtBoxTo.Text + "' " +
" ORDER BY BusinessSectorID ", con);
da_for_Branche.Fill(ds_forBranches);
dataGridView1.DataSource = ds_forBranches;
}
答案 0 :(得分:1)
您无法直接将数据集绑定为网格的数据源。您只能绑定数据集中的表。在你的原因中,你应该绑定索引0中的表。
//Wrong
dataGridView1.DataSource = ds_forBranches;
//Right
dataGridView1.DataSource = ds_forBranches.Tables[0];