单击删除按钮单击ASP.NET

时间:2016-10-31 05:27:24

标签: c# asp.net

我正在尝试删除行按钮单击删除按钮。我不能只删除选中的行,它删除了完整的网格,并且之后没有显示网格。如何修复删除所选行?

删除前:

before delete

删除后:

After delete

protected void btnDeleteOK_Click(object sender, EventArgs e)
{
    dtItems = new DataTable();
    dtItems.Columns.Add("RowId");
    dtItems.Columns.Add("ItemId");
    dtItems.Columns.Add("Percentage", typeof(decimal));
    dtItems.Columns.Add("LabDipId");
    dtItems.Columns.Add("RecipeId");

    foreach (GridViewRow gr in gdDyeNames.Rows)
    {
        TextBox txtPercentage = (TextBox)gr.FindControl("txtPercentage");
        DropDownList ddlDyeingType = ((DropDownList)gr.FindControl("ddlDyeingType"));

        DataRow dr = dtItems.NewRow();

        dr["RecipeId"] = SelectedReciptId;
        dr["ItemId"] = int.Parse(ddlDyeingType.SelectedValue.ToString());
        dr["Percentage"] = decimal.Parse(txtPercentage.Text).ToString();
        dtItems.Rows.Add(dr);
    }

    DataRow[] drItem = dtItems.Select("ItemId = " + selectedRowId);

    foreach (DataRow dr in drItem)
    {
        dtItems.Rows.Remove(dr);
    }

    gdDyeNames.DataSource = dtItems;
    gdDyeNames.DataBind();

    lblEror.Text = "";
    lblMsg.Text = "";
    mpdelete.Hide();
}

2 个答案:

答案 0 :(得分:0)

最后我开发它,那很好用

if (dtItems != null)
            {
                ImageButton imb = sender as ImageButton;
                int rowIndex = Convert.ToInt32(imb.CommandArgument);


                if (dtItems.Rows.Count > 0)
                {
                    dtItems.Rows.Remove(dtItems.Rows[rowIndex]);

                    gdDyeNames.DataSource = dtItems;
                    gdDyeNames.DataBind();

                    lblEror.Text = "";
                    lblMsg.Text = "";
                    mpdelete.Hide();

                    if (dtItems.Rows.Count == 0)
                    {
                        createIntialGrid();
                    }

答案 1 :(得分:-1)

试试这个

protected void btnDeleteOK_Click(object sender, EventArgs e)
{
    dtItems = new DataTable();
    dtItems.Columns.Add("RowId");
    dtItems.Columns.Add("ItemId");
    dtItems.Columns.Add("Percentage", typeof(decimal));
    dtItems.Columns.Add("LabDipId");
    dtItems.Columns.Add("RecipeId");

    foreach (GridViewRow gr in gdDyeNames.Rows)
    {
        TextBox txtPercentage = (TextBox)gr.FindControl("txtPercentage");
        DropDownList ddlDyeingType = ((DropDownList)gr.FindControl("ddlDyeingType"));

        DataRow dr = dtItems.NewRow();

        dr["RecipeId"] = SelectedReciptId;
        dr["ItemId"] = int.Parse(ddlDyeingType.SelectedValue.ToString());
        dr["Percentage"] = decimal.Parse(txtPercentage.Text).ToString();
        dtItems.Rows.Add(dr);
    }


        ImageButton btnDetails = (ImageButton)sender; //Change Image button to your Button used
        GridViewRow row = (GridViewRow)btnDetails.NamingContainer;
        int rowIndex = row.RowIndex; 
        DataRow dr = dtItems.Rows[rowIndex];
        dtItems.Rows.Remove(dr);
        dtItems.AcceptChanges();

    gdDyeNames.DataSource = dtItems;
    gdDyeNames.DataBind();

    lblEror.Text = "";
    lblMsg.Text = "";
    mpdelete.Hide();
}