2013-04-23 130 views
1

填充我有填充一個DGV,看起來像通過查詢

private void textBox6_Leave(object sender, EventArgs e) 
    { 
     DataClasses3DataContext db = new DataClasses3DataContext(); 

     int matchedAdd = (from c in db.GetTable<prop>() 
          where c.HOUSE_NO.Contains(textBox1.Text) && c.Direction.Contains(textBox2.Text) && c.street.Contains(textBox3.Text) && c.SUFF.Contains(textBox4.Text) 
          select c.ID).SingleOrDefault(); 

     var before = (from c in db.GetTable<prop>() 
         where c.ID < matchedAdd 
         orderby c.PARCEL descending 
         select c).Take(6); 

     var after = (from c in db.GetTable<prop>() 
        where c.ID > matchedAdd 
        orderby c.PARCEL 
        select c).Take(6); 

     var endResult = before.Concat(after); 

     dgvBRT.DataSource = endResult; 

    } 

我需要能夠通過查詢其填充後,選擇從DGV行和開BRTDetail查詢綁定的DataGridView的選中行。這是我第一次不得不使用未綁定的網格。我通常使用的代碼如下。很明顯,它不起作用,因爲BRTDataSet在那個時候是不相關的。在這種情況下如何選擇一行?

 private void dgvBRT_DoubleClick(object sender, EventArgs e) 
    { 
     System.Data.DataRowView SelectedRowView; 
     BRTDataSet.propRow SelectedRow; 

     SelectedRowView = (System.Data.DataRowView)propBindingSource.Current; 
     SelectedRow = (BRTDataSet.propRow)SelectedRowView.Row; 

     frmBRTDetail BRTDetail = new frmBRTDetail(this); 
     BRTDetail.LoadBRTNumberKey(SelectedRow.PARCEL, null); 
     BRTDetail.LoadBldgKey(SelectedRow.BLDG_CD, null); 
     BRTDetail.Show(); 

    } 

回答

0

好吧,我想通了。這是我做到的。

private void dgvBRT_DoubleClick(object sender, EventArgs e) 
     { 

     if (dgvBRT.SelectedCells.Count > 0) 
      { 
      int selectedrowindex = dgvBRT.SelectedCells[0].RowIndex; 

      DataGridViewRow selectedRow = dgvBRT.Rows[selectedrowindex]; 

      string BRTNumber = Convert.ToString(selectedRow.Cells["BRTNumber"].Value); 
      // Open selected row 

       frmBRTDetail BRTDetail = new frmBRTDetail(this); 
       BRTDetail.LoadBRTNumberKey(BRTNumber, null); 
       BRTDetail.Show(); 
      } 
     } 
0

如何回合此創建一個模板字段命名的東西:

<ItemTemplate> 
    <asp:LinkButton ID="link_button" runat="server" Text="Select" OnClick="link_button_Click"> </asp:LinkButton> 
    </ItemTemplate> 

然後在後面的代碼爲click事件你這樣做:

protected void link_button_Click(object sender, EventArgs e) 
    { 
     GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow; 

     // this selects your row and you can access the controls of your grid. 


    } 
+0

ItemTemplate代碼適用於webforms是否正確?這是一個winform應用程序。 – korrowan 2013-04-23 20:51:31

+0

我沒有看到它是一個winform。我認爲邏輯背後的代碼仍然有效。你可能需要調整語法。 – briskovich 2013-04-23 22:05:26