日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Griview中DataKeyNames的用法(多個(gè)主鍵)?

 悟靜 2012-02-25

Griview中DataKeyNames的用法(多個(gè)主鍵)?

1.單個(gè)主鍵
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 身份證號(hào)碼='" + 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[] { "身份證號(hào)碼" };
        GridView1.DataBind();
        sqlcon.Close();
    }
}
2.多個(gè)主鍵
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('結(jié)束日期必須大于開(kāi)始日期!')</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();
    }
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多