从C#获取gridview中的boundfield值

时间:2011-08-08 09:11:12

标签: c# asp.net

我在asp.net中有一个带有boundfield的数据网格。在RowCommand事件中,我想获取此boundfield的值。 columns标签中的boundfield如下所示:

<asp:BoundField DataField="LoginID" HeaderText="LoginID" InsertVisible="False" 
                ReadOnly="True" SortExpression="LoginID" />

随附的C#会是什么?

由于

2 个答案:

答案 0 :(得分:2)

在Row_Command事件中,您可以通过以下方式检索所单击行的索引:

 void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {

    //Check if it's the right CommandName... 
    if(e.CommandName=="Add")
    {
      //Get the Row Index
      int index = Convert.ToInt32(e.CommandArgument);

      // Retrieve the row
      GridViewRow row = ContactsGridView.Rows[index];

      // Here you can access the Row Cells 
      row.Cells[1].Text

    }
  }    

答案 1 :(得分:0)

  protected void gv_research_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int index = Convert.ToInt32(e.CommandArgument);

                if (e.CommandName == "editResearch")
                {

                    txt_researchName.Text = gv_research.Rows[index].Cells[1].Text.TrimEnd();

                }


            }
            catch (Exception ee)
            {
                string message = ee.Message;

            }
        }

<强>的.aspx:

<asp:ImageButton ID="btn_Edit" runat="server"  CommandArgument='<%#((GridViewRow)Container).RowIndex%>' CommandName="editResearch" />