【发布时间】:2015-02-28 13:32:40
【问题描述】:
我正在寻找 ASP.Net GridView 是否有类似于 DataGrid.SelectedItem 的功能。
情况是,在 WPF 中,单击按钮时,我正在使用 DataGrid.Items.Add(class_object) 添加一个类对象
在 WPF DataGrid 中,如果我单击任何数据网格行中的按钮,我可以使用以下代码来获取类对象:
DataGrid dg = sender as DataGrid;
MyClass editedrow = (MyClass)dg.SelectedItem;
我的问题是,ASP.Net gridview 中是否有任何此类功能可以添加类对象以及在按钮单击时检索类对象?
编辑:
我尝试了以下第一条评论中所说的:
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.SelectedRow。 -
我试过了,但我收到了一个错误,我已将其添加为问题中的编辑
-
你会得到
GridViewRow而不是你的自定义类。 -
不,您不会向 GridView 添加类对象,而是将类数组或列表分配给 GridView 数据源。
标签: c# asp.net wpf gridview datagrid