绑定GridView中的问题 [英] problem in binding gridview
本文介绍了绑定GridView中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我是Windows应用程序的新手.我正在尝试绑定gridview,但是无法绑定它.使用中断和视图数据集时,数据集包含结果,但未显示在网格中.
代码是
Hi
I am new in windows application. I am trying to bind gridview but am unable to bind it. When using break and view dataset data set contains result but not showing in grid.
code is
string sql = @"select * from SmsContent";
SqlConnection conn = new SqlConnection(ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "SmsContent");
DG_sms.DataSource = ds;
推荐答案
可能是您的DataGridView(它是DGV,不是这样)对于使用DataSet中的哪个表感到困惑(我知道只有1个,但仍然有.)
试试
It could be that your DataGridView (it is a DGV, isn''t it) is confused about which table from the DataSet to use(I know that there is only 1, but still).
Try
string sql = @"select * from SmsContent";
SqlConnection conn = new SqlConnection(ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "SmsContent");
DG_sms.DataSource = ds.Tables["SmsContent"];
代替.
或者,保留现有代码并设置网格的DataMember
属性
instead.
Alternatively keep your existing code and set the DataMember
property of your grid
DG_sms.DataMember = "SmsContent";
我想您使用数据集的数据表错过了.
试试:
DG_sms.DataSource = ds.Tables[0];
I guess you missed using datatable of dataset.
Try:
DG_sms.DataSource = ds.Tables[0];
好,我刚刚对此进行了测试,并且可以正常工作:
OK I just tested this and it is working:
SqlConnection conn = new SqlConnection("YOUR CONNECTION STRING");
conn.Open();
SqlCommand cmd = new SqlCommand(@"YOUR SQL QUERY", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
dataGridView1.AutoGenerateColumns = true;
conn.Close();
这篇关于绑定GridView中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文