简体   繁体   English

将更新的Gridview行保存到我的数据库表

[英]Saving the Updated Gridview row to my Database Table

I've added an edit/update option to a Gridview of bound data from a table in my database TextBooks. 我在数据库TextBooks中的表中为绑定数据的Gridview添加了一个编辑/更新选项。 I'm allowing this in case something changes down the road the admin can then edit/update the information. 我允许这样做,以防管理员可以随后更改信息。 Currently I am able to click edit type in the information, and then it binds it to the current Grid on page, but doesn't save it to the database and upon leaving the page and returning to it, it is reverted back to the old value. 目前我可以单击信息中的编辑类型,然后将其绑定到页面上的当前网格,但不将其保存到数据库,并在离开页面并返回到它时,它将恢复为旧值。 After following the code to the BindGrid() method i believe I've gone wrong somewhere but cannot fully grasp what it is I'm missing. 在遵循BindGrid()方法的代码后,我相信我在某个地方出了问题但却无法完全掌握它是什么,我错过了。

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            string query = "select  * from textBooks   ";
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HUTDMSConnectionString"].ToString()))
            using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))


                adapter.Fill(dt);
            ViewState["allBooks"] = dt;
            this.BindGrid();
        }
    }

    protected void BindGrid()
    {
        GridView1.DataSource = ViewState["allBooks"] as DataTable;
        GridView1.DataBind();
    }

    protected void OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        this.BindGrid();
    }

    protected void OnUpdate(object sender, EventArgs e)
    {
        GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;

        string Ancillary = (row.Cells[3].Controls[0] as TextBox).Text;
        string BookActive = (row.Cells[4].Controls[0] as TextBox).Text;          
        string InactiveDate = (row.Cells[6].Controls[0] as TextBox).Text;
        string Author = (row.Cells[7].Controls[0] as TextBox).Text;
        string Publisher = (row.Cells[8].Controls[0] as TextBox).Text;
        string EditionAndDate = (row.Cells[9].Controls[0] as TextBox).Text;
        string Imprint = (row.Cells[10].Controls[0] as TextBox).Text;
        string eISBN = (row.Cells[13].Controls[0] as TextBox).Text;
        string ebookAvailable = (row.Cells[14].Controls[0] as TextBox).Text;
        string Notes = (row.Cells[15].Controls[0] as TextBox).Text;

        DataTable dt = ViewState["allBooks"] as DataTable;

        dt.Rows[row.RowIndex]["Ancillary"] = Ancillary;
        dt.Rows[row.RowIndex]["BookActive"] = BookActive;
        dt.Rows[row.RowIndex]["InactiveDate"] = InactiveDate;
        dt.Rows[row.RowIndex]["Author"] = Author;
        dt.Rows[row.RowIndex]["Publisher"] = Publisher;
        dt.Rows[row.RowIndex]["EditionAndDate"] = EditionAndDate;
        dt.Rows[row.RowIndex]["Imprint"] = Imprint;
        dt.Rows[row.RowIndex]["eISBN"] = eISBN;
        dt.Rows[row.RowIndex]["ebookAvailable"] = ebookAvailable;
        dt.Rows[row.RowIndex]["Notes"] = Notes;


        ViewState["allBooks"] = dt;
        GridView1.EditIndex = -1;
        this.BindGrid();
    }

So what you are currently doing is pulling data from a database, storing it in a gridview, then editing the data in the GridView. 所以你目前正在做的是从数据库中提取数据,将其存储在gridview中,然后在GridView中编辑数据。 Once you call that databind command, that data becomes independent from the Database. 一旦调用该databind命令,该数据就会独立于数据库。 In order to save this data to the database, I would recommend using SqlDataAdapter's Update method in the OnUpdate method. 为了将这些数据保存到数据库,我建议在OnUpdate方法中使用SqlDataAdapter的Update方法。 This will allow you to store the update values back to a datarow and save it to the database while following a similar strategy as when you fill the GridView. 这将允许您将更新值存储回数据行并将其保存到数据库,同时遵循与填充GridView时类似的策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM