【问题标题】:How to delete a row in a gridview without a SqlDataSource in c#?如何在c#中删除没有SqlDataSource的gridview中的一行?
【发布时间】:2023-03-05 18:00:01
【问题描述】:

我的作业是在 ASP.NET 中,我的教授希望我从不使用 SqlDataSource 的 gridview 中删除一行。这可能吗?因为我认为我的教授想要让我失望只是因为我问了一个问题而他无法回答。

【问题讨论】:

  • NitinJS 向您展示了一种解决方案,实际上您可以在网格视图中删除一行而无需任何回发:)
  • 你使用什么样的数据源?
  • 男孩!你的教授确实问了一个棘手的问题。 :) 顺便说一句,你问你的教授什么?

标签: c# asp.net gridview row


【解决方案1】:

是的,您可以从不使用 sqldatasource 的 gridview 中删除一行。您所要做的就是从源中删除该行(无论源是什么...),即绑定到您的网格视图。

这里是问题的示例代码:

public static DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
            dt.Columns.Add(new DataColumn("Column1", typeof(string)));
            dt.Columns.Add(new DataColumn("Column2", typeof(string)));         
            dr = dt.NewRow();
            dr["RowNumber"] = 1;
            dr["Column1"] = "column1cell";
            dr["Column2"] = "column2cell";
            dt.Rows.Add(dr);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (dt.Rows.Count > 0)
        {
            dt.Rows.RemoveAt(0);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }

不是最好的代码,但如果你的教授希望你这样做,你就在这里。 希望这对你有帮助...

【讨论】:

    【解决方案2】:

    我你只是想删除行找到行索引并简单地调用方法

    datagridview.rows.removeat(rowindex);
    

    【讨论】:

      【解决方案3】:

      有一种更好的方法,无需重新绑定 Gridview 并强制调用 SqlDataSource。

      使用ViewState。

      当您加载 Gridview 时,将“数据”保存到 ViewState 变量中。

      即:

      //ok let's load the gridview with data as normal and display it
      //'sdsClasses' is the SQL data source
      gvStudents.DataSourceID = "sdsClasses";
      gvStudents.DataSource = null; // Null out the source, as we have a SourceID instead
      gvStudents.DataBind(); //load the gridview and display it
      
      //save the data in a viewstate for later use 
      DataView dvClasses = (DataView)sdsClasses.Select(DataSourceSelectArguments.Empty);
      DataTable dt = new DataTable();
      if (dv != null)
      {
          dt = dvClasses.ToTable();
          ViewState["gv"] = dt;
      }
      

      所以现在,当 Gridview 加载时,您可以将其在内存中用作 ViewState 的数据。

      如果您需要删除一行,请执行此操作...

      在我的示例中,我使用搜索功能根据下拉列表控件中的 SelectValue 查找要删除的行。您必须使用类似的方法来确定要删除的行。如果您想删除最后一行,请在 DataTable 上逐行执行 ForEach,直到到达最后一行并删除!

          //Load the dataview that was already saved in the ViewState
          DataTable dt = (DataTable)ViewState["gv"];
      
          //find the student in the datatable, row by row
          bool found = false;
          bool wsAtt = false; //flag to indicate if the student is already in the roll or not saved yet (ie: sdsClasses recordset)
          foreach (DataRow dr in dt.Rows)
          {
              //compare studentID in the datatable with the selected value of the student to delete
              //check that the field has TECNQ studentIDs otherwise use the 2nd cell in the row
              if (dr[0].ToString().Contains("NQ"))
                  found = (found || dr[0].ToString() == ddlRemoveStudents.SelectedValue);
              else
              {
                  found = (found || dr[1].ToString() == ddlRemoveStudents.SelectedValue);
                  wsAtt = true;
              }
      
              //he should!
              if (found)
              {
                  //remove the row to the datatable
                  dt.Rows.Remove(dr);
      
                  //Bind the grid view to the datatable and refresh
                  gvStudents.DataSource = dt;
                  gvStudents.DataSourceID = null; // Null out the id, we have a source
                  gvStudents.DataBind();
      
                  //update the viewstate with the new amount of rows
                  ViewState["gv"] = dt;
              }
          }
      

      因此,您可以看到,使用 ViewState 作为 SqlDataSource 的替代品,您可以随意操作 Gridview,而无需再次调用原始 SqlDataSource,除非是第一次获取数据。

      并告诉你的教授他是一只傲慢的猪。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-30
        • 2010-10-10
        • 1970-01-01
        • 1970-01-01
        • 2017-09-15
        • 1970-01-01
        • 2014-04-23
        • 1970-01-01
        相关资源
        最近更新 更多