.NET基础之GridView控件

GridView控件是.net里的一个显示数据控件,该控件制作很人性化,基本上不用编写代码就可以完成数据绑定、分页、排序、编辑、删除、选定行等操作。

主要属性:
Sort:根据指定的排序表达式和方向对 GridView 控件进行排序。

编程的方法绑定数据,并实现分页
页面源代码中添加一个GridView控件(GridView1),并AllowPaging="True",设置PageIndexChanging事件。
cs代码: Code
protected void bind()
    {
        //此处为GridView1绑定数据库
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select * from test";
        SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
        DataSet myDs = new DataSet();
        myDa.Fill(myDs);
        GridView1.DataSource = myDs;
        GridView1.DataBind();
    }
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.bind();
    }

 

编程方法实现排序
页面源代码中添加一个GridView控件(GridView1),并设置GridView1的Sorting事件。
cs代码:

Code
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //设置信息字典,排序字段和排序方法
            ViewState["SortOrder"] = "ID";
            ViewState["OrderDire"] = "ASC";
            this.bind();
        }
    }
public SqlConnection GetConnection()
    {
        //读取web.config中的连接字符串,创建SqlConnection连接
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }
    protected void bind()
    {
        //此处为GridView1绑定数据库
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select * from test";
        SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
        DataSet myDs = new DataSet();
        myDa.Fill(myDs);

        //设置排序所用的字段和排序方法
        string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
        GridView1.Sort = sort;
        GridView1.DataSource = myDs;
        GridView1.DataBind();
    }
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {

        //获取排序的字段
        string sPage = e.SortExpression;

        //如果目前排序方式与设置一致,则逆序
        if (ViewState["SortOrder"].ToString() == sPage)
        {
            if (ViewState["OrderDire"].ToString() == "Desc")
            {
                ViewState["OrderDire"] = "ASC";
            }
            else
            {
                ViewState["OrderDire"] = "Desc";
            }
        }
        else
        {
            ViewState["SortOrder"] = e.SortExpression;
        }
        this.bind();
    }


 选择GridView控件的行,在另一个GridView控件中显示相关数据
页面源代码中添加两个GridView控件(GridView1,GridView2),并设置GridView1的SelectedIndexChanging事件。GridView1已经绑定了一个数据表,当点击GridView1一行时,在GridView2中显示相关的另一张表中的数据。
cs代码:

Code
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        //提交编辑时,先取得要编辑行的主键值
        int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string sqlStr = "delete from test where ID=" + ID;
        SqlConnection con = new SqlConnection();
        //读取web.config中的连接字符串,创建SqlConnection连接
        con.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
        DataSet myDs = new DataSet();
        myDa.Fill(myDs);
        //将数据表绑定到GridView2
        this.GridView2.DataSource = myDs;
        GridView2.DataBind();
    }

 
编程实现全选和全不选功能

页面源代码中添加两个GridView控件(GridView1),一个CheckBox控件(CheckBox1),设置CheckBox控件的AutoPostBack="True" ,为GridView1添加一个TemplateField列,并在编辑模版中为该列添加一个CheckBox 控件:。设置CheckBox的CheckedChanged事件。
cs代码: Code
public SqlConnection GetConnection()
   {
       //读取web.config中的连接字符串,创建SqlConnection连接
       string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
       SqlConnection myConn = new SqlConnection(myStr);
       return myConn;
   }
   protected void bind()
   {
       //此处为GridView1绑定数据库
       SqlConnection myConn = GetConnection();
       myConn.Open();
       string sqlStr = "select * from test";
       SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
       DataSet myDs = new DataSet();
       myDa.Fill(myDs);
       GridView1.DataSource = myDs;
       GridView1.DataBind();
       //释放并关闭连接
       myDa.Dispose();
       myDs.Dispose();
       myConn.Close();
   }
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("Check");
            if (CheckBox1.Checked == true)
            {
                //全选
                CheckBox1.Checked = true;
            }
            else
            {
                //反选
                CheckBox1.Checked = false;
            }
        }
    }

原文:http://www.cnblogs.com/shanymen/archive/2009/05/21/1486654.html

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-604087/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12639172/viewspace-604087/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值