对所有行做一个遍历,挨个判断
具体代码如下:
页面文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="GirdView_Default" %>
无标题页 代码文件:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class GirdView_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGV();
}
}
private void BindGV()
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
for (int i = 1; i <= 10; i++)
{
DataRow dr = dt.NewRow();
dr["ID"] = i;
dr["Name"] = "Name" + i.ToString();
//dt.Rows[i - 1]["ID"] = i;
//dt.Rows[i - 1]["Name"] = "Name" + i.ToString();
dt.Rows.Add(dr);
}
gv.DataSource = dt.DefaultView;
gv.DataBind();
}
protected void btnModify_Click(object sender, EventArgs e)
{
string strMsg = "修改成功!";
foreach (GridViewRow row in gv.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("chkSelect");
TextBox txtName = (TextBox)row.FindControl("txtName");
if (chk.Checked)
{
int iID = int.Parse(row.Cells[1].Text);
string strName = txtName.Text;
//修改代码
//……
//结束
strMsg += @"\r\nID:" + iID.ToString() + ",Name:" + txtName.Text;
}
}
ClientScript.RegisterClientScriptBlock(GetType(), "ss", "");
}
}