如何绑定DatagridView [英] How to bind DatagridView

查看:65
本文介绍了如何绑定DatagridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是window applicaiton的新手,并使用以下代码进行datagridview绑定:

Hi,

I am new to window applicaiton and using the following code to datagridview binding:

SqlDataAdapter adp = new SqlDataAdapter("Select * from tblcnt", ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds);           
dataGridView1.DataSource = ds;
dataGridView1.AutoGenerateColumns = true;



请更正此问题并提供详细说明.

问候!
Aman



Please correct this and provide the detailed explanation.

Regards!
Aman

推荐答案

希望
Hope Grid View[^]will help you.


那很好,但是您需要在构造函数中提供命令对象的DataAdapter.

看到我所有通过绿色注释对您的程序进行的更正.

That''s good but you need to provide command object in costructor of DataAdapter.

See all my correction your program with Green Comments.

SqlDataAdapter adp = new SqlDataAdapter(new SqlCommand("Select * from tblcnt", ConfigurationManager.ConnectionStrings["cn"].ConnectionString));
//See I've changed here SqlDataAdapter constuctor.
DataSet ds = new DataSet();
adp.Fill(ds);           
//Keep this things before for assigning value to any grid properties
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds;
//you've just forgot to bind datagridview
dataGridView1.DataBind();


您没有将对象引用设置为对象的实例,因为您只是将数据集对象分配给了不足以绑定数据的数据源.
您可以使用给定的代码将数据绑定到datagridview

you are not setting object reference to an instance of object because you are just assigning dataset object to datasource which is not enough to bind Data.
you can use given code to bind your data to datagridview

SqlDataAdapter da = new SqlDataAdapter("Select * from tblcnt", ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
            DataSet ds = new DataSet();
            da.Fill(ds,"tblcnt");
            DataTable dt = ds.Tables["tblcnt"];
            dataGridView1.DataSource = dt.DefaultView;



试试吧.



try it..


这篇关于如何绑定DatagridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆