关于asp.net中的DataGrid数据批量更新

hflkl1314 2007-01-03 08:18:39
在asp.net中,我用DataSet获取。在DataGrid中绑定DataSet.Tables["***"].DefaultView,现在我在DataGrid修改了一些数据,我要怎么样才能批量更新数据呢,是把DataGrid中的行都取出来然后用update来更新吗?
在线等答案,也可以QQ:261766987
...全文
612 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qingqingee 2008-10-09
  • 打赏
  • 举报
回复
不错不错!
c870801 2008-06-03
  • 打赏
  • 举报
回复
我也是看不太明白啊.
vb_vs 2007-01-04
  • 打赏
  • 举报
回复
if you retrive your data from database and store it in dataset

---- get the selectedID, note that the following code only change the data
in your dataset rather than database
form each row in dataset.table("table").rows
if row.item("id") = selectedID
row.delete
end if
next

--------- when you click the "submit" botton

dim rowafftected as integer

rowaffected = dataadapter.update(dataset', "yourTable")

orange_morning 2007-01-04
  • 打赏
  • 举报
回复
我想早恋,可已经晚了
huhu88a 2007-01-04
  • 打赏
  • 举报
回复
应该有更好的方法,这也太复杂点了吧!
等等就下山 2007-01-04
  • 打赏
  • 举报
回复
应该可以把数据放在dataadapter中,然后使用sqladapter的update方法更新
不过没用过,感觉不实用,一直都是遍历整个datagrid来更新
realgoal123 2007-01-04
  • 打赏
  • 举报
回复
paleyyang 2007-01-04
  • 打赏
  • 举报
回复
顶。
hflkl1314 2007-01-04
  • 打赏
  • 举报
回复
eqh(我想早恋,可已经晚了...)
是不是就是用循环把表中的内容取出,然后更新啊,有没有其他办法
coffeeant 2007-01-03
  • 打赏
  • 举报
回复
顶下
PCHWBANK 2007-01-03
  • 打赏
  • 举报
回复
mark
eqh 2007-01-03
  • 打赏
  • 举报
回复
参考:

/// <summary>
/// WebForm2 的摘要说明。
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
private string strcon = "server=localhost;database=pubs;uid=sa;pwd=wang";
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
System.Data.SqlClient.SqlConnection con=new System.Data.SqlClient.SqlConnection(strcon);
string sql="select top 5 * from authors order by au_id desc";
System.Data.SqlClient.SqlCommand cmd=new System.Data.SqlClient.SqlCommand(sql,con);
con.Open();
DataGrid1.DataSource = cmd.ExecuteReader();
DataGrid1.DataBind();
con.Close();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.Item.ItemType == ListItemType.Footer)
{
if(e.CommandName=="edit")
{
for(int i=0;i< DataGrid1.Items.Count;i++)
{

TextBox t1=new TextBox();
t1=(TextBox)DataGrid1.Items[i].FindControl("TextBox1");

TextBox t2=new TextBox();
t2=(TextBox)DataGrid1.Items[i].FindControl("TextBox2");
string id=DataGrid1.Items[i].Cells[0].Text;
string sql="update authors set au_fname='"+t1.Text+"',au_lname='"+t2.Text+"' where au_id='"+id+"'";
if(Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery(strcon,CommandType.Text,sql)!=1)
{
Response.Write("更新失败!");
return;
}

//Response.Write("<Li>ID:"+DataGrid1.Items[i].Cells[0].Text);
//Response.Write("更新内容1:" + t1.Text);
//Response.Write("更新内容2:" + t2.Text);

}

BindGrid();
}
}
}

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//if(e.Item.ItemType == ListItemType.Footer)
//{
//Button b1=(Button)e.Item.FindControl("Button1");
//b1.Attributes.Add("onclick","return confirm('您真的要全部更新吗?');");
//}

if(e.Item.ItemType== ListItemType.Item || e.Item.ItemType== ListItemType.AlternatingItem)
{
TextBox t1,t2;
t1=(TextBox)e.Item.FindControl("TextBox1");
t1.Attributes.Add("onfocus","this.className='edit'");
t1.Attributes.Add("onblur","this.className='noedit'");

t2=(TextBox)e.Item.FindControl("TextBox2");
t2.Attributes.Add("onfocus","this.className='edit'");
t2.Attributes.Add("onblur","this.className='noedit'");

}
}




}
HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="1.css" type="text/css" rel="stylesheet">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server" CellPadding="3" BackColor="White" BorderWidth="1px"
BorderStyle="None" BorderColor="#CCCCCC" AutoGenerateColumns="False" ShowFooter="True" ShowHeader="False">
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"></SelectedItemStyle>
<ItemStyle ForeColor="#000066"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="au_id" HeaderText="ID"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox id=TextBox1 runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"au_fname")%>' CssClass="noedit">
</asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="*!" ControlToValidate="TextBox1"
EnableViewState="False"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox id=TextBox2 runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"au_lname")%>' CssClass="noedit">
</asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ErrorMessage="*!" ControlToValidate="TextBox2"
EnableViewState="False"></asp:RequiredFieldValidator>
</ItemTemplate>
<FooterTemplate>
<DIV align="right">
<asp:Button id="Button1" runat="server" Text="更新" CommandName="edit"></asp:Button><INPUT type="reset" value="还原"> </DIV>
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
</form>
</body>
</HTML>
RC3修正和改进的问题包括: 1.前台对主题进行管理操作后,增加主题显示最后一次管理操作信息的功能; 2.后台增加设置主题被执行管理操作后,是否短消息通知主题作者; 3.前台增加Ajax发表回复功能,Ctrl+回车无刷新快速回复(另保留传统回复子模板供用户选择); 4.添加未删除setup和upgrade文件夹时锁定后台功能。 5.改进用户生日的注册和格式验证; 6.修正后台用户组评分范围修改后未及时更新缓存的bug; 7.修正短消息转发时自动填写用户名的bug; 8.修正短消息回复时内容的html字符译码功能; 9.查看短消息时增加显示日期; 10.前台设置精华和置顶时默认设置为1级; 11.修改前台编辑帖子的已上传附件功能,使上传附件的信息显示更明晰; 12.编辑帖子界面增加了查看帖子的链接; 13.修正前台查看个人信息时默认模板未加版权信息页脚; 14.改进后台DataGrid控件的翻页功能; 15.添加后台批量删除用户功能; 16.添加cache.config记录缓存的更新时间,已保证设置的及时生效; 17.修改后台更新缓存,增加了部分缓存的更新; 18.修改邮箱设置,增加了SMTP端口的设置; 19.增加后台搜索帖子后全部删除功能; 20.修改了帖子列表的帖子列表的模版,增加横线使帖子阅读更清晰; 21.修改部分页面的模版错误; 22.修改了精华设置增加默认选择; 23.修正后台版块合并,版块和分类合并的错误; 24.修正后台版块管理版块调整操作时可能引起CPU满负荷的错误; 25.修正后台DataGrid控件的viewstate错误; 26.修正后台多处提示和界面 27.修正偶尔出现的在线表重建时的错误 28.改进后台添加广告时设置广告类型后投放范围设置无法保留的问题; 29.改进后台修改用户勋章时操作烦琐的问题; 30.改进后台合并用户; 31.修正后台添加用户时用户名大写字符自动被转为小写的问题; 32.修正精华区错误;1-19 33.修正用户访问查看ip功能时提示不正常的错误;1-19 34.改进用户操作类;1-19 35.修正会员列表按管理权限排列时的错误;1-23 36.高亮操作实现自定义颜色;1-23 37.修正用户控制面版头象选择模版的一处错误;1-23 38.添加后台用户组名称颜色自定义控件;1-23 39.添加模版编辑的颜色自定义控件;1-23 40.修正查看用户资料搜索该用户帖子无效的问题;1-23 41.修正短消息标题可以使用空格的问题;1-23

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧