Get specific data row in gridview?

不羁岁月 提交于 2019-12-05 23:47:45

Since you were able to access the GridViewRow using gvRow, you can access StudentID and Coursenumber from row cells 1 and 2 as follows

var studentID = grvRow.Cells[0].Text;
var courseNumber = grvRow.Cells[1].Text;

An other example to resolve your problem is:

<asp:GridView ID="gvStudent" runat="server" AutoGenerateColumns="False" DataSourceID="StudentDataSource">
        <Columns>
            <asp:BoundField DataField="Serial" HeaderText="Serial" SortExpression="Serial" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
            <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
            <asp:TemplateField HeaderText="Verified" SortExpression="Verified">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Verified") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Verified") %>' 
                        AutoPostBack="true" OnCheckedChanged="chkVerified_CheckedChanged" />
                    <asp:HiddenField ID="hddSerial" runat="server" Value='<%# Bind("Serial") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:Label ID="lblResult" runat="server" Text="You have validate: N/a"></asp:Label>

    <asp:ObjectDataSource ID="StudentDataSource" runat="server" SelectMethod="Get" TypeName="WebFormApp._32846281.Models.Student"></asp:ObjectDataSource>

in your code behind:

 protected void chkVerified_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;
        GridViewRow grvRow = (GridViewRow)chk.NamingContainer;

        HiddenField hf = grvRow.FindControl("hddSerial") as HiddenField;

        lblResult.Text = string.Format("You have validate:{0}", hf.Value);

    }

That resolve this problem: if you reorder colums, with first the method, you must change code behind

In my solution if you reorder columns the code behind is same.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!