【问题标题】:update the gridview using onrowupdating method [closed]使用 onrowupdating 方法更新 gridview [关闭]
【发布时间】:2014-10-25 22:47:39
【问题描述】:

我从 msdn 复制了大部分部分。点击更新什么都不做

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            DataTable dt = (DataTable)Session["hospital"];
            GridViewRow row = GridView1.Rows[e.RowIndex];//getting current index
            dt.Rows[row.DataItemIndex]["NAME"] = ((TextBox)(row.Cells[0].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["phone_no."] = ((TextBox)(row.Cells[1].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["ADDRESS"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["HOS_ID"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
            GridView1.EditIndex = -1;
            gvbind(GridView1, "select * from HOSPITAL"); //
        }
        catch(OracleException ex)
        {
            lblresult.Text = ex.Message.ToString();
        }
        catch (NullReferenceException nx)
        {
            lblresult.Text = nx.Message.ToString();
        }
    }     

Jere 是将gridview 绑定到datasource 的函数。

 public void gvbind(GridView g, string sql)
{
    try
    {
        conn.Open();

        OracleCommand cmd = new OracleCommand();
        cmd.Connection = conn;
        cmd.CommandText = sql;
        cmd.CommandType = CommandType.Text;

        //   cmd.ExecuteNonQuery();
        OracleDataAdapter da = new OracleDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        Session["hospital"] = ds.Tables[0];

        if (ds.Tables[0].Rows.Count > 0)
        {
            g.DataSource = ds;
            g.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            g.DataSource = ds;
            g.DataBind();
            int columncount = g.Rows[0].Cells.Count;
            g.Rows[0].Cells.Clear();
            g.Rows[0].Cells.Add(new TableCell());
            g.Rows[0].Cells[0].ColumnSpan = columncount;
            g.Rows[0].Cells[0].Text = "No Records Found";
        }

        conn.Close();
        conn.Dispose();
    }


}    

这是 asp.net 代码

<asp:GridView ID="GridView1" runat="server" DataKeyNames="HOS_ID" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" OnPageIndexChanging="GridView1_PageIndexChanging" 
OnRowUpdating="GridView1_RowUpdating" OnRowEditing="GridView1_RowEditing"
OnRowCancelingEdit="GridView1_RowCancelingEdit">
<Columns>
<asp:BoundField DataField="NAME" HeaderText="name" />
<asp:BoundField DataField="phone_no." HeaderText="Phone_no." />
<asp:BoundField DataField="ADDRESS" HeaderText="address" />
<asp:BoundField DataField="HOS_ID" HeaderText="hos_id"/>
<asp:CommandField ShowEditButton="true" />
<asp:CommandField ShowDeleteButton="true" />
</Columns>
</asp:GridView>

【问题讨论】:

  • 很多相关代码缺失。 “gvbind”有什么作用?同样,当您使用新的详细信息填充 DataTable 时,您永远不会将其传递回数据库,可能是使用 DataAdapter。
  • @akton 我已经用“gvbind”函数编辑了这个问题。我将为表格使用多个gridviews”所以我创建了一个用于绑定的通用函数。我认为绑定函数正在将值传回. 请提出修改建议。

标签: c# asp.net database gridview


【解决方案1】:

问题似乎是代码用GridView1_RowUpdating 中已编辑行中的值填充DataTable,但在调用gvbind 时将其丢弃。存储在Session["hospital"] 中的DataTable 是上次更新网格时从数据库加载的数据的副本。更改它的值只会更改内存中的副本,而不是基础表。

相反,GridView1_RowUpdating 应该:

  1. 打开与数据库的连接。
  2. 使用DataAdapter 填充DataTable
  3. DataTable 中查找与网格中更新的行相对应的行。
  4. 将新值复制到DataTable 中的相应行和单元格中。
  5. 使用DataTable 更新网格。这包括其他人在此页面之外所做的更改。
  6. 将更改写回数据库。
  7. 关闭并处理连接和相关对象。

【讨论】:

  • 正如你所说,我使用 dataAdapter 来传递新值,代码现在似乎可以工作了。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-05
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
相关资源
最近更新 更多