GridView操作

里面包含了GridView大部分的操作
并且配合Dropdownlist 和 联动

编辑时的联动定位可能写的不是很好

忘有大神提出优化


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestSQL
{
    public partial class index1 : System.Web.UI.Page
    {
        string _connectionString = ConfigurationManager.AppSettings["ConnectionString"];
        protected void Page_Load(object sender, EventArgs e)
        {
            /*
            if (HttpContext.Current.Session["UserName"] == null)
            {
                Response.Write("<script>alert('非法登录!请重新登录');window.location.href='Login.aspx'</script>");
                //Response.Redirect("Login.aspx", false); 
            }*/
         //   SessionLogin.Login1();

            if (!IsPostBack)
            {
                this.Bind();
            }

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //HiddenField hd = ((Label)GridView1.Rows[e.NewEditIndex].FindControl("Label2")).Text;
            // string b = ((Label)(GridView1.Rows[e.NewEditIndex].FindControl("Label3"))).Text.ToString();
            string Sname = (GridView1.Rows[e.NewEditIndex].FindControl("Label2") as Label).Text;//获取label的值
            string ClassN = (GridView1.Rows[e.NewEditIndex].FindControl("Label3") as Label).Text;//获取label的值
            GridView1.EditIndex = e.NewEditIndex;
            this.Bind();
            DropDownList ddl = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1");
            ddl.SelectedValue = Sname;
            SqlConnection conn = new SqlConnection(_connectionString);
            conn.Open();
            DropDownList DropDownList1 = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1"); //得到当前的DropDownList
            string Specialty_Name = DropDownList1.SelectedValue;
            string strSQL = "SELECT class_name FROM class  WHERE Specialty_ID=(SELECT ID FROM   Specialty WHERE Specialty_Name='" + Specialty_Name + "' ) ";
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            SqlDataAdapter adp = new SqlDataAdapter(strSQL, conn);
            adp.Fill(ds, "Class");
            dt = ds.Tables["Class"];
            GridViewRow dvr = (GridViewRow)DropDownList1.NamingContainer;
            DropDownList DropDownList2 = (DropDownList)dvr.FindControl("DropDownList2");
            DropDownList2.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DropDownList2.Items.Add(dt.Rows[i]["class_name"].ToString());
            }
            conn.Close();
            DropDownList2.SelectedValue = ClassN;
        }

        protected void Bind()
        {
            DataSet ds = new DataSet();
            SqlConnection sqlconn = new SqlConnection(_connectionString);
            SqlDataAdapter sqld = new SqlDataAdapter("select * from student", sqlconn);
            sqld.Fill(ds, "tabstudent");
            GridView1.DataSource = ds.Tables["tabstudent"].DefaultView;
            GridView1.DataBind();
            Label1.Text = "查找成功";  
        }

        //更新操作
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            SqlConnection conn = new SqlConnection(_connectionString);
            DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1");
            string Specialty_Name = ddl.SelectedValue;
            DropDownList ddl2 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2");
            string Class_Name = ddl2.SelectedValue;
            string strSQL = "update student set student_name="
                +"'" +((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "'"
                +",Sex="
                + "'" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "'"
                + ",Specialty="
                + "'"+Specialty_Name+"'"
                + ",Class="
                + "'" + Class_Name + "'"
                +"where Student_id="   
                + "'"+GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            GridView1.EditIndex = -1;
            Bind();
            Label1.Text = "更新成功";  
        }
        //GridView取消按钮操作
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            this.Bind();
        }
        //GridView删除操作
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string a = GridView1.Rows[e.RowIndex].Cells[4].Text;
            string b = ((Label)(GridView1.Rows[e.RowIndex].FindControl("Label2"))).Text;
            SqlConnection conn = new SqlConnection(_connectionString);
            string strSQL = "DELETE Student WHERE Student_id = "
                + "'" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            conn.Open();
            //cmd.ExecuteNonQuery();
            conn.Close();
            GridView1.EditIndex = -1;
            Bind();
            Label1.Text = "删除成功";  
        }
        //专业DropDownlist绑定数据
        public DataSet ddlBind() {
            SqlConnection conn = new SqlConnection(_connectionString);
            conn.Open();
            SqlDataAdapter adp = new SqlDataAdapter("SELECT Specialty_Name FROM Specialty ",conn);
            DataSet ds = new DataSet();
            adp.Fill(ds, "Specialty");
            conn.Close();
            return ds;
        }
        //下拉变化事件,级联的实现效果
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(_connectionString);
            conn.Open();            
            DropDownList DropDownList1 = (DropDownList)sender; //得到当前的DropDownList
            string Specialty_Name = DropDownList1.SelectedValue;
            string strSQL = "SELECT class_name FROM class  WHERE Specialty_ID=(SELECT ID FROM   Specialty WHERE Specialty_Name='" + Specialty_Name + "' ) ";
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            SqlDataAdapter adp = new SqlDataAdapter(strSQL, conn);
            adp.Fill(ds, "Class");
            dt = ds.Tables["Class"];
            GridViewRow dvr = (GridViewRow)DropDownList1.NamingContainer; //获取对DropDownList的容器引用
            //得到dvr后就好办了查找下面的子控件。
            DropDownList DropDownList2 = (DropDownList)dvr.FindControl("DropDownList2");
            DropDownList2.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DropDownList2.Items.Add(dt.Rows[i]["class_name"].ToString());
            }
            //DropDownList2.SelectedIndex = DropDownList1.SelectedIndex;
            conn.Close();
        }

        public void ReadLoadDroplist() { 

        }



        protected void Button1_Click(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)GridView1.Rows[0].FindControl("DropDownList1");
            ddl.SelectedValue = "Android開發與應用";
        }



    }
}

这里写图片描述
这里写图片描述
原创文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值