简体   繁体   English

不包含DataBind的定义

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

I tried this code but this displays an error 我尝试了这段代码,但显示错误

Does Not Contain Definition for DataBind 不包含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];

Now i try this Code and its works really fine for me 现在我尝试使用此代码,它对我来说真的很好

Problem is that the datareader must iterate over the an object try this instead create a custome class to hold your data.like below. 问题在于,数据读取器必须遍历一个对象,尝试使用此方法创建一个custome类来保存您的数据,如下所示。 If that is too much trouble use an data adaptor and use DataSets 如果那太麻烦了,请使用数据适配器并使用数据集

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM