asp.net gridview 72般绝技第1/2页(aspnetpager分页控件)快来看

随心笔谈2年前发布 编辑
173 0
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买



GridView无代码分页排序

GridView选中,编辑,取消,删除

GridView正反双向排序

GridView和下拉菜单DropDownList结合

GridView和CheckBox结合

鼠标移到GridView某一行时改变该行的背景色方法一

鼠标移到GridView某一行时改变该行的背景色方法二

GridView实现删除时弹出确认对话框

GridView实现自动编号

GridView实现自定义时间货币等字符串格式

GridView实现用“…”代替超长字符串

GridView一般换行与强制换行

GridView显示隐藏某一列

GridView弹出新页面/弹出新窗口

GridView固定表头(不用javascript只用CSS,2行代码,很好用)

GridView合并表头多重表头无错完美版(以合并3列3行举例)

GridView突出显示某一单元格(例如金额低于多少,分数不及格等)

GridView加入自动求和求平均值小计

GridView数据导入Excel/Excel数据读入GridView

1.GridView无代码分页排序

效果图:

1.AllowSorting设为True,aspx代码中是AllowSorting=”True”;

2.默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代码中是PageSize=”12″。

3.默认的是单向排序的,右击GridView弹出“属性”,选择AllowSorting为True即可。

2.GridView选中,编辑,取消,删除:

效果图:

后台代码:

你可以使用sqlhelper,本文没用。代码如下:

using System;

using System.Data;

using System.Configuration;

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 _Default : System.Web.UI.Page

{

//清清月儿http://blog.csdn.net/21aspnet 

    SqlConnection sqlcon;

    SqlCommand sqlcom;

    string strCon=”Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码”;

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            bind();

        }

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

    {

        GridView1.EditIndex=e.NewEditIndex;

        bind();

    }

//删除

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

        string sqlstr=”delete from 表 where id='” + GridView1.DataKeys[e.RowIndex].Value.ToString() + “‘”;

        sqlcon=new SqlConnection(strCon);

        sqlcom=new SqlCommand(sqlstr,sqlcon);

        sqlcon.Open();

        sqlcom.ExecuteNonQuery();

        sqlcon.Close();

        bind();

    }

//更新

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

        sqlcon=new SqlConnection(strCon);

        string sqlstr=”update 表 set 字段1='”

            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + “‘,字段2='”

            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + “‘,字段3='”

            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + “‘ where id='”

            + GridView1.DataKeys[e.RowIndex].Value.ToString() + “‘”;

        sqlcom=new SqlCommand(sqlstr,sqlcon);

        sqlcon.Open();

        sqlcom.ExecuteNonQuery();

        sqlcon.Close();

        GridView1.EditIndex=-1;

        bind();

    }

//取消

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

    {

        GridView1.EditIndex=-1;

        bind();

    }

//绑定

    public void bind()

    {

        string sqlstr=”select * from 表”;

        sqlcon=new SqlConnection(strCon);

        SqlDataAdapter myda=new SqlDataAdapter(sqlstr, sqlcon);

        DataSet myds=new DataSet();

        sqlcon.Open();

        myda.Fill(myds, “表”);

        GridView1.DataSource=myds;

        GridView1.DataKeyNames=new string[] { “id” };//主键

        GridView1.DataBind();

        sqlcon.Close();

    }

}

前台主要代码:

                            … …

                        ForeColor=”#333333″ GridLines=”None” OnRowDeleting=”GridView1_RowDeleting” OnRowEditing=”GridView1_RowEditing”

                        OnRowUpdating=”GridView1_RowUpdating” OnRowCancelingEdit=”GridView1_RowCancelingEdit”>

3.GridView正反双向排序:

效果图:点姓名各2次的排序,点其他也一样可以。

后台代码:

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 Default3 : System.Web.UI.Page

{

//清清月儿的博客http://blog.csdn.net/21aspnet 

    SqlConnection sqlcon;

    string strCon=”Data Source=(local);Database=北风贸易;Uid=sa;Pwd=”;

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            ViewState[“SortOrder”]=”身份证号码”;

            ViewState[“OrderDire”]=”ASC”;

            bind();

        }

    }

    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;

        }

        bind();

    }

    public void bind()

    {

        string sqlstr=”select top 5 * from 飞狐工作室”;

        sqlcon=new SqlConnection(strCon);

        SqlDataAdapter myda=new SqlDataAdapter(sqlstr, sqlcon);

        DataSet myds=new DataSet();

        sqlcon.Open();

        myda.Fill(myds, “飞狐工作室”);

        DataView view=myds.Tables[“飞狐工作室”].DefaultView;

        string sort=(string)ViewState[“SortOrder”] + ” ” + (string)ViewState[“OrderDire”];

        view.Sort=sort;

        GridView1.DataSource=view;

        GridView1.DataBind();

        sqlcon.Close();

    }

}

前台主要代码:

                        CellPadding=”3″ Font-Size=”9pt” OnSorting=”GridView1_Sorting” BackColor=”White” BorderColor=”#CCCCCC” BorderStyle=”None” BorderWidth=”1px”>

                    </asp:GridView>

4.GridView和下拉菜单DropDownList结合:

效果图:

后台代码:

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 Default4 : System.Web.UI.Page

{

    SqlConnection sqlcon;

    string strCon=”Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa”;

    protected void Page_Load(object sender, EventArgs e)

    {

        DropDownList ddl;

        if (!IsPostBack)

        {

            string sqlstr=”select top 5 * from 飞狐工作室”;

            sqlcon=new SqlConnection(strCon);

            SqlDataAdapter myda=new SqlDataAdapter(sqlstr, sqlcon);

            DataSet myds=new DataSet();

            sqlcon.Open();

            myda.Fill(myds, “飞狐工作室”);

            GridView1.DataSource=myds;

            GridView1.DataBind();

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

            {

                DataRowView mydrv=myds.Tables[“飞狐工作室”].DefaultView[i];

                if (Convert.ToString(mydrv[“员工性别”]).Trim()==”True”)

                {

                    ddl=(DropDownList)GridView1.Rows[i].FindControl(“DropDownList1”);

                    ddl.SelectedIndex=0;

                }

                if (Convert.ToString(mydrv[“员工性别”]).Trim()==”False”)

                {

                    ddl=(DropDownList)GridView1.Rows[i].FindControl(“DropDownList1”);

                    ddl.SelectedIndex=1;

                }

            }

            sqlcon.Close();

        }

    }

    public SqlDataReader ddlbind()

    {

        string sqlstr=”select distinct 员工性别 from 飞狐工作室”;

        sqlcon=new SqlConnection(strCon);

        SqlCommand sqlcom=new SqlCommand(sqlstr, sqlcon);

        sqlcon.Open();

        return sqlcom.ExecuteReader();

    }

12下一页阅读全文
您可能感兴趣的文章:ASP.NET数据绑定之GridView控件灵活掌握asp.net中gridview控件的多种使用方法(下)灵活掌握asp.net中gridview控件的多种使用方法(上)asp.net GridView控件中模板列CheckBox全选、反选、取消asp.net下gridview 批量删除的实现方法Asp.net GridView使用大全(分页实现)asp.net gridview代码绑定asp.net GridView 删除时弹出确认对话框(包括内容提示)Asp.net简单代码设置GridView自适应列宽不变形实现思路与代码ASP.NET 2.0/3.5中直接操作Gridview控件插入新记录灵活掌握Asp.net MVC中GridView的使用方法

© 版权声明

相关文章