【发布时间】: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