2014-10-30 47 views
0

我的一个项目,其读取在硬件配置的XML文件,并解析和排序由协议类型其数据和显示在一个两列gridview的进行进一步的修改信道工作。列1将通道名称保存为DataGridViewTextBoxColumn,而列2设置为DataGridViewComboBoxColumn,其中用户应该能够从通过SQL数据库(通道类型)的查询生成的下拉列表中选择一个项目。如何用C#改变的数据源为DataGridVewComboBoxColumn在运行时

的SQL数据库表本身有几个列中显示如下: 接口通道型

“接口”列被用作键来区分“通道型”如果他们是RS232,RS422 ,RS485等。

正如我填充DataGridView的,我整理出来,通过所有渠道RS422上市第一次运行,其次是RS232列表等。然而,要做到这一点,我需要能够改变数据源的ComboBoxColumn有不同的查询,如第一个将是 “选择CHANNEL_TYPE FROM表1 WHERE接口=‘422’” 而对于第二个我将需要运行 “选择CHANNEL_TYPE FROM表1 WHERE接口=‘RS232’”

所以在代码中,我有以下几点:

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; 
} 

不幸的是,我不知道如何正确地更改数据源,因为这样做它用这种方法只会显示最后一个数据源定义中的项目列表。

任何帮助,并建议将不胜感激!

谢谢!

+0

为什么不创建两个数据网格,每个查询一个? – Roger 2014-10-30 19:06:10

+0

另外,如何在查询中使用GROUP BY来区分这两种类型? – Roger 2014-10-30 19:06:59

+0

我无法使用GROUP BY,因为Channel-Type名称对于任一接口都可能具有相同的名称,并会导致我在进一步编辑时遇到问题。此外,上市将是非常大的。 具有多个数据网格将是一个解决办法,如果我不能得到这个工作,但我不知道该GUI将如何清洁一下,因为不是所有的硬件配置文件必须按照它们多个不同的接口。 – Pita 2014-10-30 19:11:58

回答

0

大量尝试后,我终于找到了做题的方法。我不需要通过列来完成这个任务,而是需要通过单元格来完成我所需要的行。

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; 
    } 
}