DataGridView 组合框列绑定
好的,我有一个 SQL Server 数据库。
我正在为其构建一个非常简单的前端,其中包含一个用于选择表格的组合框、一个用于选择要显示的字段的复选框以及一个显示数据的数据网格视图。
我遇到的问题(对于大多数人来说这可能是一个非常简单的问题,但我一般对数据库很陌生)是我有一列与另一列有关系,而 datagridview 仅显示该列的 ID字段值而不是实际值。
为了澄清这一点,我有一张表(称为“ItemTypes”),其中包含以下字段: ID 项目类型 CAT1 CAT2
和另一个表(称为 CAT1),其字段为:
ID CAT1
你可以明白我要说的是什么。在 datagridview 中,我从 ItemTypes 表导入所有数据,但我想让 CAT1 列成为一个组合框,从 CAT1s 表的 CAT1 字段填充。但目前它显示的是 CAT1 的 ID 字段 - 对用户来说是一个毫无意义的数字。
以下是我必须将数据导入 DGV 的代码:
private void GetData(string selectCommand)
{
dataGridView2.DataSource = bindingSource2;
try
{
String connectionString = sConnection;
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
dataAdapter.Fill(table);
bindingSource2.DataSource = table;
dataGridView2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}
catch (SqlException)
{
}
任何帮助将不胜感激。
干杯
OK so I have an SQL Server database.
I'm building a very simple frontend for it that consists of a combobox where I pick the table, a checkedlistbox where I choose which fields to show, and a datagridview that shows the data.
The problem I'm having (which is probably a very simple one for most people, but I'm pretty new to databases in general) is that I have a column with a relationship to another, and the datagridview just shows the ID of the fields value instead of the actual value.
To clarify, I have one table (called "ItemTypes") with fields:
ID
Item Type
CAT1
CAT2
and another table (called CAT1s) with the fields:
ID
CAT1
You can see where I'm going with this. In the datagridview I'm importing all the data from the ItemTypes table, but I want to make the CAT1 column a combobox to be populated from the CAT1s table's CAT1 field. Except at the moment it's showing the ID field from CAT1s - a meaningless number to the user.
Here is the code I have to import the data into the DGV:
private void GetData(string selectCommand)
{
dataGridView2.DataSource = bindingSource2;
try
{
String connectionString = sConnection;
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
dataAdapter.Fill(table);
bindingSource2.DataSource = table;
dataGridView2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}
catch (SqlException)
{
}
Any help would be mucho appreciated.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要显示两个或多个表中的数据,您必须将它们连接在一起。我建议你阅读 SQL 中的表 JOINS。
这是您可以开始的地方
http://w3schools.com/sql/sql_join.asp
To show data from two or more tables, you would have to JOIN them together. I suggest you read up on table JOINS in SQL.
Here is a place where you can start
http://w3schools.com/sql/sql_join.asp