【问题标题】:Similar to DataGrid.SelectedItem for ASP.Net GridView类似于 ASP.Net GridView 的 DataGrid.SelectedItem
【发布时间】: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


【解决方案1】:

试试这个

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 



}

【讨论】:

  • 我可以把它转换成类对象吗?
  • gv.SelectedRow 返回 null。
  • 请为 GridVIew 发布 .aspx
  • @Nubicus 你可以像我为ItemType做的那样获取值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-06
  • 1970-01-01
相关资源
最近更新 更多