检索GridView字符串值

本文关键字:字符串 GridView 检索 | 更新日期: 2023-09-27 17:54:10

我有一个GridView -

<asp:GridView ID="table_example" Runat="server" 
DataSourceID="SqlDataSource3" AutoGenerateColumns="False" 
ClientIDMode="Static" onprerender="table_example_PreRender">
    <Columns>
        <asp:TemplateField>
     <HeaderTemplate>
         <asp:Label ID="Label1" runat="server" Text="Select"></asp:Label>
     </HeaderTemplate>
     <ItemTemplate>
        <asp:CheckBox ID="chkRow" runat="server" OnCheckedChanged="CheckBoxPN_CheckedChanged" AutoPostBack="true"/>
     </ItemTemplate>
  </asp:TemplateField>
        <asp:BoundField HeaderText="profileID" DataField="profileID" 
                        SortExpression="profileID"></asp:BoundField>
        <asp:BoundField HeaderText="profileName" DataField="profileName" 
                        SortExpression="profileName"></asp:BoundField>
    </Columns>
</asp:GridView>

其中chkRow -

后面的代码
protected void CheckBoxPN_CheckedChanged(Object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;
        GridViewRow row = (GridViewRow)chk.NamingContainer; 
        //when a user clicks the checkbox I need the string from profileName in that row. 
    }

在调试时,我可以得到checkBox changed事件,但是需要从profileName单元格从其各自的行单元格检索字符串。

我试过这样做-

string c = row.Cells("profileName").value.toString();

然而,这没有工作,我怎么能得到字符串?

检索GridView字符串值

如果您使用BoundField,则需要使用Cells[index].Text。因为它在第三列

string profileName = row.Cells[2].Text;

交货:你可以直接给出;

string profilename = gridview.Rows[0].Cells[1].Text;