【轉】GridView 72般絕技(一)

iDotNetSpace發表於2010-04-08

快速預覽:
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
{

    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" nRowDeleting="GridView1_RowDeleting" nRowEditing="GridView1_RowEditing"
                        nRowUpdating="GridView1_RowUpdating" nRowCancelingEdit="GridView1_RowCancelingEdit">
                       
                       
                           
                           
                           
                           
                           
                           
                           
                       

                       
                       
                       
                       
                   

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-631621/,如需轉載,請註明出處,否則將追究法律責任。

相關文章