如何使用数据集i填充datagrid视图特定列

时间:2013-07-30 09:04:54

标签: c# datagridview

我有一个数据集。我试图将我的数据集中的值填充到我的gridview中,如下所示: http://i.stack.imgur.com/xTvQd.png

SqlCommand cmd = new SqlCommand("UnclosedTransactionfetch", con.connect);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@locid", SqlDbType.Int).Value = locid;
da.SelectCommand = cmd;
da.Fill(ds);
DgvUnclosed.Columns("Carid").Name = ds.Tables(0).Columns(0).ToString();

但是我的数据网格视图没有填充....我的代码有什么问题

2 个答案:

答案 0 :(得分:0)

仅为所需列创建列, 并将其与source绑定,它将仅绑定您在gridview结构中提到的列

也将autogeneratecolumns设为false

以窗口形式

仅为您想要的字段提供数据属性名称,其余为空白

enter image description here

输出

enter image description here

答案 1 :(得分:0)

尝试关注我的朋友:

SqlCommand cmd = new SqlCommand("UnclosedTransactionfetch", con.connect);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@locid", SqlDbType.Int).Value = locid;
da.SelectCommand = cmd;
da.Fill(ds);
DgvUnclosed.DataSource = ds.Tables[0]; //In this part the dataset pass all fields to the gridview

如果您需要过滤某些列,请添加下一行代码:

DgvUnclosed.Columns["CarlID"].Visible = false;
相关问题