【问题标题】:Deleting a row in grid view,in which data and column names are given from code behind删除网格视图中的一行,其中数据和列名由后面的代码给出
【发布时间】:2014-03-30 13:31:35
【问题描述】:

我需要从 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();
}

行删除:

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

aspx:

<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>

【问题讨论】:

  • 你遇到异常了吗?
  • 您已声明 onrowdeleting="grd_RowDeleting" 并尝试在 onrowcommand="grd_RowCommand" 中处理删除

标签: c# asp.net


【解决方案1】:

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

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.
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2016-12-19
    • 2014-12-17
    • 1970-01-01
    相关资源
    最近更新 更多