2012-02-09 101 views
1

我有3個DataGridViews。使用DataGridView1和DataGridView2,您可以選擇行。 按下按鈕後,將比較DataGridView1和DataGridView2中的行,並在DataGridView3中設置具有相同值的每一行。在第三個DataGridView中選擇時,如何在2個DataGridView中選擇一行?

我想要的是當你在DataGridView3中選擇一行時,在DataGridView1和DataGridView2中選擇相同的行。

的代碼我有,而且工作原理是:

private int ShowSelected(int selectedId, Boolean sBool) 
{ 
    DataTable dt = DataGridView1.DataSource; 

    if(!sBool) 
     currentGrid = DataGridView1; 


    int indexCounter = 0;    
     foreach (DataRow dr in dt.Rows) 
     { 
      int cellIdDgv = Convert.ToInt32(dr["cellId"]); 

       if (selectedId == cellIdDgv) 
       { 
        if (sBool) 
        { 
         DataGridView1.Rows[indexCounter].Selected = true; 
         DataGridView1.FirstDisplayedCell = DataGridView1.Rows[indexCounter].Cells[0]; 
        } 
        else 
        { 
         DataGridView2.Rows[indexCounter].Selected = true; 
         DataGridView2.FirstDisplayedCell = DataGridView2.Rows[indexCounter].Cells[0]; 
        } 
       } 

     indexCounter++; 
    } 
} 

但我想是這樣的,這樣你就不會在整個電網必須循環:

string selection = "cellId = " + selectedId; 
DataRow[] drResult = dt.Select(selection); 
int rowId = drResult.RowId; 

if (sBool) 
{ 
    DataGridView1.Rows[rowId].Selected = true; 
     DataGridView1.FirstDisplayedCell = DataGridView1.Rows[rowId].Cells[0]; 
} 
else 
{ 
    DataGridView2.Rows[rowId].Selected = true; 
     DataGridView2.FirstDisplayedCell = DataGridView2.Rows[rowId].Cells[0]; 
} 

我該如何做這項工作?

+0

您可以使用['BindingSource.Filter'](http://msdn.microsoft.com/zh-cn/library/system.windows.forms.bindingsource.filter.aspx)。這將使您能夠過濾第三個DataGridView。該鏈接也有例子。 – MoonKnight 2012-02-09 10:54:36

回答

0

爲了擴大對我的評論,並提供本地(計算器)解決方案:

過濾是使用BindingSource.Filter屬性完成的。 MSDN上提供的例子是:

private void PopulateDataViewAndFilter() 
    { 
     DataSet set1 = new DataSet(); 

     // Some xml data to populate the DataSet with. 
     string musicXml = 
      "<?xml version='1.0' encoding='UTF-8'?>" + 
      "<music>" + 
      "<recording><artist>Coldplay</artist><cd>X&amp;Y</cd></recording>" + 
      "<recording><artist>Dave Matthews</artist><cd>Under the Table and Dreaming</cd></recording>" + 
      "<recording><artist>Dave Matthews</artist><cd>Live at Red Rocks</cd></recording>" + 
      "<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" + 
      "<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" + 
      "</music>"; 

     // Read the xml. 
     StringReader reader = new StringReader(musicXml); 
     set1.ReadXml(reader); 

     // Get a DataView of the table contained in the dataset. 
     DataTableCollection tables = set1.Tables; 
     DataView view1 = new DataView(tables[0]); 

     // Create a DataGridView control and add it to the form. 
     DataGridView datagridview1 = new DataGridView(); 
     datagridview1.AutoGenerateColumns = true; 
     this.Controls.Add(datagridview1); 

     // Create a BindingSource and set its DataSource property to 
     // the DataView. 
     BindingSource source1 = new BindingSource(); 
     source1.DataSource = view1; 

     // Set the data source for the DataGridView. 
     datagridview1.DataSource = source1; 

     //The Filter string can include Boolean expressions. 
     source1.Filter = "artist = 'Dave Matthews' OR cd = 'Tigerlily'"; 
    } 

爲了方便,你可能要創建一個FilterBuilder

編輯:爲了避免過濾或在你的問題中提到的上述循環,使用鏈接到你的`DataGridView1然後BindingSource等創建DataTable

DataTable DT; // Obtained from the relevent DGV. 
DataView view = new DataView(DT); 
DataView thisDV = new DataView(); 

// Then you can use... 
thisDV.Find(thisOrThat); 
thisDV.FindRows(thisOrThat); 

要找到數據中的相關行,然後這可以用於餵給你 s中的Selection()

我希望這會有所幫助。

+0

這不會選擇已經存在的datarow,如果我理解,這會將新數據放入我的DataGridView中? – Remco 2012-02-09 11:07:55

+0

您可以在相關的'DataGridView'上創建過濾器(由您希望的任何事件選擇 - 另一個'DataGridView'上的'CellClick'等),並使用它過濾相關記錄。 – MoonKnight 2012-02-09 11:10:29

+0

但我仍然不明白。這是如何選擇我的2個網格中的行? – Remco 2012-02-09 11:12:19