2014-10-30 22 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; 
    } 
}