数据集到的DataGridView与pre定义的列 [英] DataSet to DataGridView with pre-defined Columns

查看:159
本文介绍了数据集到的DataGridView与pre定义的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的WinForms项目已经设置一个DataGridView。列是在设计文件中的所有定义。当我去运行我的SQL在DataGridView的列被替换为在查询中的列名。

我如何保持列由DataGridView的定义,只是从数据集从数据集到GridView添加行。

当他们出现在DataGridView

  //列
 名称|地址|城|国家|邮编|账户民|

//填充的GridView
的DataSet ds为Account.search(strSearch);
accountGrid.DataSource = ds.Tables [0];

//查询
字符串SQL =SELECT DISTINCT acctNum,姓名,地址,城市,州,邮编从账户;
返回DataService.GetDataSet(SQL);

//列的最终结果
acctNum |名称|地址|城市|国家|拉链
 

解决方案

您需要的列在你的数据表,在您的DataGridView列关联。

通过更新DGV列 DataPropertyName 属性与你想显示在DGV列中的数据表的列名执行此操作。例如,将 DataPropertyName 属性您DGV帐户数 acctNum 。注意:您可以在设计师通过编辑列对话框轻松地做到这一点(右键从弹出的点击你的DGV在设计和选择编辑列...带来的对话框上)。

请注意,您需要从创建设置你的DataGridView的的AutoGenerateColumns 来prevent它列自身。我相信您只能通过code此设置。

I have a DataGridView that I've setup in a WinForms project. The Columns are all defined in the designer file. When I go to run my SQL the columns in the DataGridView get replaced with the columns names in the query.

How do I keep the columns as defined by the DataGridView, and just add the rows from the dataset from the dataset to the GridView.

//Columns as they appear in the DataGridView
 Name | Address | City | State | Zip | Account Num |

//Populate the gridView
DataSet ds = Account.search(strSearch);
accountGrid.DataSource = ds.Tables[0];

//query
string sql = "SELECT distinct acctNum, name, address, city, state, zip from accounts";
return DataService.GetDataSet(sql);

//The End Result of the columns
acctNum | name | address |city | state | zip

解决方案

You need to associate columns in your DataTable with columns in your DataGridView.

Do this by updating the DataPropertyName property of a DGV column with the name of the column in the DataTable you wanted displayed in that DGV column. For example, set the DataPropertyName property in your DGV for account number to acctNum. Note you can easily do this in the designer through the "Edit Columns" dialog (bring the dialog up by right clicking your DGV in the designer and selecting "Edit Columns..." from the pop up).

Note that you'll need to set your DataGridView's AutoGenerateColumns to false to prevent it from creating columns on its own. I believe you can only make this setting through code.

这篇关于数据集到的DataGridView与pre定义的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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