请问DataGrid中Edit控件点击后数据库记录改变,但页面刷新后什么都没有,不知怎么回事,请指教! DataSet ds=new DataSet();dgProducts.DataSource=ds.Tables[ProductTableName];dgProducts.DataBind();新建DataSet再绑定,还没加载数据呢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Update要能过SqlCommand来更新数据库public void UpdateCommand(Object sender, DataGridCommandEventArgs e) { // Retrieve the new text int nColPositionIndex = 2; // 0-based position of the column int nColFromIndex = 3; // 0-based position of the column TextBox txtPosition = (TextBox) e.Item.Cells[nColPositionIndex].Controls[0]; TextBox txtFrom = (TextBox) e.Item.Cells[nColFromIndex].Controls[0]; // Prepare the command text String strCmd = "UPDATE Employees SET title=@sPosition, country=@sCountry WHERE employeeid=@nEmpID"; SqlConnection conn = new SqlConnection(txtConn.Text); SqlCommand cmd = new SqlCommand(strCmd, conn); SqlParameter p1 = new SqlParameter("@nEmpID", SqlDbType.Int); p1.Direction = ParameterDirection.Input; p1.Value = grid.DataKeys[e.Item.ItemIndex]; cmd.Parameters.Add(p1); SqlParameter p2 = new SqlParameter("@sPosition", SqlDbType.NVarChar, 30); p2.Direction = ParameterDirection.Input; p2.Value = txtPosition.Text; cmd.Parameters.Add(p2); SqlParameter p3 = new SqlParameter("@sCountry", SqlDbType.NVarChar, 15); p3.Direction = ParameterDirection.Input; p3.Value = txtFrom.Text; cmd.Parameters.Add(p3); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); // Reset the edit mode for the current item grid.EditItemIndex = -1; // Refresh the grid UpdateView();} private void UpdateView(){ SqlConnection conn = new SqlConnection(txtConn.Text); SqlDataAdapter da = new SqlDataAdapter(txtCommand.Text, conn); DataSet ds = new DataSet(); da.Fill(ds, "MyTable"); // Bind the data grid.DataSource = ds.Tables["MyTable"]; // Display the data grid.DataBind();} 将你的LoadGrid()函数改为下面的即可public void LoadGrid() { Connect(); SqlDataAdapter adapter=new SqlDataAdapter(strSQLSelect,objConnection); DataSet ds=new DataSet(); adapter.Fill(ds,"ProductTableName"); Disconnect(); dgProducts.DataSource=ds.Tables["ProductTableName"].DefaultView; dgProducts.DataBind(); }主要是dgProducts.DataSource=ds.Tables[ProductTableName]和adapter.Fill(ds,ProductTableName);;的表达有问题改为如上即可 相应的UpdateProduct(int ProductID,decimal Price)中的也要更改 用div生成chart图和label,label怎么显示在chart图正上方 未将对象引用设置到对象的实例。 20秒倒记时进度条拉升 AutoCompleteExtender怎么不执行???? asp.net 创建 excel 的问题 如何分别搭建web服务器和数据库服务器 使cookies随浏览器关闭而失效? 将通用进行到底。另类程序架构。 如何取到存储过程的输出参数? 以前用VB6写好的DLL,在ASP.NET中怎么样去用? 怎么控制datagrid的行的宽度? 求助:我创建asp项目的时候,提示说“web服务器运行的不是asp.net 1.1版本”
public void UpdateCommand(Object sender, DataGridCommandEventArgs e)
{
// Retrieve the new text
int nColPositionIndex = 2; // 0-based position of the column
int nColFromIndex = 3; // 0-based position of the column
TextBox txtPosition = (TextBox) e.Item.Cells[nColPositionIndex].Controls[0];
TextBox txtFrom = (TextBox) e.Item.Cells[nColFromIndex].Controls[0]; // Prepare the command text
String strCmd = "UPDATE Employees SET title=@sPosition, country=@sCountry WHERE employeeid=@nEmpID";
SqlConnection conn = new SqlConnection(txtConn.Text);
SqlCommand cmd = new SqlCommand(strCmd, conn); SqlParameter p1 = new SqlParameter("@nEmpID", SqlDbType.Int);
p1.Direction = ParameterDirection.Input;
p1.Value = grid.DataKeys[e.Item.ItemIndex];
cmd.Parameters.Add(p1); SqlParameter p2 = new SqlParameter("@sPosition", SqlDbType.NVarChar, 30);
p2.Direction = ParameterDirection.Input;
p2.Value = txtPosition.Text;
cmd.Parameters.Add(p2); SqlParameter p3 = new SqlParameter("@sCountry", SqlDbType.NVarChar, 15);
p3.Direction = ParameterDirection.Input;
p3.Value = txtFrom.Text;
cmd.Parameters.Add(p3); conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
// Reset the edit mode for the current item
grid.EditItemIndex = -1; // Refresh the grid
UpdateView();
} private void UpdateView()
{
SqlConnection conn = new SqlConnection(txtConn.Text);
SqlDataAdapter da = new SqlDataAdapter(txtCommand.Text, conn);
DataSet ds = new DataSet();
da.Fill(ds, "MyTable"); // Bind the data
grid.DataSource = ds.Tables["MyTable"]; // Display the data
grid.DataBind();
}
public void LoadGrid()
{
Connect();
SqlDataAdapter adapter=new SqlDataAdapter(strSQLSelect,objConnection);
DataSet ds=new DataSet();
adapter.Fill(ds,"ProductTableName");
Disconnect(); dgProducts.DataSource=
ds.Tables["ProductTableName"].DefaultView;
dgProducts.DataBind();
}主要是dgProducts.DataSource=ds.Tables[ProductTableName]和adapter.Fill(ds,ProductTableName);
;的表达有问题
改为如上即可 相应的UpdateProduct(int ProductID,decimal Price)中的也要更改