Griview中DataKeyNames的用法(多个主键)?

简介: 1.单个主键 using System; using System.Data; using System.Configuration; using System.Collections; using System.
1.单个主键 
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; 
using System.Data.SqlClient; 
public partial class GridViewCheckbox : System.Web.UI.Page 

    SqlConnection sqlcon; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!IsPostBack) 
        { 
            bind(); 
        } 
    } 
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e) 
    { 
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++) 
        { 
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); 
            if (CheckBox2.Checked == true) 
            { 
                cbox.Checked = true; 
            } 
            else 
            { 
                cbox.Checked = false; 
            } 
        } 
    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
        sqlcon = DB.createCon(); 
        SqlCommand sqlcom; 
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++) 
        { 
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); 
            if (cbox.Checked == true) 
            { 
                string sqlstr = "delete from Employee where 身份证号码='" + GridView1.DataKeys[i].Value + "'"; 
                sqlcom = new SqlCommand(sqlstr, sqlcon); 
                sqlcon.Open(); 
                sqlcom.ExecuteNonQuery(); 
                sqlcon.Close(); 
            } 
        } 
        bind(); 
    } 
    protected void Button2_Click(object sender, EventArgs e) 
    { 
        CheckBox2.Checked = false; 
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++) 
        { 
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); 
            cbox.Checked = false; 
        } 
    } 
    public void bind() 
    { 
        string sqlstr = "select top 4 * from Employee"; 
        sqlcon = DB.createCon(); 
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); 
        DataSet myds = new DataSet(); 
        sqlcon.Open(); 
        myda.Fill(myds, "Employee"); 
        GridView1.DataSource = myds; 
        GridView1.DataKeyNames = new string[] { "身份证号码" }; 
        GridView1.DataBind(); 
        sqlcon.Close(); 
    } 

2.多个主键 
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; 
using System.Data.SqlClient; 
public partial class SendMessageBox : System.Web.UI.Page 

    protected void Page_Load(object sender, EventArgs e) 
    { 
        if(!IsPostBack) 
        { 
            bind(); 
        } 
    } 
    public void bind() 
    { 
        string str = "select * from Sms_Send"; 
        SqlConnection con = DB.CreateDB(); 
        SqlDataAdapter da = new SqlDataAdapter(str, con); 
        DataSet ds = new DataSet(); 
        con.Open(); 
        da.Fill(ds, "Sms_Send"); 
        GridView1.DataSource = ds; 
        GridView1.DataKeyNames = new string[] { "Mobile", "SendTime" }; 
        GridView1.DataBind(); 
    } 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
        foreach (TableCell tc in e.Row.Cells) 
        { 
            tc.Attributes["style"] = "border-color:Black"; 
        } 
        if (e.Row.RowIndex != -1) 
        { 
            int id = e.Row.RowIndex + 1; 
            e.Row.Cells[1].Text = id.ToString(); 
        }     
    } 
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 
        GridView1.PageIndex = e.NewPageIndex; 
        bind(); 
        GridView1.PageIndex = e.NewPageIndex; 
    } 
    protected void btnSelectAll_Click(object sender, ImageClickEventArgs e) 
    { 
        for(int i=0;i<=GridView1.Rows.Count-1;i++) 
        { 
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); 
            if(cbox.Checked==false) 
            { 
                cbox.Checked=true; 
            } 
        } 
    } 
    protected void btnCancel_Click(object sender, ImageClickEventArgs e) 
    { 
        for(int i=0;i<=GridView1.Rows.Count-1;i++) 
        { 
            CheckBox cbox=(CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); 
            if(cbox.Checked==true) 
            { 
                cbox.Checked=false; 
            } 
        } 
    } 
   protected void btnQuery_Click(object sender, ImageClickEventArgs e)
    {
        DateTime start = DateTime.Parse(txtStart.Text.ToString());// Convert.ToDateTime(txtStart.Text.ToString());
        DateTime end = Convert.ToDateTime(txtEnd.Text.ToString());
        if (end <= start)
        {
            Page.RegisterStartupScript("message", "<Script>alert('结束日期必须大于开始日期!')</Script>");
            return;
        }
        string str = "select * from Sms_Send where SendTime between '" + start + "' and '" + end + "'";//"select * from Sms_Send where SendTime>=start and SendTime<=end";
        SqlConnection con = DB.CreateDB();
        SqlDataAdapter da = new SqlDataAdapter(str, con);
        DataSet ds = new DataSet();
        da.Fill(ds, "Sms_Send");
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "Mobile", "SendTime" };
        GridView1.DataBind();
    }
    protected void btnDelete_Click(object sender, ImageClickEventArgs e) 
    { 
        SqlConnection con = DB.CreateDB(); 
        SqlCommand cmd; 
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++) 
        { 
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); 
            if (cbox.Checked == true) 
            { 
                DataKey datakey = GridView1.DataKeys[i]; 
                string mobile = datakey["Mobile"].ToString(); 
                string sendTime = datakey["SendTime"].ToString(); 
                string str = "delete from Sms_Send where Mobile='" + mobile + "'and SendTime='" + sendTime + "'"; 
                cmd = new SqlCommand(str, con); 
                con.Open(); 
                cmd.ExecuteNonQuery(); 
                con.Close(); 
            } 
        } 
        bind(); 
    } 

}

http://www.cnblogs.com/huangjianhuakarl/articles/1095657.html

目录
相关文章
|
5月前
|
存储 关系型数据库 索引
10. 在一个非主键字段上创建了索引, 想要根据该字段查询到数据, 需要查询几次 ?
在非主键字段上创建索引,查询数据通常需两次。对于MyISAM,先通过索引找到数据行指针,再获取数据;而InnoDB则先找主键ID,再从主键索引中查找数据。
34 0
|
14天前
|
关系型数据库 MySQL 数据库
MYSQL索引的分类与创建语法详解
理解并合理应用这些索引类型,能够有效提高MySQL数据库的性能和查询效率。每种索引类型都有其特定的优势,适当地使用它们可以为数据库操作带来显著的性能提升。
38 3
|
5月前
|
存储 关系型数据库 MySQL
MySQL索引简介(包含索引优化,索引失效,最左前缀简洁版)
MySQL索引简介(包含索引优化,索引失效,最左前缀简洁版)
67 0
|
11月前
|
数据建模 Java 数据库
PowerDesigner使用之为表中字段添加索引
PowerDesigner使用之为表中字段添加索引
353 1
|
SQL 存储 运维
order by 字段到底要不要加索引?[大坑]
order by 字段到底要不要加索引?[大坑]
186 0
order by 字段到底要不要加索引?[大坑]
|
10月前
|
关系型数据库 MySQL 数据库
MySQL中列属性(主键、唯一键和自增等)使用实践
MySQL中列属性(主键、唯一键和自增等)使用实践
220 0
|
12月前
|
存储 数据库 索引
【面试官挖坑】聚集索引会隐式的自动为表添加一个主键索引那你是不是就可以不设置主键索引了?
【面试官挖坑】聚集索引会隐式的自动为表添加一个主键索引那你是不是就可以不设置主键索引了?
|
存储 关系型数据库 MySQL
mysql字符串等值查询中条件字段值末尾有空格也能查到数据问题
mysql字符串等值查询中条件字段值末尾有空格也能查到数据问题
241 0
|
存储 SQL 关系型数据库
MySql索引详解-各种索引的定义与区别和应用
什么是索引?索引的作用,有无索引的区别。
193 0
MySql索引详解-各种索引的定义与区别和应用