簡體   English   中英

如何刪除 gridview 中沒有 c# 中的 SqlDataSource 的行?

[英]How to delete a row in a gridview without a SqlDataSource in c#?

我的作業在 ASP.NET 中,我的教授希望我從不使用SqlDataSourcegridview中刪除一行。 這可能嗎? 因為我認為我的教授想要讓我失望只是因為我問了一個問題而他無法回答。

有一種更好的方法,無需重新綁定Gridview並強制調用SqlDataSource

使用ViewState

當您加載 Gridview 時,將“數據”保存到 ViewState 變量中。

IE:

//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 加載時,您將在 memory 中使用的數據作為 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,除非是第一次獲取數據。

告訴你的教授他是只傲慢的豬。

我你只想刪除行找到行索引並簡單地調用方法

datagridview.rows.removeat(rowindex);

是的,您可以從 gridview 中刪除不使用 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();
        }
    }

不是最好的代碼,但是如果您的教授希望您這樣做,那么您就在這里。 希望這可以幫助你...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM