C# 如何将 dataGridView 预定义列与 sql 语句中的列绑定(不添加新列)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1741299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bind dataGridView predefined columns with columns from sql statement (without adding new columns)?
提问by Jooj
Is there a elegant way, to bind predefined dataGridView columns with results from a SQL statement?
有没有一种优雅的方法,将预定义的 dataGridView 列与 SQL 语句的结果绑定?
Example:
例子:
dataGridView1.Columns.Add("EID", "ID");
dataGridView1.Columns.Add("FName", "FirstName");
Some SQL like
一些 SQL 喜欢
SELECT t.FirstName AS FName, t.EmpID AS EID
FROM table t ...
and then I call
然后我打电话
dataGridView1.DataSource = someDataSet.Tables[0].DefaultView;
The last call add columns to my datagrid but I just want to bind it by column name not to add new columns.
最后一次调用将列添加到我的数据网格,但我只想按列名绑定它而不是添加新列。
The example will give a result like this:
该示例将给出如下结果:
Table columns: ID, FirstName, FName, EID (ID and FirstName holds empty cells)
表格列:ID、FirstName、FName、EID(ID 和 FirstName 包含空单元格)
How to get this:
如何获得这个:
Table columns: ID, FirstName or FirstName, ID
Best regards!
此致!
回答by Neil Barnwell
I think the DataGridView has an AutoGenerateColumns
property, doesn't it?
我认为 DataGridView 有一个AutoGenerateColumns
属性,不是吗?
dataGridView1.AutoGenerateColumns = True;
From the MSDN docs:
来自 MSDN 文档:
public bool AutoGenerateColumns { set; get; } Member of System.Windows.Forms.DataGridView
Summary: Gets or sets a value indicating whether columns are created automatically when the System.Windows.Forms.DataGridView.DataSource or System.Windows.Forms.DataGridView.DataMember properties are set.
Returns: true if the columns should be created automatically; otherwise, false. The default is true.
public bool AutoGenerateColumns { set; 得到; System.Windows.Forms.DataGridView 的成员
摘要:获取或设置一个值,该值指示在设置 System.Windows.Forms.DataGridView.DataSource 或 System.Windows.Forms.DataGridView.DataMember 属性时是否自动创建列。
返回: 如果应自动创建列,则为 true;否则为假。默认值为真。
The property isn't on the Properties window though, you have to set it via code as in my example.
但是,该属性不在“属性”窗口中,您必须像在我的示例中一样通过代码进行设置。
回答by adrianos
How about adding columns to the Columns tag of your gridview like so?
像这样将列添加到 gridview 的 Columns 标签怎么样?
<Columns>
<asp:BoundField DataField="EID" HeaderText="ID" />
<asp:BoundField DataField="FName" HeaderText="First name" />
...
回答by jaybz
Aside from setting AutoGenerateColumns
to false, you also need to set DataPropertyName
for each column in the DataGridView
to the corresponding field in the data source. You can set this in the designer or in code before setting the DataSource
property.
除了设置AutoGenerateColumns
为false之外,还需要为数据源中的对应字段设置DataPropertyName
每一列DataGridView
。您可以在设置DataSource
属性之前在设计器或代码中设置它。
回答by Iftikhar
Use dataGridView1.Columns["FName"].DataPropertyName = "FName"
where FNameis column in your data table.
使用dataGridView1.Columns["FName"].DataPropertyName = "FName"
其中FName是数据表中的列。
回答by Hannington Mambo
If you are doing WinForm, the important part is setting the DataPropertyName
property to match the DataTable Column name. You can do it in the designer or code as follows:
如果您正在使用 WinForm,重要的部分是设置DataPropertyName
属性以匹配 DataTable 列名称。您可以在设计器或代码中执行如下操作:
Me.AccountDataGridView.Columns("Account").DataPropertyName = "Account"
Of course, having set this:
当然,设置了这个:
Me.AccountDataGridView.AutoGenerateColumns = False