1.       把gridView控件拖放到界面上

2.       用代码设置数据源绑定gridview控件

OleDbCommandnew OleDbCommand();  
        string sql = "select * from admin";    //绑定到数据表admin
        command.CommandText = sql;
        command.Connection = shyConnection.conn;
        shyConnection.conn.Open();    //打开数据连接
        OleDbDataAdapter adapter = new OleDbDataAdapter(command);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        GridViewX.DataSource = ds;
        GridViewX.DataKeyNames = new string[] { "ID" };  控件的主键数组
        GridViewX.DataBind();
   shyConnection.conn.Close();   //关闭数据连接

3.   在源界面gridView控件内加入如下代码:

<Columns>
               <asp:BoundField HeaderText="学号"DataField="XueHao" />      //绑定数据源的XueHao列
               <asp:BoundField HeaderText="姓名"DataField="Name" />        //绑定数据源的Name列
               <asp:BoundField HeaderText="密码"DataField="PasWord" />     //绑定数据源的PasWord列
               <asp:TemplateField HeaderText="删除">
                   <ItemTemplate>
                       <asp:CheckBox ID="CheckBox1" runat="server" />
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="编辑">
                   <ItemTemplate>
                       <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="zuoza"  CommandArgument="<%# GridViewX.Rows.Count %>" Text="编辑"></asp:LinkButton>
                   </ItemTemplate>
               </asp:TemplateField>
      </Columns>

4.判断gridView控件里面每列的CheckBox控件是否被中

  

protected void Button1_Click(object sender, EventArgs
    {
        for (int
        {
            CheckBox cbox = (CheckBox)GridViewX.Rows[i].FindControl("CheckBox1");   //在每列中查找名称是CheckBox1的CheckBox控件,然后强制实例转换为CheckBox的对象
            if (cbox.Checked == true)
            {
                Response.Write((int)GridViewX.DataKeys[i].Value + "<br>");  //输出当前列的主键值
            }
            else
            {
                Response.Write("SHY" + "</BR>");
            }
        }
}

5.如何点击编辑的时候触发函数GridViewX_RowCommand:   

protected void GridViewX_RowCommand(object sender, GridViewCommandEventArgs
    {
        if (e.CommandName == "edit")
        {
            string ID = "";
            int index = Convert.ToInt32((string)e.CommandArgument);
            ID = Convert.ToString(GridViewX.DataKeys[index].Value.ToString());  
//现在获取了所选记录的主键ID
        }
}

6.当控件加载的时候触发这个事件函数:GridViewX_RowDataBound

protected void GridViewX_RowDataBound(object sender, GridViewRowEventArgs
    {
        this.lblCurrentPage.Text = string.Format("当前第{0}页/总共{1}页", this.GridViewX.PageIndex + 1, this.GridViewX.PageCount);
        //遍历所有行设置边框样式
        foreach (TableCell tc in
        {
            tc.Attributes["style"] = "border-color:Black";
        }
        //用索引来取得编号
        if
        {
            int
            //e.Row.Cells[0].Text = id.ToString();
        }
    }