C# 删除网格视图中的行,其中的数据和列名由代码隐藏提供

C# 删除网格视图中的行,其中的数据和列名由代码隐藏提供,c#,asp.net,C#,Asp.net,我需要从GridView中删除一行,我使用了不同的方法,所有操作都是在代码隐藏中完成的。当用户单击“删除链接”按钮时,我尝试删除一行,但该行未被删除,我尝试在“行删除事件”中删除,但收到堆栈溢出异常 添加列名: DataTable dtToGrid = new DataTable(); DataColumn dc = dtToGrid.Columns.Add("S.No", typeof(int)); dc.AutoIncrement = true; dc.AutoIncrementSeed =

我需要从GridView中删除一行,我使用了不同的方法,所有操作都是在代码隐藏中完成的。当用户单击“删除链接”按钮时,我尝试删除一行,但该行未被删除,我尝试在“行删除事件”中删除,但收到堆栈溢出异常

添加列名:

DataTable dtToGrid = new DataTable();
DataColumn dc = dtToGrid.Columns.Add("S.No", typeof(int));
dc.AutoIncrement = true;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep = 1;
dtToGrid.Columns.Add("ItemName", typeof(string));
dtToGrid.Columns.Add("Quantity", typeof(string));
dtToGrid.Columns.Add("Unit", typeof(string));
dtToGrid.Columns.Add("RatePer Unit", typeof(string));
dtToGrid.Columns.Add("Amount", typeof(string));
Session["dtToGrid"] = dtToGrid;   
添加行值:

double sum = 0;

grd.Visible = true;

DataTable dtToGrid = (DataTable)Session["dtToGrid"];

DataRow drToGrid = dtToGrid.NewRow();

drToGrid["ItemName"] =ddlProduct.SelectedItem.Value;
drToGrid["Quantity"] = txtQty.Text;
drToGrid["Unit"]=Unit.Value;
drToGrid["RatePer Unit"]=costperunit.Value;
drToGrid["Amount"]=txtAmount.Text;                                  

dtToGrid.Rows.Add(drToGrid);

grd.DataSource = dtToGrid;
grd.DataBind();
for (int x = 0; x < grd.Rows.Count; x++)
{
    sum += Convert.ToDouble(grd.Rows[x].Cells[5].Text);
}
if (totalAmountINcludingTax.Text == " ")
{
    sum = (sum * Convert.ToDouble(Tax.Value)) / 100 + sum;
    totalAmountINcludingTax.Text = sum.ToString();
}
else
{
    sum = (sum * Convert.ToDouble(Tax.Value)) / 100 +Convert.ToDouble( totalAmountINcludingTax.Text);
    totalAmountINcludingTax.Text = sum.ToString();
}
Aspx:


为什么不执行与删除行上添加行相同的逻辑呢。 基本上找到要删除的数据行,从数据表中删除并重新绑定

protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
        int Prod_Id = Convert.ToInt32(e.CommandArgument);        

        DataTable dtToGrid = (DataTable)Session["dtToGrid"];

        DataRow rowToBeDeleted = dtToGrid.Rows.Where(r=>r["Prod_Id"] == Prod_Id.ToString());

        if (rowToBeDeleted != null)
        {
          dtToGrid.Rows.Remove(rowToBeDeleted);

          grd.DataSource = dtToGrid;
          grd.DataBind();

          // do your summation logic.
        }
    }
}

您是否遇到异常?您已声明onrowdeleding=“grd\u rowdeleding”并试图在onrowcommand=“grd\u rowdeleding”中处理删除操作
<asp:GridView ID="grd" runat="server" AutoGenerateColumns="true" 
    onrowdeleting="grd_RowDeleting" onrowcommand="grd_RowCommand">

    <Columns>
        <asp:CommandField ShowDeleteButton="True" />

        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lbtnDelete"  runat="server" CommandName="delete" title="Delete"
                    OnClientClick="return confirm('Do you Want to Delete this Record?');">
                </asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
        int Prod_Id = Convert.ToInt32(e.CommandArgument);        

        DataTable dtToGrid = (DataTable)Session["dtToGrid"];

        DataRow rowToBeDeleted = dtToGrid.Rows.Where(r=>r["Prod_Id"] == Prod_Id.ToString());

        if (rowToBeDeleted != null)
        {
          dtToGrid.Rows.Remove(rowToBeDeleted);

          grd.DataSource = dtToGrid;
          grd.DataBind();

          // do your summation logic.
        }
    }
}