DataGridView comboboxcolumn动态绑定 [英] DataGridView comboboxcolumn dynamic binding

查看:96
本文介绍了DataGridView comboboxcolumn动态绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为C#中的SQL Server数据库构建一个非常基本的表编辑器;基本上有点像MS Access用来编辑表格的旧表格。

I'm building a pretty basic table editor for an SQL Server DB in C#; basically a bit like the old forms that used to come with MS Access for editing tables.

到目前为止,我在表格上有一个组合框,您可以在其中选择想要的表编辑,然后创建一个datagridview,显示已在组合框中选择的表。

So far I have a combobox on the form where you select the table you want to edit, and then a datagridview that shows the table that has been selected in the combobox.

我想在运行时添加组合框列,具体取决于该列是否包含,所以用户可以看到他们选择的值,而不仅仅是ID。

I want to add comboboxcolumns at runtime according to whether the column has a relationship or not, so the user can see the value they're picking, not just the ID.

所以基本上我不知道combobox列在哪里直到用户选择了一个表,才需要(或者它们需要绑定到什么数据)。因此,这必须在运行时在代码中完成。

So basically I don't know where the comboboxcolumns need to be (or what data they need to be bound to) until the user has selected a table. Hence this has to be done in code at runtime.

到目前为止,我正在使用以下数据填充datagridview:

So far I'm populating the datagridview using:

String connectionString = sConnection;
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource2.DataSource = table; 

所以我猜我的路线会是这样的:填充dgv,遍历各列对于任何有关系的东西(我该怎么做?!?),然后将类型更改为comboboxcolumn,然后将其displaymember和valuemember属性更改为所需的值(我需要从某个地方获取。) .how ???)

So I'm guessing my route will be something like: populate the dgv, loop through the columns looking for anything with a relationship (how do I do that?!?), then change the type to comboboxcolumn, and then change the displaymember and valuemember properties for it to whatever they need to be (which I will need to get from somewhere...how???)

有人可以用我的方式抛出一些代码来指示我正确的方向吗?

Can someone throw some code my way to point me in the right direction?

谢谢

推荐答案

有可能:假设您有Employee和Department表,可以发现Employee表中的deptId有FK到Department.DeptId,那么,您就可以获取Department表的所有列。

it's possible: let's say you have the tables Employee and Department, you can find out that deptId in Employee table has a FK to Department.DeptId, fine, then you can get all columns of the Department table.

如何查找您感兴趣的FK(对于当前选定的FK table),例如,google,我已经找到了它: http://blog.sqlauthority.com/2007/09/04/sql-server-2005-find-tables-with-foreign-key-constraint-in-Database

for how to find the FK you are interested in ( for the currently selected table ), google for it, for example I have found this: http://blog.sqlauthority.com/2007/09/04/sql-server-2005-find-tables-with-foreign-key-constraint-in-database

这篇关于DataGridView comboboxcolumn动态绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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