如何同时编辑 GridView的 多行,sql server版的.立马结贴哦
access版本如下.本人新手.
http://dotnet.aspx.cc/article/a933b187-06c3-4263-9eec-414a54d9c815/read.aspx
------解决方案--------------------sql server版本
<%@ Page Language= "C# " Debug= "true " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script runat= "server ">
string[] ClassNameArray = { "大学 ", "中学 ", "高中 " };
//初始化原来的值
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRowView drv = (System.Data.DataRowView)e.Row.DataItem;
RadioButtonList rbl = (RadioButtonList)e.Row.FindControl( "txtGender ");
if (rbl != null)
{
if ((bool)drv[ "Gender "])
{
rbl.Items.FindByText( "男 ").Selected = true;
}
else
{
rbl.Items.FindByText( "女 ").Selected = true;
}
}
DropDownList ddl = (DropDownList)e.Row.FindControl( "txtClassName ");
if (ddl != null)
{
ddl.Items.FindByText(drv[ "ClassName "].ToString()).Selected = true;
}
}
}
// 初始化项目列表
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl( "txtClassName ");
ddl.DataSource = ClassNameArray;
ddl.DataBind();
}
}
//处理提交的数据
protected void XianhuiMeng_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GridView1.Rows)
{
string tGender = ((RadioButtonList)gvr.FindControl( "txtGender ")).SelectedValue;
SqlDataSource1.UpdateParameters.Add(new Parameter( "Title ", TypeCode.String, ((TextBox)gvr.FindControl( "txtTitle ")).Text));
SqlDataSource1.UpdateParameters.Add(new Parameter( "Gender ", TypeCode.Boolean, (tGender == "男 " ? "True " : "False ")));
SqlDataSource1.UpdateParameters.Add(new Parameter( "ClassName ",
TypeCode.String, ((DropDownList)gvr.FindControl( "txtClassName ")).SelectedValue));
SqlDataSource1.UpdateParameters.Add(new Parameter( "id ", TypeCode.Int32, GridView1.DataKeys[gvr.DataItemIndex].Value.ToString()));
SqlDataSource1.Update();
SqlDataSource1.UpdateParameters.Clear();
}
Response.Redirect(Request.UrlReferrer.ToString());
}
</script>
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head id= "Head1 " runat= "server ">
<title> 同时编辑 GridView的 多行 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<asp:GridView ID= "GridView1 " runat= "server " Font-Size= "12px " CellPadding= "3 " AutoGenerateColumns= "False "
DataKeyNames= "id " DataSourceID= "SqlDataSource1 " OnRowDataBound= "GridView1_RowDataBound "
OnRowCreated= "GridView1_RowCreated ">
<HeaderStyle BackColor= "#EDEDED " />
<Columns>
<asp:TemplateField HeaderText= "姓名 ">