选择由查询填充的未绑定数据网格视图行

时间:2013-04-23 19:35:17

标签: c# winforms datagridview

我有一个填充看起来像

的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填充该查询后从DGV打开BRTDetail。这是我第一次使用未绑定的网格。我通常使用的代码如下。显然它不起作用,因为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();

    }

2 个答案:

答案 0 :(得分:0)

如何创建一个名为something的模板字段:

  <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. 


   }

答案 1 :(得分: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();
            }
        }