如何在datagrid中实现数据的增、删和修改

bardi 2003-07-18 02:30:36
在winform下,实现数据的上述操作,数据库采用SQL Server。
请大家给个例子吧,谢谢!
...全文
77 9 打赏 收藏 举报
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
孟子E章 2003-07-18
  • 打赏
  • 举报
回复
http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=8ADE535F-AD40-4DE3-A962-A64B4FAF12C4
雪狼1234567 2003-07-18
  • 打赏
  • 举报
回复
首先取得数据,放到DataGrid里

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=194910");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from customers",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"customers");

然后绑定数据集和DataGrid
DataGrid.SetDataBinding(dt,"customers");
然后进行数据的操作如:
增加:
this.BindingContext[dt,"customers"].AddNew();
删除:
this.BindingContext[dt,"customers"].RemoveAt(this.BindingContext[dt,"customers"].Position);
最后把结果写回数据库:


//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name, sex, address) VALUES (@stuno, @name, @sex, @addr" +
"ess); SELECT stuno, name, sex, address FROM student WHERE (stuno = @stuno)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.Int, 4, "stuno"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.Int, 4, "name"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sex", System.Data.SqlDbType.Int, 4, "sex"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@address", System.Data.SqlDbType.Int, 4, "address"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = @"UPDATE student SET stuno = @stuno, name = @name, sex = @sex, address = @address WHERE (stuno = @Original_stuno) AND (address = @Original_address OR @Original_address IS NULL AND address IS NULL) AND (name = @Original_name OR @Original_name IS NULL AND name IS NULL) AND (sex = @Original_sex OR @Original_sex IS NULL AND sex IS NULL); SELECT stuno, name, sex, address FROM student WHERE (stuno = @stuno)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.Int, 4, "stuno"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.Int, 4, "name"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sex", System.Data.SqlDbType.Int, 4, "sex"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@address", System.Data.SqlDbType.Int, 4, "address"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.Int, 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.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));

try
{
sqlDa.Update(dt.GetChanges,"customers");
return true;
}
catch(System.Data.SqlClient.SqlException ex)
{
myTran.Rollback();
return false;
}
finally
{
conn.Close();
}
tgm78 2003-07-18
  • 打赏
  • 举报
回复
public void EditRecord(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=(int)e.Item.ItemIndex;

BindDataGrid();
}
public void CancelEdit(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=-1;
BindDataGrid();
}
public void UpdateRecord(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string updateCmd="update LiangHua set AffectFactor=@AffcetFactor,RiskFactor=@RiskFactor,ExpertID=@ExpertID,JinDu=@JinDu,gene1=@gene1,YouCost=@YouCost,gene2=@gene2,WuCost=@WuCost,gene3=@gene3,Quality=@Quality,gene4=@gene4,Company=@Company,gene5=@gene5 where id=@id";

SqlCommand MC=new SqlCommand(updateCmd,myConnection);
//MC.Connection.Open();

MC.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4));
MC.Parameters.Add(new SqlParameter("@AffectFactor",SqlDbType.NVarChar,50));
MC.Parameters.Add(new SqlParameter("@RiskFactor",SqlDbType.NVarChar,50));
MC.Parameters.Add(new SqlParameter("@ExpertID",SqlDbType.Int,4));
MC.Parameters.Add(new SqlParameter("@JinDu",SqlDbType.Int,4));
MC.Parameters.Add(new SqlParameter("@gene1",SqlDbType.Int,4));
MC.Parameters.Add(new SqlParameter("@YouCost",SqlDbType.Int,4));
MC.Parameters.Add(new SqlParameter("@gene2",SqlDbType.Int,4));
MC.Parameters.Add(new SqlParameter("@WuCost",SqlDbType.Int,4));
MC.Parameters.Add(new SqlParameter("@gene3",SqlDbType.Int,4));
MC.Parameters.Add(new SqlParameter("@Quality",SqlDbType.Int,4));
MC.Parameters.Add(new SqlParameter("@gene4",SqlDbType.Int,4));
MC.Parameters.Add(new SqlParameter("@Company",SqlDbType.Int,4));
MC.Parameters.Add(new SqlParameter("@gene5",SqlDbType.Int,4));

//下面表达式暂时没有问题
MC.Parameters["@id"].Value=DataGrid1.DataKeys[(int)e.Item.ItemIndex];
//Response.Write(MC.Parameters["@id"].Value+" ");
string[] cols={"@AffectFactor","@RiskFactor","@ExpertID","@JinDu","@gene1","@YouCost","@gene2","@WuCost","@gene3","@Quality","@gene4","@Company","@gene5"};
int numcols=e.Item.Cells.Count;
for(int i=1;i<numcols-2;i++)
{ //下面的语句没有起作用

//string col=((TextBox)e.Item.Cells[i].Controls[0]).Text;
string col=txtzhi.Text;
if(col=="")
{
Message.InnerHtml="<h2>错误:内容等不能为空</h2>";
Message.Style["color"]="red";
}
MC.Parameters[cols[0]].Value=col;
Response.Write(MC.Parameters[cols[0]].Value+" ");
}

MC.Connection.Open();
try
{
MC.ExecuteNonQuery();
Response.Write("修改成功");
DataGrid1.EditItemIndex=-1;


}
catch(SqlException)
{
Response.Write("修改失败");

}
MC.Connection.Close();
BindDataGrid();

}
bardi 2003-07-18
  • 打赏
  • 举报
回复
楼上的。
我的意思是通过添加、修改、删除datagrid的数据,然后反映到数据库中啊。
reformer 2003-07-18
  • 打赏
  • 举报
回复
修改DataGrid的数据源就可以了。
ybtaylor 2003-07-18
  • 打赏
  • 举报
回复
能不能给我也发一个?

ybtaylor@hotmail.com
bardi 2003-07-18
  • 打赏
  • 举报
回复
liboemail@163.com
谢谢给我发个吧!
jrh9981 2003-07-18
  • 打赏
  • 举报
回复
给我也一个我的邮箱是jrh8891@sina.com
3no 2003-07-18
  • 打赏
  • 举报
回复
要不给我个信箱,我给你发一个吧
相关推荐
发帖
C#

10.8w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2003-07-18 02:30
社区公告

让您成为最强悍的C#开发者