C# 如何从GridView中删除一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/592106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:30:37  来源:igfitidea点击:

How to delete a row from GridView?

c#asp.netgridviewdelete-row

提问by Kartik

I am using GridViewcontrol in asp.net2005 c#using .

GridViewasp.net2005 c# 中使用控件使用 .

How can I delete a particular row from GridView.

如何从GridView.

I have written the following code. But it's not working...

我已经编写了以下代码。但它不起作用...

DataRow dr = dtPrf_Mstr.NewRow();
dtPrf_Mstr.Rows.Add(dr);
GVGLCode.DataSource = dtPrf_Mstr;
GVGLCode.DataBind();

int iCount = GVGLCode.Rows.Count;
for (int i = 0; i <= iCount; i++)
{
    GVGLCode.DeleteRow(i);
}
GVGLCode.DataBind();

采纳答案by TheTXI

You are deleting the row from the gridview but you are then going and calling databind again which is just refreshing the gridview to the same state that the original datasource is in.

您正在从 gridview 中删除该行,但随后您将再次调用 databind,这只是将 gridview 刷新到与原始数据源所处的状态相同的状态。

Either remove it from the datasource and then databind, or databind and remove it from the gridview without redatabinding.

要么将其从数据源中删除,然后进行数据绑定,要么进行数据绑定并将其从 gridview 中删除,而无需重新绑定。

回答by Welbog

Delete the row from the table dtPrf_Mstr rather than from the grid view.

从表 dtPrf_Mstr 而不是从网格视图中删除行。

回答by Ken Browning

The default answer is to remove the item from whatever collection you're using as the GridView's DataSource.

默认答案是从您用作 GridView 的数据源的任何集合中删除该项目。

If that option is undesirable then I recommend that you use the GridView's RowDataBoundevent to selectively set the row's (e.Row) Visibleproperty to false.

如果该选项不受欢迎,那么我建议您使用 GridView 的RowDataBound事件有选择地将行的 ( e.Row)Visible属性设置为 false。

回答by atfergs

You're deleting the row from the gridview and then rebinding it to the datasource (which still contains the row). Either delete the row from the datasource, or don't rebind the gridview afterwards.

您正在从 gridview 中删除该行,然后将其重新绑定到数据源(仍包含该行)。从数据源中删除该行,或者之后不要重新绑定 gridview。

回答by JOEL LYTE

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Default3 : System.Web.UI.Page
{
   DataTable dt = new DataTable();
    DataSet Gds = new DataSet();
   // DataColumn colm1 = new DataColumn();
   //DataColumn colm2 = new DataColumn();

    protected void Page_Load(object sender, EventArgs e)
    {
        dt.Columns.Add("ExpId", typeof(int));
        dt.Columns.Add("FirstName", typeof(string));

    }


    protected void BtnLoad_Click(object sender, EventArgs e)
    {
        //   gvLoad is Grid View Id
        if (gvLoad.Rows.Count == 0)
        {
            Gds.Tables.Add(tblLoad());
        }
        else
        {
            dt = tblGridRow();
            dt.Rows.Add(tblRow());
            Gds.Tables.Add(dt);
        }
        gvLoad.DataSource = Gds;
        gvLoad.DataBind();
    }

    protected DataTable tblLoad()
    {
        dt.Rows.Add(tblRow());
        return dt;
    }
    protected DataRow tblRow()
    {
        DataRow dr;
        dr = dt.NewRow();
        dr["Exp Id"] = Convert.ToInt16(txtId.Text);
        dr["First Name"] = Convert.ToString(txtName.Text);
        return dr;
    }

    protected DataTable tblGridRow()
    {
        DataRow dr;
        for (int i = 0; i < gvLoad.Rows.Count; i++)
        {
            if (gvLoad.Rows[i].Cells[0].Text != null)
            {

                dr = dt.NewRow();
                dr["Exp Id"] = gvLoad.Rows[i].Cells[1].Text.ToString();
                dr["First Name"] = gvLoad.Rows[i].Cells[2].Text.ToString();
                dt.Rows.Add(dr);

            }

        }
        return dt;
    }

    protected void btn_Click(object sender, EventArgs e)
    {
        dt = tblGridRow();
        dt.Rows.Add(tblRow());
        Session["tab"] = dt;
        // Response.Redirect("Default.aspx");
    }

    protected void gvLoad_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        dt = tblGridRow();
        dt.Rows.RemoveAt(e.RowIndex);
        gvLoad.DataSource = dt;
        gvLoad.DataBind();
    }
}

回答by Paramita

My solution:

我的解决方案:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    myobj.myconnection();// connection created
    string mystr = "Delete table_name where water_id= '" + GridView1.DataKeys[e.RowIndex].Value + "'";// query
    sqlcmd = new SqlCommand(mystr, myobj.mycon);
    sqlcmd.ExecuteNonQuery();
    fillgrid();
}

回答by mahdi

hi how to delete from datagridview

嗨,如何从数据网格视图中删除

1.make query delete by id
2.type

1.使查询删除 id
2.type

tabletableadaptor.delete
query(datagridwiewX1.selectedrows[0].cell[0].value.tostring);