类似于 DataGrid.SelectedItem for ASP.Net GridView

Similar to DataGrid.SelectedItem for ASP.Net GridView

我正在寻找 ASP.Net GridView 是否有类似于 DataGrid.SelectedItem 的功能。

情况是,在 WPF 中,单击按钮时,我使用 DataGrid.Items.Add(class_object) 添加了一个 class 对象 在 WPF DataGrid 中,如果我单击任何数据网格行中的按钮,我可以使用以下代码获取 class 对象:

DataGrid dg = sender as DataGrid;
MyClass editedrow = (MyClass)dg.SelectedItem;

我的问题是,ASP.Net gridview 中是否有这样的功能来添加 class 对象以及在单击按钮时检索 class 对象?

编辑:

我尝试了第一条评论中所说的以下方法:

GridView gv = sender as GridView;
MyClass editedrow = (MyClass)gv.SelectedRow;

显示错误 'System.Web.UI.WebControls.GridViewRow' to 'Nubicus.gui.MyClass'

编辑 2

<asp:GridView ID="dgSODetails" runat="server" AutoGenerateColumns="False" 
                                        onrowcommand="dgSODetails_RowCommand"  >
                                        <Columns>
                                            <asp:BoundField DataField="RowNum" HeaderText="RowNum" ItemStyle-Width="0" Visible="false">
                                                <ItemStyle Width="0px" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="SO_ItemType" HeaderText="Item Category" ItemStyle-Width="0"
                                                Visible="false">
                                                <ItemStyle Width="0px" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="SO_Item_Name" HeaderText="Item Name"></asp:BoundField>
                                            <asp:BoundField DataField="SO_Item_Quantity" HeaderText="Qty."></asp:BoundField>
                                            <asp:BoundField DataField="SO_Unit_Name" HeaderText="Unist"></asp:BoundField>
                                            <asp:BoundField DataField="SO_Line_Discount_Percentage" HeaderText="Disc. %"></asp:BoundField>
                                            <asp:BoundField DataField="SO_Line_Discount_Amount" HeaderText="Disc. Amt."></asp:BoundField>
                                            <asp:BoundField DataField="SO_Item_Final_Price" HeaderText="Total Amt."></asp:BoundField>
                                            <asp:TemplateField HeaderText="Actions">
                                                <ItemTemplate>
                                                    <asp:Button ID="btnEdit" runat="server" Text="Edit" OnCommand="dgSODetails_Command"
                                                        CommandName="Edit" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
                                                    <asp:Button ID="btnDelete" runat="server" Text="Delete" OnCommand="dgSODetails_Command"
                                                        CommandName="Delete" CommandArgument="<%#((GridViewRow)Container).RowIndex %>" />
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                    </asp:GridView>

试试这个

GridView gv = sender as GridView;
GridViewRow editedrow = (GridViewRow)gv.SelectedRow;


    <asp:Button ID="btnEdit" runat="server" Text="Edit" OnCommand="dgSODetails_Command"
CommandName="Edit" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>"
OnClick="btnEdit_Click" />


protected void btnEdit_Click(object sender,EventArgs e)
{
        Button btn = (Button)sender;
        GridViewRow row = (GridViewRow)btn.NamingContainer;
        int i = Convert.ToInt32(row.RowIndex);
        string SO_ItemType= row.Cells[1].Text 



}