如何绑定的dataGridView与SQL语句中列预定义列(不添加新列)? [英] How to bind dataGridView predefined columns with columns from sql statement (without adding new columns)?

查看:180
本文介绍了如何绑定的dataGridView与SQL语句中列预定义列(不添加新列)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种优雅的方式,绑定预定义的DataGridView列从SQL语句的结果。



?例如:

  dataGridView1.Columns.Add(EID,ID); 
dataGridView1.Columns.Add(FName参数,名字);



一些SQL像 SELECT t.FirstName如FName参数,如t.EmpID EID从表T3 ...



然后我称之为 dataGridView1.DataSource = someDataSet.Tables [0] .DefaultView ;



最后通话列添加到我的DataGrid,但我只是想通过列名绑定不增加新的栏目。

的例子会给这样的结果是:

 表coulumns:ID,名字,FName参数,EID(ID和名字持有空单元格)

如何得到这样的:

 表coulumns:ID,名字或名字,ID 

祝商祺!


解决方案

除了的AutoGenerateColumns设置为false,您还需要设置DataPropertyName用于在DataGridView到相应的字段的每一列中的数据源。您可以在设计还是在设置DataSource属性之前代码中设置这一点。


Is there a elegant way, to bind predefined dataGridView columns with results from sql statement?

Example:

dataGridView1.Columns.Add("EID", "ID");
dataGridView1.Columns.Add("FName", "FirstName");

some sql like 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 coulumns: ID, FirstName, FName, EID (ID and FirstName holds empty cells)

How to get this:

Table coulumns: ID, FirstName or FirstName, ID

Best Regards!

解决方案

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.

这篇关于如何绑定的dataGridView与SQL语句中列预定义列(不添加新列)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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