获取

2013-08-07 44 views
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

然而gridview的内部检查的项的值,没有获得该值。

+0

我们就可以抓取每一行并将其值设置。我相信你想抓住selectedrow – Charles380

+0

不能只引用this.chk1并从代码隐藏中获取值? – Botonomous

+0

你的意思是像这样GridViewRow row = gv1.SelectedRow; row.Cells [1] .Text? – user544079

回答

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

您需要的不是文字。像这样做

label1.Text = Convert.ToString(row.Cells[1].Value); 
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; 
    } 
}