C# 如何在运行时在C中更改DataGridVewcomboxColumn的数据源

C# 如何在运行时在C中更改DataGridVewcomboxColumn的数据源,c#,C#,我正在从事一个项目,该项目读取硬件配置xml文件,并按协议类型和通道对其数据进行解析和排序,以便在两列gridview中显示进一步的修改。列1将频道名称设置为DataGridViewTextBoxColumn,而列2设置为DataGridViewComboxColumn,用户应能够从下拉列表中选择从SQL数据库频道类型查询生成的项目 SQL数据库表本身有两列,如下所示: 接口通道类型 如果通道类型为RS232、RS422、RS485等,则接口列用作区分通道类型的键 在填充DataGridView

我正在从事一个项目,该项目读取硬件配置xml文件,并按协议类型和通道对其数据进行解析和排序,以便在两列gridview中显示进一步的修改。列1将频道名称设置为DataGridViewTextBoxColumn,而列2设置为DataGridViewComboxColumn,用户应能够从下拉列表中选择从SQL数据库频道类型查询生成的项目

SQL数据库表本身有两列,如下所示: 接口通道类型

如果通道类型为RS232、RS422、RS485等,则接口列用作区分通道类型的键

在填充DataGridView时,我将其排序为首先运行所有RS422通道列表,然后是RS232列表,依此类推。然而,要做到这一点,我需要能够更改ComboBoxColumn的数据源,使其具有不同的查询,例如第一个查询 从接口为RS422的表1中选择通道类型 而对于第二个,我需要跑步 从接口为RS232的表1中选择通道类型

因此,在代码中,我有以下内容:

private void scanner()
{
    //...code for parser that assembles a List<> ...

    ChannelTypeColumn.DataSource = Populate("SELECT Channel_Type FROM Table1 WHERE Interface='RS422'");
    ChannelTypeColumn.ValueMember = "Channel_Type";
    ChannelTypeColumn.DisplayMember = ChannelTypeColumn.ValueMember;

    for(int x = 0; x < ChannelRS422List.Count; x++)
    {
        RowDataBuffer[0] = ChannelRS422List.channel;
        dgv.Rows.Add(RowDataBuffer);
    }

    ChannelTypeColumn.DataSource = Populate("SELECT Channel_Type FROM Table1 WHERE Interface='RS232'");

    for(int x = 0; x < ChannelRS232List.Count; x++)
    {
        RowDataBuffer[0] = ChannelRS232List.channel;
        dgv.Rows.Add(RowDataBuffer);
    }
}

private DataTable Populate(string query)
{
    SqlCommand command = new SqlCommand(query, connection);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = command;

    DataTable table = new DataTable();
    table.Locale = System.Globalization.CultureInfo.InvariantCulture;
    adapter.Fill(table);

    return table;
}
不幸的是,我不知道如何正确地更改数据源,因为使用此方法只会显示上一个数据源定义中的项目列表

任何帮助和建议都将不胜感激


谢谢

经过多次尝试,我终于找到了解决这个问题的方法。我不需要按列来做,而是需要按我需要的行的单元格来做

private void scanner()
{
    //...code for parser that assembles a List<> ...

    for(int x = 0; x < ChannelRS422List.Count; x++)
    {
        RowDataBuffer[0] = ChannelRS422List.channel;
        dgv.Rows.Add(RowDataBuffer);
    }

    int rowCount = 0;
    for (int row = 0; row < dgv.Rows.Count; row++)
    {
        DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)(dgv.Rows[row].Cells[1]);
        cell.DataSource = Populate("SELECT Channel_Type FROM Table1 WHERE Interface='RS422'");
        cell.ValueMember = "Channel_Type";
        cell.DisplayMember = cell.ValueMember;
        rowCount = row+1;
    }

    for(int x = 0; x < ChannelRS232List.Count; x++)
    {
        RowDataBuffer[0] = ChannelRS232List.channel;
        dgv.Rows.Add(RowDataBuffer);
    }

    for (int row = rowCount; row < dgv.Rows.Count; row++)
    {
        DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)(dgv.Rows[row].Cells[1]);
        cell.DataSource = Populate("SELECT Channel_Type FROM Table1 WHERE Interface='RS232'");
        cell.ValueMember = "Channel_Type";
        cell.DisplayMember = cell.ValueMember;
        rowCount = row+1;
    }
}

为什么不创建两个数据网格,每个查询一个?或者,在查询中使用GROUP BY来区分这两种类型如何?我不能使用GROUP BY,因为通道类型名称可能与任一接口的名称相同,并且会导致进一步编辑的问题。此外,上市规模将非常大。如果我无法使用多个数据网格,那么使用多个数据网格将是一个解决方案,但我不确定GUI看起来会有多干净,因为并非所有硬件配置文件中都必须有多个不同的接口。我还考虑过,如果不使用数据源,我只使用一个项目列表,其中列出了从查询并以这种方式插入它。假设每个单元格也可以这样做,但理想情况下,如果有办法的话,我希望使用DataSource方法。