C# 在row命令中获取selectedrow
我的gridview如下所示:C# 在row命令中获取selectedrow,c#,asp.net,gridview,buttonfield,C#,Asp.net,Gridview,Buttonfield,我的gridview如下所示: <asp:GridView ID="gdvOpinions" runat="server" Width="100%" CellPadding="4" ForeColor="#333333" GridLines="None" Height="100px" Visible="False" AutoGenerateColumns="False" onrowcomma
<asp:GridView ID="gdvOpinions" runat="server" Width="100%" CellPadding="4"
ForeColor="#333333" GridLines="None" Height="100px" Visible="False"
AutoGenerateColumns="False" onrowcommand="gdvOpinions_RowCommand" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Confirm" Text="تائید" />
<asp:ButtonField ButtonType="Button" CommandName="Delete" Text="حذف" />
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="مقاله" HeaderText="مقاله" >
<ControlStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="نظر کاربر" HeaderText="نظر کاربر" >
<ControlStyle Width="250px" />
</asp:BoundField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
返回null;
如何查找所选的行?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"
OnRowCommand = "OnRowCommand">
<Columns>
<asp:ButtonField CommandName = "ButtonField" DataTextField = "CustomerID"
ButtonType = "Button"/>
</Columns>
</asp:GridView>
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow = GridView1.Rows[index];
}
受保护的void onrow命令(对象发送方,GridViewCommandEventArgs e)
{
int index=Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow=GridView1.Rows[索引];
}
受保护的void onrow命令(对象发送方,GridViewCommandEventArgs e)
{
int index=Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow=GridView1.Rows[索引];
}
根据我对您问题的理解,如果您试图获取单击按钮的行,那么在这种情况下,这可能是正确的做法:
GridViewRow gRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
现在,这是一个控件,例如按钮
或图像按钮
。对于按钮字段
,您可能需要在这方面即兴创作
您还可以使用按钮将按钮字段
更改为简单的模板字段
希望这有帮助。根据我对您问题的理解,如果您试图获取单击按钮的行,那么在这种情况下,这可能是正确的做法:
GridViewRow gRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
现在,这是一个控件,例如按钮
或图像按钮
。对于按钮字段
,您可能需要在这方面即兴创作
您还可以使用按钮将按钮字段
更改为简单的模板字段
希望这有帮助。单击按钮字段不会触发SelectedIndexChanged
谢谢,这正是我要讨论的问题@HarveySpecter@SalehRezaei您可以使用OnRowCommand事件获取当前行..查看我编辑的ans..单击按钮字段不会触发SelectedIndexChanged
谢谢,这正是我正在谈论的问题@HarveySpecter@SalehRezaei您可以使用OnRowCommand事件获取当前行..查看我编辑的ans..单击按钮字段不会触发SelectedIndexChanged
谢谢,这正是我正在谈论的问题@HarveySpecter@SalehRezaei您可以使用OnRowCommand事件获取当前行。查看我编辑的ans。删除。NamingContainer
并重试。它返回:无法将类型“System.Web.UI.WebControl.ButtonField”转换为“System.Web.UI.WebControl.GridViewRow”将ButtonField
更改为带有按钮的模板字段
。应该可以。删除.NamingContainer
并重试。它返回:无法将类型“System.Web.UI.WebControls.ButtonField”转换为“System.Web.UI.WebControls.GridViewRow”使用按钮将ButtonField
更改为模板字段。应该可以。删除.NamingContainer
并重试。它返回:无法将类型“System.Web.UI.WebControls.ButtonField”转换为“System.Web.UI.WebControls.GridViewRow”使用按钮将ButtonField
更改为模板字段。应该这样做。
GridViewRow gRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;