[英]Does Not Contain Definition for DataBind
using (SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-IIDC3HF\SQLEXPRESS;Initial Catalog=EmployeeNotifier;Integrated Security=True"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select Name,Salary FROM YOUR TABLE", con);
SqlDataReader dr = cmd.ExecuteReader();
dataGridView1.DataSource = dr;
dataGridView1.DataBind(); // causing problem here
con.Close();
}
我嘗試了這段代碼,但顯示錯誤
不包含DataBind的定義
var select =q;
var c = new SqlConnection(@"Your Connection String here ");
var dataAdapter = new SqlDataAdapter(select, c);
var commandBuilder = new SqlCommandBuilder(dataAdapter);
var ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = ds.Tables[0];
現在我嘗試使用此代碼,它對我來說真的很好
問題在於,數據讀取器必須遍歷一個對象,嘗試使用此方法創建一個custome類來保存您的數據,如下所示。 如果那太麻煩了,請使用數據適配器並使用數據集
List<myCustomerCLass> list = new List<myCustomerCLass>();
con.Open();
SqlCommand cmd = new SqlCommand("Select Name,Salary FROM YOUR TABLE", con);
SqlDataReader dr = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
myCustomerCLass test = new myCustomerCLass();
test.property1 = reader["Property1"]
test.property2 = reader["Property2"]
test.property3 = reader["Property3"]
list.add(test);
}
}
reader.Close();
dataGridView1.DataSource = list;
dataGridView1.DataBind(); // causing problem here
con.Close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.