C#中如何实现DataGrid多选功能?急!!! 加一个模板列里放入一个CHECKBOX 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用模板列,放checkbox。<asp:TemplateColumn> <ItemTemplate> <asp:CheckBox id="CheckBox1" runat="server"></asp:CheckBox></FONT> </ItemTemplate><asp:TemplateColumn> int count = MyDataGrid.Items.Count;for(int i=0;i<count;i++){ if(((CheckBox)(MyDataGrid.Items[i].FindControls("MyCheckBox"))).Selected) { //选中的行执行的操作 } else { //未选中的行执行的操作 }} 加一个模板列里放入一个CHECKBOX,把选中的id返回就是 for (int i = 0; i < grdPayments.Items.Count; i++) { DataGridItem dgrdIItem = grdPayments.Items[i]; CheckBox chkCheckBox = (CheckBox)dgrdIItem.FindControl("chkPayment"); if (chkCheckBox.Checked) { intSequence = Common.Utility.StringToInteger( dgrdIItem.Cells[1].Text ); try { for(int j=0; j < dtPayments.Rows.Count; j++) { if ( dtPayments.Rows[j]["Sequence"].ToString() == Convert.ToString( intSequence ) ) { dtPayments.Rows[j].Delete(); break; } } } catch { Response.Redirect("../showmessage.aspx?code=6906"); } } webform1-->html:<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm1" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML> <HEAD> <title>WebForm1</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <FONT face="宋体"> <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 64px" runat="server"></asp:DataGrid> <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 8px; POSITION: absolute; TOP: 24px" runat="server" Text="open"></asp:Button></FONT> </form> </body></HTML>webform1-->cs:using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;namespace WebApplication2{ /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid DataGrid1; protected System.Web.UI.HtmlControls.HtmlForm Form1; protected System.Web.UI.WebControls.Button Button1; SqlConnection sc=new SqlConnection("server=localhost;database=pubs;uid=zxy;pwd=zxy"); private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 if (!this.IsPostBack) { if (this.Session["_value"]!=null) { BindData(this.Session["_value"].ToString()); } } } private void BindData(string Value) { SqlDataAdapter sp=new SqlDataAdapter("select emp_id,fname from employee where emp_id in("+Value+") order by emp_id",sc); DataSet ds=new DataSet(); sp.Fill(ds,"titleauthor"); DataGrid1.DataSource=ds.Tables[0].DefaultView; DataGrid1.DataBind(); } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Button1_Click(object sender, System.EventArgs e) { this.Response.Write("<script language='javascript'>window.showModalDialog('Webform3.aspx', window,'');</script>"); } }}webform3-->html:<%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm3" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML> <HEAD> <title>WebForm3</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <base target="_self"> <FONT face="宋体"></FONT> <form id="Form1" method="post" runat="server"> <FONT face="宋体"></FONT> <asp:DataGrid id="DataGrid1" style="Z-INDEX: 103; LEFT: 8px; POSITION: absolute; TOP: 48px" runat="server" AutoGenerateColumns="False" Width="216px"> <Columns> <asp:TemplateColumn> <ItemTemplate> <FONT face="宋体"> <asp:CheckBox id="cb_del" runat="server"></asp:CheckBox></FONT> </ItemTemplate> </asp:TemplateColumn> <asp:BoundColumn DataField="emp_id" HeaderText="emp_id"></asp:BoundColumn> <asp:BoundColumn DataField="fname" HeaderText="fname"></asp:BoundColumn> </Columns> </asp:DataGrid> <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 240px; POSITION: absolute; TOP: 16px" runat="server" Text="返回"></asp:Button> </form> </body></HTML>webform3-->cs:using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;namespace WebApplication2{ /// <summary> /// WebForm3 的摘要说明。 /// </summary> public class WebForm3 : System.Web.UI.Page { protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.DataGrid DataGrid1; protected System.Web.UI.HtmlControls.HtmlForm Form1; SqlConnection sc=new SqlConnection("server=localhost;database=pubs;uid=zxy;pwd=zxy"); private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 if (!this.IsPostBack) { BindData(); } } private void BindData() { SqlDataAdapter sp=new SqlDataAdapter("select emp_id,fname from employee order by emp_id",sc); DataSet ds=new DataSet(); sp.Fill(ds,"titleauthor"); DataGrid1.DataSource=ds.Tables[0].DefaultView; DataGrid1.DataBind(); } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void dataGridSelect() { string _key=""; for (int i=0;i<=DataGrid1.Items.Count-1;i++) { CheckBox ck=(CheckBox)DataGrid1.Items[i].Cells[0].FindControl("cb_del");//增加一个模板列CheckBox if (ck.Checked) { _key="'"+DataGrid1.Items[i].Cells[1].Text+"',"+_key; } } String a=_key,b=""; b=a.Substring(0,a.Length-1); if (this.Session==null) { this.Session.Add("_value",b); } else { this.Session["_value"]=b; } } private void Button1_Click(object sender, System.EventArgs e) { dataGridSelect(); this.Response.Write("<script language='javascript'>window.dialogArguments.location.href =window.dialogArguments.location.href;window.close();</script>"); } }} 有做过门户网 或数据量比较多的网站,进来帮帮忙 Web使用AD登陆方式,切换用户登陆问题 |M| 求SQL语句取出当前ID在查出来的记录中的第几行 大虾救命 问个字符串的问题 System.Data.OleDb的DLL在那下载?? C#单击按钮时显示提示信息,停留几秒后自动消失 关于Response.Redirect跳转后乱码的问题 关闭进程问题 一位一点都不懂asp.net的经理叫我做这么一个功能, 安装.net2003中文版的问题 是不是一个放在runat=server上的控件的Visible属性会继承这个表格的Visible状态
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox id="CheckBox1" runat="server"></asp:CheckBox></FONT>
</ItemTemplate><asp:TemplateColumn>
for(int i=0;i<count;i++)
{
if(((CheckBox)(MyDataGrid.Items[i].FindControls("MyCheckBox"))).Selected)
{
//选中的行执行的操作
}
else
{
//未选中的行执行的操作
}}
{
DataGridItem dgrdIItem = grdPayments.Items[i];
CheckBox chkCheckBox = (CheckBox)dgrdIItem.FindControl("chkPayment");
if (chkCheckBox.Checked)
{
intSequence = Common.Utility.StringToInteger( dgrdIItem.Cells[1].Text );
try
{
for(int j=0; j < dtPayments.Rows.Count; j++)
{
if ( dtPayments.Rows[j]["Sequence"].ToString() == Convert.ToString( intSequence ) )
{
dtPayments.Rows[j].Delete();
break;
}
}
}
catch
{
Response.Redirect("../showmessage.aspx?code=6906");
}
}
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 64px" runat="server"></asp:DataGrid>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 8px; POSITION: absolute; TOP: 24px" runat="server"
Text="open"></asp:Button></FONT>
</form>
</body>
</HTML>webform1-->cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace WebApplication2
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.Button Button1; SqlConnection sc=new SqlConnection("server=localhost;database=pubs;uid=zxy;pwd=zxy");
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!this.IsPostBack)
{
if (this.Session["_value"]!=null)
{
BindData(this.Session["_value"].ToString());
}
}
}
private void BindData(string Value)
{
SqlDataAdapter sp=new SqlDataAdapter("select emp_id,fname from employee where emp_id in("+Value+") order by emp_id",sc);
DataSet ds=new DataSet();
sp.Fill(ds,"titleauthor");
DataGrid1.DataSource=ds.Tables[0].DefaultView;
DataGrid1.DataBind();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
this.Response.Write("<script language='javascript'>window.showModalDialog('Webform3.aspx', window,'');</script>");
}
}
}webform3-->html:
<%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<base target="_self">
<FONT face="宋体"></FONT>
<form id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 103; LEFT: 8px; POSITION: absolute; TOP: 48px" runat="server"
AutoGenerateColumns="False" Width="216px">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<FONT face="宋体">
<asp:CheckBox id="cb_del" runat="server"></asp:CheckBox></FONT>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="emp_id" HeaderText="emp_id"></asp:BoundColumn>
<asp:BoundColumn DataField="fname" HeaderText="fname"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 240px; POSITION: absolute; TOP: 16px" runat="server"
Text="返回"></asp:Button>
</form>
</body>
</HTML>webform3-->cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace WebApplication2
{
/// <summary>
/// WebForm3 的摘要说明。
/// </summary>
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
SqlConnection sc=new SqlConnection("server=localhost;database=pubs;uid=zxy;pwd=zxy");
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!this.IsPostBack)
{
BindData();
}
}
private void BindData()
{
SqlDataAdapter sp=new SqlDataAdapter("select emp_id,fname from employee order by emp_id",sc);
DataSet ds=new DataSet();
sp.Fill(ds,"titleauthor");
DataGrid1.DataSource=ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void dataGridSelect()
{
string _key="";
for (int i=0;i<=DataGrid1.Items.Count-1;i++)
{
CheckBox ck=(CheckBox)DataGrid1.Items[i].Cells[0].FindControl("cb_del");//增加一个模板列CheckBox
if (ck.Checked)
{
_key="'"+DataGrid1.Items[i].Cells[1].Text+"',"+_key;
}
}
String a=_key,b="";
b=a.Substring(0,a.Length-1);
if (this.Session==null)
{
this.Session.Add("_value",b);
}
else
{
this.Session["_value"]=b;
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
dataGridSelect();
this.Response.Write("<script language='javascript'>window.dialogArguments.location.href =window.dialogArguments.location.href;window.close();</script>");
}
}
}