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

目录
相关文章
|
IDE Java Linux
Seata常见问题之重新打包以单独用jar来部署如何解决
Seata 是一个开源的分布式事务解决方案,旨在提供高效且简单的事务协调机制,以解决微服务架构下跨服务调用(分布式场景)的一致性问题。以下是Seata常见问题的一个合集
526 1
|
自然语言处理 并行计算 PyTorch
GitHub 开源神器 Bark模型,让文本转语音更简单!
GitHub 开源神器 Bark模型,让文本转语音更简单!
537 0
|
弹性计算 运维 安全
2024年阿里云一键搭建部署幻兽帕鲁服务器解决方案
幻兽帕鲁火了,为了确保畅快体验游戏,构建高效、稳定的游戏服务器至关重要。幸运的是,阿里云为您提供了快速、简便的服务器搭建解决方案,即使您对技术知识了解有限,也能在短短一分钟内轻松完成《幻兽帕鲁》游戏的联机服务器搭建!
2165 4
|
容器
文本溢出省略号text-overflow: ellipsis显示无效?这一属性到底该怎么用?
文本溢出省略号text-overflow: ellipsis显示无效?这一属性到底该怎么用?
513 0
Element UI 数字输入框的实现
Element UI 数字输入框的实现
533 0
|
供应链 区块链 数据安全/隐私保护
探索区块链技术在金融领域的应用与前景分析
本文将深入探讨区块链技术在金融领域的具体应用场景,分析其优势与挑战,并展望未来发展趋势。通过案例分析和技术解析,揭示区块链技术在金融行业中的革新意义及前景。
2232 15
|
弹性计算 应用服务中间件 Apache
2核4g云服务器支持多少人同时在线?2核4G服务器并发数计算
阿里云服务器2核4g并发数是多少?支持多少人同时在线?
1352 0
2核4g云服务器支持多少人同时在线?2核4G服务器并发数计算
|
机器学习/深度学习 监控 负载均衡
金融风控系统的演进与升级:从第一代到第四代(1)
金融风控系统的演进与升级:从第一代到第四代
1167 0
|
Linux Shell 应用服务中间件
Linux which & whereis & locate 命令详解
Linux which & whereis & locate 命令详解
255 0
|
存储 数据采集 人工智能
跨越时空的对话:如何使用AI阅读工具ChatDOC快速建立数字化身?
开门见山,这篇文章主要介绍如何将 AI 改造为靠谱、好用、基于某个人物的数字化身。比如,乔布斯 AI、马斯克 AI、张一鸣 AI、王兴 AI、佛陀 AI、孔子 AI. 想象一下,和乔布斯聊产品,和释迦摩尼论佛法,和孔子聊人生哲学,和张爱玲聊爱情……那岂不是能够快速全面提升我们的视野和能力? 让各个领域的精英群体或者名人,成为你的专属 AI 助手,便是这篇文章的写作目的。
724 0