Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用ComboBox从两个表填充DataGridView_C#_Datagridview - Fatal编程技术网

C# 如何使用ComboBox从两个表填充DataGridView

C# 如何使用ComboBox从两个表填充DataGridView,c#,datagridview,C#,Datagridview,我有以下两个表格: | Part_id | Name |Part_Type| |----------|---------|---------| | 1 | ABC | Nut | | 2 | DEF | Nut | | 3 | GHI | Washer | | Type | |----------| | Nut | | Screw | | Washer | 如

我有以下两个表格:

| Part_id  |   Name  |Part_Type| 
|----------|---------|---------|
|    1     |   ABC   |   Nut   | 
|    2     |   DEF   |   Nut   |
|    3     |   GHI   |  Washer |

|   Type   |   
|----------|  
|   Nut    |
|  Screw   |
|  Washer  |
如何填充DataGridView,如下所示:

| Part_id  |   Name  |     Part_Type     | 
|----------|---------|-------------------|
|    1     |   ABC   |   Nut(combobox)   | 
|    2     |   DEF   |   Nut(combobox)   |
|    3     |   GHI   |  Washer(combobox) |
string myCmdText = "SELECT * FROM Parts";
string myCmdText2 = "SELECT * FROM Types";
int yourDropdownIndex = 2;
MySqlCommand myQuery = new MySqlCommand(myCmdText, myConnection);
MySqlCommand myQuery2 = new MySqlCommand(myCmdText2, myConnection);
using (MySqlDataAdapter myAdapter = new MySqlDataAdapter(myQuery))
using (MySqlDataAdapter myAdapter2 = new MySqlDataAdapter(myQuery2))
{
    DataSet DS = new DataSet();
    DataTable DT2 = new DataTable();
    myAdapter.Fill(DS);
    myAdapter2.Fill(DT2);
    DS.Tables.Add(DT2);
    myDataGridView.DataSource = DS.Tables[0];
    foreach (DataGridViewRow row in myDataGridView.Rows)
    {
        var temp = row.Cells[yourDropdownIndex].Value;
        DataGridViewComboBoxCell comboCell = new DataGridViewComboBoxCell();
        comboCell.DisplayMember = "type";
        comboCell.ValueMember = "type";
        comboCell.DataSource = DS.Tables[1];
        row.Cells[yourDropdownIndex] = comboCell;
        row.Cells[yourDropdownIndex].Value = temp;
    }
}
在Part_type(零件类型)列上有一个包含每种类型的组合框,因此用户可以更改特定零件的类型

这就是我所拥有的:

string myCmdText = "SELECT * FROM Parts";
MySqlCommand myQuery = new MySqlCommand(myCmdText, myConnection);
using (MySqlDataAdapter myAdapter = new MySqlDataAdapter(myQuery))
{
    DataSet DS = new DataSet();
    myAdapter.Fill(DS);
    myDataGridView.DataSource = DS.Tables[0];
}
我尝试将DataGridView的Part_类型字段单元格强制转换为ComboxCells,并手动添加缺少的Part类型,但我得到了一个invalidcastexception


有没有办法做到这一点,最好比我尝试的方法更简单?

要使用下拉单元格类型,您需要做两件事:

创建DataGridViewComboxCell类型的单元格 用适当的数据填充它们 创建下拉单元格:

您必须按照代码中希望的类型创建所有列,而不是使用自动列生成。设置myDataGridView.AutoGenerateColumns=false;这需要在发出查询之前完成,因为以后不能更改列类型

但是有一个解决方法:对于以后更改类型,您必须将每个您想要成为特殊的单元格与您想要的类型的单元格进行交换。注意:这是关于单元格而不是列的,所以您需要在所有行上循环,就像我在下面的代码中所做的那样

用适当的数据填充它们:

您可以使用数据绑定:

为ComboBoxCells设置数据源,通常为第二个表中的查询;但您也可以将它们设置为与第一个查询结果或其他源不同的select。。 将案例中的DisplayMember和ValueMember设置为同一字段。 或者您可以用未绑定的数据填充Items集合

您可以使用以下内容:

| Part_id  |   Name  |     Part_Type     | 
|----------|---------|-------------------|
|    1     |   ABC   |   Nut(combobox)   | 
|    2     |   DEF   |   Nut(combobox)   |
|    3     |   GHI   |  Washer(combobox) |
string myCmdText = "SELECT * FROM Parts";
string myCmdText2 = "SELECT * FROM Types";
int yourDropdownIndex = 2;
MySqlCommand myQuery = new MySqlCommand(myCmdText, myConnection);
MySqlCommand myQuery2 = new MySqlCommand(myCmdText2, myConnection);
using (MySqlDataAdapter myAdapter = new MySqlDataAdapter(myQuery))
using (MySqlDataAdapter myAdapter2 = new MySqlDataAdapter(myQuery2))
{
    DataSet DS = new DataSet();
    DataTable DT2 = new DataTable();
    myAdapter.Fill(DS);
    myAdapter2.Fill(DT2);
    DS.Tables.Add(DT2);
    myDataGridView.DataSource = DS.Tables[0];
    foreach (DataGridViewRow row in myDataGridView.Rows)
    {
        var temp = row.Cells[yourDropdownIndex].Value;
        DataGridViewComboBoxCell comboCell = new DataGridViewComboBoxCell();
        comboCell.DisplayMember = "type";
        comboCell.ValueMember = "type";
        comboCell.DataSource = DS.Tables[1];
        row.Cells[yourDropdownIndex] = comboCell;
        row.Cells[yourDropdownIndex].Value = temp;
    }
}
如有必要,现在可以将单个单元格强制转换为DataGridViewComboxCell:


您可能希望在类级别声明数据集DS。

不如更改列类型那么简单,这是不可能的,但也不是那么难。。请参阅我的更正和添加的注释!–谢谢这是我必须要写的代码,不幸的是,没有更简单的方法。但是,您不应将DataGridViewTextBoxCell强制转换为DataGridViewComboxCell,因为这将导致InvalidCastException,因此我将从答案中删除最后一位。如果且仅当该单元格实际上是DataGridViewComboxCell或您无法访问其特殊属性时,您可以且必须执行强制转换,最值得注意的是项目集合。因此,为了安全起见,您可能需要在强制转换之前进行检查:if cell.EditType==TypeOfDataGridViewComboxCell..-或者用as强制转换并检查null..是的,我试图转换另一个单元格,这就是为什么我得到了异常:p。。。奇怪的是,combocell的下拉背景是黑色的,我找不到一种方法使其成为通常的黑白?奇怪,不是这里。你对手机还做了什么?那么文本是白色的吗??