Similar to DataGrid.SelectedItem for ASP.Net GridView
本文关键字:ASP Net GridView for SelectedItem to DataGrid Similar | 更新日期: 2023-09-27 18:36:05
我正在寻找是否有类似于 DataGrid.SelectedItem 的功能 ASP.Net GridView。
情况是,在 WPF 中,单击按钮时,我正在使用 DataGrid.Items.Add(class_object)
添加一个类对象在 WPF DataGrid 中,如果我单击任何 datagrid 行中的按钮,则可以使用以下代码来获取类对象:
DataGrid dg = sender as DataGrid;
MyClass editedrow = (MyClass)dg.SelectedItem;
我的问题是,网格视图中是否有任何此类功能 ASP.Net 可以添加类对象以及在单击按钮时检索类对象?
编辑:
我尝试了以下方法,如第一条评论中所述:
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
}