C# 从gridview中删除一行
我试图在rowdataBound()事件上从gridview中删除一行,但get过程或函数delete_行指定的参数太多。 下面是代码C# 从gridview中删除一行,c#,asp.net,C#,Asp.net,我试图在rowdataBound()事件上从gridview中删除一行,但get过程或函数delete_行指定的参数太多。 下面是代码 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if ((e.Row != null) && (e.Row.RowType == DataControlRowType.DataRow)) { for (in
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row != null) && (e.Row.RowType == DataControlRowType.DataRow))
{
for (int i = 0; i < GridView1.Rows.Count; i++ )
{
GridView1.Rows[i].Attributes["style"] += "cursor: pointer; cursor: hand;";
if (GridView1.DataKeys[i].Values[1].ToString() != "broken")
GridView1.Rows[i].Attributes["onclick"] =
"window.open('" + GridView1.DataKeys[i].Values[0].ToString() + "','open_window', 'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480, left=0, top=0')";
else
{
GridView1.DeleteRow(i);
}
}
}
这是因为您的HiddenField未绑定到SqlDataSource,所以它会传递空值或null值以删除行过程。由于将SQLDataSource直接绑定到datagridview,因此需要使用datagridview的选定行值绑定HideenField。这应该可以帮助您尝试一些选项,但这是您的基本问题。过程或函数delete#u row指定的参数太多。deleterow函数是什么样子的?@nikhil:没有必要在标题中放置“asp.NET C#”,既然你在标签里有这个,是我还是从数据库中删除一个数据绑定事件似乎是个坏主意?
<asp:HiddenField ID="hiddenField1" runat="server" Value="" />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:User42ConnectionString %>"
SelectCommand="lsp_show_by_letter" onselecting="SqlDataSource1_Selecting"
SelectCommandType="StoredProcedure" DeleteCommand="delete_row"
DeleteCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="hiddenField1" DefaultValue=" "
Name="letter" PropertyName="Value" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:ControlParameter Name="link_Id" ControlID="hiddenField1" PropertyName="Value" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound"
DataKeyNames="link_url,link_description,link_id">
<Columns>
<asp:BoundField DataField="link_display_string"
HeaderText="link_display_string" SortExpression="link_display_string" />
<asp:BoundField DataField="link_url"Visible="False" />
<asp:BoundField DataField="link_description" Visible="False" />
<asp:BoundField DataField="link_id" ReadOnly="true" Visible="False" />
</Columns>
</asp:GridView>
`ALTER PROCEDURE dbo.delete_row @link_Id int AS BEGIN DELETE FROM [links] WHERE ([link_id] = @link_Id) END`