我正在寻找是否有类似于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中是否有任何这样的功能来添加一个类对象以及在Button上点击检索类对象?
修改
我按照第一条评论中的说法尝试了以下内容:
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>
答案 0 :(得分:0)
试试这个
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
}