gridviewの高度な使用

5903 ワード

 
   
 
    

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

using System.Text;

using System.IO;



namespace WebApplication2

{

    public partial class WebForm6 : System.Web.UI.Page

    {

        SqlConnection sqlcon;

        string strCon = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True;User Instance=True";

        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 Button2_Click(object sender, EventArgs e)

        {

            sqlcon = new SqlConnection(strCon);

            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 tb_sql where sname='" + GridView1.DataKeys[i].Value + "'";

                    sqlcom = new SqlCommand(sqlstr, sqlcon);

                    sqlcon.Open();

                    sqlcom.ExecuteNonQuery().ToString();

                    sqlcon.Close();

                }

            }

            bind();

        }

        protected void Button1_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 5 * from tb_sql";

            sqlcon = new SqlConnection(strCon);

            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

            DataSet myds = new DataSet();

            sqlcon.Open();

            myda.Fill(myds, "tb_sql");

            GridView1.DataSource = myds;

            GridView1.DataKeyNames = new string[] { "sname" };

            GridView1.DataBind();

            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)

            {

                DataRowView mydrv = myds.Tables["tb_sql"].DefaultView[i];

                string score = Convert.ToString(mydrv["sid"]);

                if (Convert.ToDouble(score) < 5)//              ToInt32  

                {

                    GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;

                }

            }

            //sqlcon.Close();



            sqlcon.Close();

        }





        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

        {

            int i;

            //    ,           

            for (i = 0; i < GridView1.Rows.Count; i++)

            {

                //          

                if (e.Row.RowType == DataControlRowType.DataRow)

                {

                    //           

                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");

                    //           

                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                }

            }



            //         

            if (e.Row.RowType == DataControlRowType.DataRow)

            {

                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)

                {



                    //CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

                    ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('         :\"" + e.Row.Cells[2].Text + "\" ?')");

                }

            }

            if (e.Row.RowIndex != -1)

            {

                int id = e.Row.RowIndex + 1;

                e.Row.Cells[0].Text = id.ToString();

            }





        }



        protected void Button3_Click(object sender, EventArgs e)

        {

            Export("application/ms-excel", "      .xls");



        }

        private void Export(string FileType, string FileName)

        {

            Response.Charset = "GB2312";

            Response.ContentEncoding = System.Text.Encoding.UTF7;

            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());

            Response.ContentType = FileType;

            this.EnableViewState = false;

            StringWriter tw = new StringWriter();

            HtmlTextWriter hw = new HtmlTextWriter(tw);

          

            GridView1.RenderControl(hw);

          

            Response.Write(tw.ToString());

            Response.End();

        }

        public override void VerifyRenderingInServerForm(Control control)

        {

        }





    }



}