How to delete a row from GridView?

后端 未结 7 467
攒了一身酷
攒了一身酷 2020-11-29 12:14

I am using GridView control in asp.net 2005 c# using .

How can I delete a particular row from GridView.

I have written the followin

相关标签:
7条回答
  • 2020-11-29 12:36
    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();
        }
    }
    
    0 讨论(0)
  • 2020-11-29 12:40

    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();
    }
    
    0 讨论(0)
  • 2020-11-29 12:44

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

    0 讨论(0)
  • 2020-11-29 12:45

    hi how to delete from datagridview

    1.make query delete by id
    2.type

    tabletableadaptor.delete
    query(datagridwiewX1.selectedrows[0].cell[0].value.tostring);
    
    0 讨论(0)
  • 2020-11-29 12:47

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

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

    0 讨论(0)
  • 2020-11-29 12:48

    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.

    0 讨论(0)
提交回复
热议问题