1

我有一个gridview定义为

<asp:GridView ID="gv1" runat="server" AutoPostBack="true" onselectedindexchanged="gv1_SelectedIndexChanged">
     <Columns>
        <asp:TemplateField>
           <ItemTemplate>
                <asp:CheckBox ID="chk1" runat="server" />
                <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" ReadOnly="true" />
           </ItemTemplate>
        </asp:TemplateField>
     </Columns>
</asp:GridView>

代码被处理为

protected void gv1_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach(GridViewRow row in gv1.Rows)
    {
       CheckBox check1 = (CheckBox)row.FindControl("chk1");
       if(check1 != null && check1.Checked)
       {
           label1.Text = row.Cells[1].Text;
       }
    }
}

所需的列位于索引 1

但是,未获得该值。

4

3 回答 3

3
label1.Text = row.Cells[1].Value.ToString()
于 2013-08-07T17:35:00.873 回答
2

你需要价值而不是文字。像这样做

label1.Text = Convert.ToString(row.Cells[1].Value);
于 2013-08-07T17:37:09.470 回答
0

试试下面这行可能对某人有帮助

var temp = ot_approval_grid.DataKeys[GridView.RowIndex].Value;
//ot_approval_grid is the id of grid view

foreach (GridViewRow GridView in ot_approval_grid.Rows)
{
    CheckBox isapplicable = (CheckBox)GridView.FindControl("chkrow");
    if (isapplicable != null && isapplicable.Checked)
    {
        //var temp = GridView.Cells["OTApplicationID"];
        var temp = ot_approval_grid.DataKeys[GridView.RowIndex].Value;
    }
}
于 2016-03-07T12:09:43.697 回答