for(int i=0;i<MyGrid.items.Count;i++)
{
CheckBox check= (CheckBox)MyGrid.item[i].FindControl("DeleteOrNot")
……你的操作
}

解决方案 »

  1.   

    应该是在ItemDataBound事件里:
    For Each CheckBoxItem In MyGrid.Items  //去掉这句!!
     IsChecked = CType(CheckBoxItem.FindControl("DeleteOrNot"), CheckBox).Checked
    ..................
      

  2.   

    IsChecked = CType(e.Item.FindControl("DeleteOrNot"), CheckBox).Checked
      

  3.   

    .aspx文件
    <%@ Page language="c#" Codebehind="DataGridCheckBox.aspx.cs" AutoEventWireup="false" Inherits="test.DataGridCheck" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>DataGridCheck</title>
    <meta content="Microsoft Visual Studio 7.0" 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">
    </HEAD>
    <body>
    <form id="DataGridCheck" method="post" runat="server">
    <asp:datagrid id="dgMain" runat="server" Width="98%" AutoGenerateColumns="False">
    <SelectedItemStyle Font-Size="9pt" Font-Names="宋体"></SelectedItemStyle>
    <EditItemStyle Font-Size="9pt" Font-Names="宋体" Font-Bold="True" ForeColor="Red" BackColor="Info"></EditItemStyle>
    <AlternatingItemStyle Font-Size="9pt" Font-Names="宋体" ForeColor="ControlText" BackColor="White"></AlternatingItemStyle>
    <ItemStyle Font-Size="9pt" Font-Names="宋体" ForeColor="ControlText" BackColor="WhiteSmoke"></ItemStyle>
    <HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="Black" VerticalAlign="Middle" BackColor="Control"></HeaderStyle>
    <Columns>
    <asp:TemplateColumn HeaderText="操作">
    <HeaderStyle HorizontalAlign="Center" Width="50px"></HeaderStyle>
    <ItemTemplate>
    <asp:CheckBox ID="chkExport" Runat="server" />
    </ItemTemplate>
    <EditItemTemplate>
    <asp:CheckBox ID="chkExportON" Runat="server" Enabled="true" />
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn Visible="False">
    <ItemTemplate>
    <asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "medicine_id")%>' runat="server" Width="80%" ID="lblID" />
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="medicine_id" ReadOnly="True" HeaderText="药品编号">
    <HeaderStyle Width="80px"></HeaderStyle>
    </asp:BoundColumn>
    <asp:TemplateColumn SortExpression="demo" HeaderText="药品名称">
    <ItemTemplate>
    <asp:Label Text='<%# Server.HtmlEncode((string)DataBinder.Eval(Container.DataItem, "medicine_name"))%>' runat="server" Width="80%" ID="lblColumn" />
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid><asp:button id="cmdSelectAll" runat="server" Text="全部选中"></asp:button><asp:button id="cmdFindSelected" runat="server" Text="删除选中的项目"></asp:button><br>
    <asp:label id="Label1" runat="server"></asp:label></form>
    </body>
    </HTML>
    .aspx.cs文件
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Configuration;namespace test
    {
    /// <summary>
    /// DataGridCheck 的摘要说明。
    /// </summary>
    public class DataGridCheck : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid dgMain;
    protected System.Web.UI.WebControls.Button cmdSelectAll;
    protected System.Web.UI.WebControls.Button cmdFindSelected;
    protected System.Web.UI.WebControls.Label Label1;  
    protected System.Data.SqlClient.SqlConnection cn;
    protected System.Data.SqlClient.SqlCommand cm;
    protected System.Data.SqlClient.SqlDataAdapter da;
    DataView oDataView;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    // dgMain.Columns[0].HeaderText = "选项";
    // dgMain.Columns[1].HeaderText = "药品编号";
    // dgMain.Columns[2].HeaderText = "药品名称";
    // cmdFindSelected.Text = "删除选中的项目";
    if( !this.IsPostBack)
    {
    cmdSelectAll.Text = "全部选中";
    RefreshGrid();
    cmdFindSelected.Attributes.Add("onclick","return confirm('yes/no');");
    }
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.cmdSelectAll.Click += new System.EventHandler(this.cmdSelectAll_Click);
    this.cmdFindSelected.Click += new System.EventHandler(this.cmdFindSelected_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void cmdSelectAll_Click(object sender, System.EventArgs e)
    {
    selectAll();
    } private void selectAll()
    {
    System.Web.UI.WebControls.CheckBox chkExport ;
    if( cmdSelectAll.Text == "全部选中")
    {
    foreach(DataGridItem oDataGridItem in dgMain.Items)
    {
    chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");
    chkExport.Checked = true;
    }
    cmdSelectAll.Text = "全部不选";
    }
    else
    {
    foreach(DataGridItem oDataGridItem in dgMain.Items)
    {
    chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");
    chkExport.Checked = false;
    }
    cmdSelectAll.Text = "全部选中";
    }
    } private void RefreshGrid()
    {
    DataSet oDataSet = new DataSet();
    try
    {
    string sSQL = "Select * from medicine order by medicine_id";
    cn = new SqlConnection(ConfigurationSettings.AppSettings["sa"]);
    da = new SqlDataAdapter(sSQL,cn);
    cn.Open();
    da.Fill(oDataSet,"medicine");
    oDataView = new DataView(oDataSet.Tables["medicine"]);
    dgMain.DataSource = oDataView;
    dgMain.DataKeyField="medicine_id";
    dgMain.DataBind();
    cn.Close();
    }
    catch(Exception ex)
    {
    Label1.Text = ex.ToString();
    }
    } private void cmdFindSelected_Click(object sender, System.EventArgs e)
    {
    System.Web.UI.WebControls.CheckBox chkExport;
    System.Collections.ArrayList oExArgs = new System.Collections.ArrayList();
    string sID;
    Label1.Text = "";
    foreach(DataGridItem oDataGridItem in dgMain.Items)
    {
    chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");
    if( chkExport.Checked)
    {
    Label1.Text = "";
    sID = ((Label)oDataGridItem.FindControl("lblID")).Text;
    oExArgs.Add(sID);
    int i = 0;
    cn = new SqlConnection(ConfigurationSettings.AppSettings["sa"]);
    cn.Open();
    string DeleteSQL="";
    for( i = 0;i<oExArgs.Count;i++)
    {
    // DeleteSQL="delete from medicine where medicine_id='"+oExArgs[i]+"'";
    DeleteSQL="Update medicine set medicine_stocks=medicine_stocks+1 where medicine_id='"+oExArgs[i]+"'";
    oExArgs.Clear();//保证不会重复更新。是重要的一个步骤。 cm = new SqlCommand(DeleteSQL,cn);
    cm.ExecuteNonQuery();
    DeleteSQL="";
    }
    cn.Close();
    }
    }
    RefreshGrid();
    cmdSelectAll.Text = "全部选中";
    }
    }
    }
      

  4.   

    还是不行啊 ,怎么搞的。我是玩VB的,楼上的兄台都写VB看看嘛
      

  5.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=FF130C7F-3650-4DA6-8943-8AA4AF3E3459http://dotnet.aspx.cc/Exam/DataGridCheckBox/DataGridCheckBox.aspx
      

  6.   

    http://www.codeproject.com/aspnet/datagrid_checkbox.asp
      

  7.   

    改好了,最后的结果为:                谢谢大家,散分
     Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim MyConnection As New SqlConnection("server=localhost;uid=sa;pwd=007;database=pubs")
            Dim DeleteCmd As String = "DELETE FROM Account WHERE AccountID = @Uid"
            Dim MyCommand As New SqlCommand(DeleteCmd, MyConnection)
            MyCommand.Parameters.Add("@Uid", SqlDbType.Int)
            Dim IsChecked As Boolean
            Dim CheckBoxItem As DataGridItem
            For Each CheckBoxItem In MyGrid.Items            IsChecked = CType(CheckBoxItem.FindControl("DeleteOrNot"), CheckBox).Checked
                If IsChecked Then
                    MyCommand.Parameters("@Uid").Value = MyGrid.DataKeys(CheckBoxItem.ItemIndex)
                    MyCommand.Connection.Open()
                    MyCommand.ExecuteNonQuery()
                    Try
                        MyCommand.ExecuteNonQuery()
                        Message.Text = "数据记录已经成功删除"
                    Catch Exc As SqlException
                        Message.Text = "错误: 无法删除数据纪录"
                    End Try                MyCommand.Connection.Close()
                End If
            Next
            Angel()
        End Sub