上面的“第二:我想通过"删除"在DataGrid上删除一条记录,然后输入数据后再按"更新"保存到数据库;”更正如下:“第二:我想通过"删除"在DataGrid上删除一条记录,然后再按"更新"保存到数据库;”
解决方案 »
- c#中如何查找arraylist中是否包含某个字符
- winform 开发类似于手机风格的界面
- c#向dll传递结构体指针的问题
- linq基础问题
- 怎么弹出一个类似qq上线提示那样的tip啊
- c#控制word文件生成pdf格式的文件
- C#:怎么开发像bindingNavigator一样功能的控件?
- 请教大神们~如图所示,如何将gridview中每一行及checkbox勾选值存入另一个数据库?最好能有示例代码~
- 如何控制datagridview单元格的输入
- 在c#中怎样在自己做的菜单的菜单项的左边加上图标
- 为什么电脑必需连接上网线才能接连数据库?
- 我想学C#,请问什么书比较好,最近比较忙,只有今天有空去书店,急,在线等
int rowNum = dataGrid1.CurrentCell.RowNumber;
object QSMValue = dataGrid1[rowNum, 2];
object ASMValue = dataGrid1[rowNum, 3];
object CNValue = dataGrid1[rowNum, 1];
string QSM = string.Format("{0}", QSMValue);
string ASM = string.Format("{0}", ASMValue);
string CN = string.Format("{0}", CNValue);
不论是删除后的更新,还是添加后的更新都是sqlDataAdapter.Update(tempDS.tempDT)
{
SqlConnection thisConnection = new SqlConnection(
@"server = CELERON\SQL2000;"+
"integrated Security = sspi;"+
"Database = Northwind");
string SQL = "SELECT * FROM Employees "; da = new SqlDataAdapter(SQL,thisConnection); sqlCb = new SqlCommandBuilder(da);
da.Fill(dataSet1,"Employees"); dataGrid1.SetDataBinding(dataSet1,"Employees"); } private void button1_Click(object sender, System.EventArgs e)
{
da.Update(dataSet1,"Employees");
}上面button1_Click 代码可以做你想做的
谢谢你及楼上各位的回答。我现在的目的是在dataGrid上给用户一个交互的界面。比如,用户按下了"增加"按钮,那么就要求在dataGrid上新增一个空白的记录让用户自己来填;
如果用户按了"删除"按钮,那么,自动地在dataGrid下删除他选中的当前行。
绑定的datagrid 在用户输入数据的时候,会在当前行的下行自动添加一个空行。
2> del
DataTable(DataGrid.CurrentRowIndex).Delete()
DataTable.Table.AcceptChanges()
HNU(為楚有材,於我為盛!)
回答的不是很好吗,如果是我,也这么写,
da = new SqlDataAdapter(SQL,thisConnection);是什么意思?而且在更新时并没有真正更新到数据库。
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string deletecmd="delete from table where dept_id=@dept_id";
SqlConnection myConnection =new SqlConnection(ConfigurationSettings.AppSettings["sqlconn"] ) ;
SqlCommand myCommand=new SqlCommand(deletecmd,myConnection);
myCommand.Parameters.Add(new SqlParameter("@dept_id",SqlDbType.VarChar ,50));
myCommand.Parameters["@dept_id"].Value=DataGrid1.DataKeys[e.Item.ItemIndex];
myConnection.Open();
myCommand.ExecuteNonQuery();
MyDataBind();
} private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
DataGrid1.EditItemIndex=e.Item.ItemIndex;
MyDataBind();
}
catch(System.Exception errr)
{
Response.Write(errr.ToString() );
}
} private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
DataGrid1.EditItemIndex=-1;
MyDataBind();
}
catch(System.Exception err)
{
Response.Write(err.ToString() );
}
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
string updateCmd = "update table set dept_name=@dept_name where dept_id=@dept_id";
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["sqlconn"]);
SqlCommand myCommand = new SqlCommand(updateCmd, myConnection); myCommand.Parameters.Add(new SqlParameter("@dept_name", SqlDbType.VarChar, 50));
myCommand.Parameters["@dept_name"].Value=((TextBox)e.Item.FindControl("edit_dept_name")).Text;
myCommand.Parameters.Add(new SqlParameter("@dept_id", SqlDbType.VarChar, 50));
myCommand.Parameters["@dept_id"].Value =DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString() ;
myConnection.Open(); try
{
myCommand.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException err2)
{
Response.Write(err2.ToString() );
}
myConnection.Close();
//重新绑定数据
MyDataBind();
//恢复到正常模式
DataGrid1_CancelCommand(source,e);
}
catch (System.Exception err)
{
Response.Write(err.ToString() );
}
}
但更新始终不成功。也没有出错信息,就是没有保存到数据库中。请问是怎么回事?我在保存按钮中的代码是:
dataSet11.AcceptChanges();
SqlDataAdapter1.Update(dataSet11,"authors");
首先取得数据,放到DataGrid里System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=110");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"student");然后绑定数据集和DataGrid
DataGrid.SetDataBinding(dt,"student");
如果需要,可以绑定TextBox来作录入,而用DataGrid显示
this.textBox16.DataBindings.Add("Text",dt,"student.stuno");
然后进行数据的操作如:
增加:
this.BindingContext[dt,"student"].AddNew();
删除:
this.BindingContext[dt,"student"].RemoveAt(this.BindingContext[dt,"student"].Position);
最后把结果写回数据库:
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name) VALUES (@stuno, @name)";
this.sqlInsertCommand1.Connection = this.conn;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE student SET stuno = @stuno, name = @name WHERE (stuno = @Original_stuno)";
this.sqlUpdateCommand1.Connection = this.conn;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM student WHERE (stuno = @Original_stuno)";
this.sqlDeleteCommand1.Connection = this.conn;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));
this.sqlDa.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDa.InsertCommand = this.sqlInsertCommand1;
this.sqlDa.UpdateCommand = this.sqlUpdateCommand1;
try
{
sqlDa.Update(dt.GetChanges,"student");
return true;
}
catch(System.Data.SqlClient.SqlException ex)
{
return false;
}
finally
{
conn.Close();
}
我也想要份这功能的代码,我的邮箱,谢谢!