获取gridview中已检查项的值

时间:2013-08-07 17:32:42

标签: c# asp.net

我将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

但是,未获得该值。

3 个答案:

答案 0 :(得分:3)

label1.Text = row.Cells[1].Value.ToString()

答案 1 :(得分:2)

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

label1.Text = Convert.ToString(row.Cells[1].Value);

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