獲取

2013-08-07 46 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; 
    } 
}