我有一个 Gridview,我在其中加入两个表,引入要显示的数据
表 1 sid, schedule, stime, splace, stourid
表 2 tourid, tourname
基本上,他们加入了 stourid to tourid。这样我就可以在网格中显示比赛名称。
现在我想编辑这个 GridView,我需要做什么,例如,如果用户编辑了 tourname,它会被保存到数据库中吗?我假设需要更新语句,但它如何链接到 GridView?
什么是最好和最整洁的方法?
问候,
我有一个 Gridview,我在其中加入两个表,引入要显示的数据
表 1 sid, schedule, stime, splace, stourid
表 2 tourid, tourname
基本上,他们加入了 stourid to tourid。这样我就可以在网格中显示比赛名称。
现在我想编辑这个 GridView,我需要做什么,例如,如果用户编辑了 tourname,它会被保存到数据库中吗?我假设需要更新语句,但它如何链接到 GridView?
什么是最好和最整洁的方法?
问候,
创建Store Procedure以更新两个表并在您的更新代码中调用它...
Create Proc UpdateData
@sid int,
@schedule,
@stime,
@splace,
@tourname
as
Begin
declare @tourid int,
select distinct @tourid=tourid from table1 where sid=@sid
begin try
// update tabel1
update tabel2 set tourname=@tourname where tourid =@tourid
end Try
begin Catch
end Catch
End
使用 LinqToSQL,您可以执行类似的操作(如果表之间存在 FK 关系)
DatabaseDataContext data = new DatabaseDataContext();
Table1 row = data.Table1s.Where(t =>t.ID == selectedID);
row.Table2.tourname = newName;
data.SubmitChanges();
然后,您需要重新绑定网格以显示新数据。