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。这是我第一次不得不使用未绑定的网格。我通常使用的代码如下。显然它不起作用,因为此时 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();

    }
4

2 回答 2

0

How bout this create an template field named something :

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

Then in your code behind for the click event you do this :

 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. 


   }
于 2013-04-23T20:04:46.570 回答
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();
            }
        }
于 2013-04-30T19:40:52.840 回答