当前位置:编程学习 > C#/ASP.NET >>

gridview

 girdView我想是我们在进行web编程的过程中用得太多了,可是,正是太过熟悉,可能会有一些东西我们没有意识到,下面就通过一些简单效果来走进girdView控件。

实现如下效果:

1、增加鼠标动作

2、为包含有特定值的行改变样式

3、客户端隐藏特定的列

4、一次删除多条数据

5、在gridview之外的地方显示当前的页码

 

说明:所用数据库为pubs数据库中的authors。界面设计如下:

 

 


后台代码:


[csharp] 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
 
namespace gridView 

    public partial class _3 : System.Web.UI.Page 
    { 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!IsPostBack) 
            { 
                for (int i = 1; i <=GridView1.Rows.Count; i++) 
                { 
                    DropDownList1.Items.Add(i.ToString()); 
                } 
            } 
        } 
 
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
        { 
            if (e.Row.RowType== DataControlRowType.DataRow) 
            { 
                //e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.backgroundColor='#00ffee';"); 
                //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c"); 
 
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00ffee';"); 
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;"); 
            } 
        } 
        /// <summary> 
        /// 为包含有特定值的行改变样式 
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
        { 
            for (int i = 0; i < GridView1.Rows.Count; i++) 
            { 
                string lbl = Convert.ToString(DataBinder.Eval(e.Row.DataItem,"state")); 
                if (lbl=="CA") 
                { 
                    e.Row.BackColor = System.Drawing.Color.LimeGreen; 
                } 
            } 
        } 
        /// <summary> 
        /// 列全部显示 
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        protected void Button1_Click(object sender, EventArgs e) 
        { 
            int columns = Convert.ToInt32(DropDownList1.SelectedValue); 
            GridView1.Columns[columns].Visible = false; 
        } 
        /// <summary> 
        /// 全选 
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        protected void CheckBox1_CheckedChanged(object sender, EventArgs e) 
        { 
            foreach (GridViewRow gr in GridView1.Rows) 
            { 
                CheckBox chk = (CheckBox)gr.FindControl("itemchk"); 
                if (!chk.Checked) 
                { 
                    chk.Checked = true; 
                } 
                else 
                { 
                    chk.Checked = false; 
                } 
            } 
  &nb

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,