第四讲 GridView 72般绝技

时间:2022-02-08 08:53:07

C#精髓

第四讲 GridView 72般绝技

快速预览:
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无代码分页排序

效果图:
第四讲 GridView 72般绝技
1.AllowSorting设为True,aspx代码中是AllowSorting="True";
2.默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代码中是PageSize="12"。
3.默认的是单向排序的,右击GridView弹出“属性”,选择AllowSorting为True即可。


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

效果图:
第四讲 GridView 72般绝技

后台代码:
你可以使用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();
    }
}

前台主要代码:
                            ... ...
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                        ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
                        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                        <Columns>
                            <asp:BoundField DataField="身份证号码" HeaderText="用户ID" ReadOnly="True" />
                            <asp:BoundField DataField="姓名" HeaderText="用户姓名" />
                            <asp:BoundField DataField="员工性别" HeaderText="性别" />
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
                            <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                            <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>


3.GridView正反双向排序:
效果图:点姓名各2次的排序,点其他也一样可以。
第四讲 GridView 72般绝技第四讲 GridView 72般绝技
后台代码:
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();
    }
}

前台主要代码:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                        CellPadding="3" Font-Size="9pt" OnSorting="GridView1_Sorting" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                             <asp:BoundField DataField="身份证号码" HeaderText="用户ID" SortExpression="身份证号码" />
                            <asp:BoundField DataField="姓名" HeaderText="用户姓名" SortExpression="姓名"/>
                            <asp:BoundField DataField="员工性别" HeaderText="性别" SortExpression="员工性别"/>
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/>
                               
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>

 

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

效果图:
第四讲 GridView 72般绝技

后台代码:
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();
    }

前台主要代码:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                        CellPadding="3" Font-Size="9pt"  BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                             <asp:BoundField DataField="身份证号码" HeaderText="用户ID" SortExpression="身份证号码" />
                            <asp:BoundField DataField="姓名" HeaderText="用户姓名" SortExpression="姓名"/>
                            <asp:TemplateField HeaderText="员工性别">
                                <ItemTemplate>
                                    <asp:DropDownList ID="DropDownList1" runat="server" DataSource='<%# ddlbind()%>' DataValueField="员工性别" DataTextField="员工性别">
                                    </asp:DropDownList>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/>
                               
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>

5.GridView和CheckBox结合:

效果图:
第四讲 GridView 72般绝技

后台代码:

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 Default5 : System.Web.UI.Page
{
//清清月儿http://blog.csdn.net/21aspnet
    SqlConnection sqlcon;
    string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (CheckBox2.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        sqlcon = new SqlConnection(strCon);
        SqlCommand sqlcom;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {

                string sqlstr = "delete from 飞狐工作室 where 身份证号码='" + GridView1.DataKeys[i].Value + "'";
                sqlcom = new SqlCommand(sqlstr, sqlcon);
                sqlcon.Open();
                sqlcom.ExecuteNonQuery();
                sqlcon.Close();
            }
        }
        bind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CheckBox2.Checked = false;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            cbox.Checked = false;
        }
    }
    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, "tb_Member");
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "身份证号码" };
        GridView1.DataBind();
        sqlcon.Close();
    }
}

前台主要代码:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                        CellPadding="3" Font-Size="9pt"  BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                             <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                             <asp:BoundField DataField="身份证号码" HeaderText="用户ID" SortExpression="身份证号码" />
                            <asp:BoundField DataField="姓名" HeaderText="用户姓名" SortExpression="姓名"/>
                           
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/>
                               
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>
                     <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" OnCheckedChanged="CheckBox2_CheckedChanged"
                        Text="全选" />
                    <asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="取消" OnClick="Button1_Click" />
                    <asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="删除" OnClick="Button2_Click" />

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

效果图:
第四讲 GridView 72般绝技
做法:
双击GridView的OnRowDataBound事件;
在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int i;
        //执行循环,保证每条数据都可以更新
        for (i = 0; i < GridView1.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

    }

前台代码:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>实现鼠标划过改变GridView的行背景色 清清月儿http://blog.csdn.net/21aspnet </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="身份证号码"
            DataSourceID="SqlDataSource1" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="身份证号码" HeaderText="身份证号码" ReadOnly="True" SortExpression="身份证号码" />
                <asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" />
                <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址" />
                <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" SortExpression="邮政编码" />
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <RowStyle ForeColor="#000066" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:北风贸易ConnectionString1 %>"
            SelectCommand="SELECT top 5 [身份证号码], [姓名], [员工性别], [家庭住址], [邮政编码] FROM [飞狐工作室]" DataSourceMode="DataReader"></asp:SqlDataSource>
   
    </div>
    </form>
</body>
</html>

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

效果图:
第四讲 GridView 72般绝技

做法:和上面的一样就是代码不同
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //int i;
        ////执行循环,保证每条数据都可以更新
        //for (i = 0; i < GridView1.Rows.Count; i++)
        //{
        //    //首先判断是否是数据行
        //    if (e.Row.RowType == DataControlRowType.DataRow)
        //    {
        //        //当鼠标停留时更改背景色
        //        e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
        //        //当鼠标移开时还原背景色
        //        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        //    }
        //}
        //如果是绑定数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标经过时,行背景色变
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
            //鼠标移出时,行背景色变
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }

    }

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

效果图:
第四讲 GridView 72般绝技

实现方法:
双击GridView的OnRowDataBound事件;
在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //如果是绑定数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
             if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:/"" + e.Row.Cells[1].Text + "/"吗?')");
            }
        }

    }

9.GridView实现自动编号:

效果图:
第四讲 GridView 72般绝技

实现方法:
双击GridView的OnRowDataBound事件;
在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //如果是绑定数据行 //清清月儿http://blog.csdn.net/21aspnet 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ////鼠标经过时,行背景色变
            //e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
            ////鼠标移出时,行背景色变
            //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");

            ////当有编辑列时,避免出错,要加的RowState判断
            //if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            //{
            //    ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:/"" + e.Row.Cells[1].Text + "/"吗?')");
            //}

        }
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[0].Text = id.ToString();
        }

    }

 

注意这时最好把前台的第一列的表头该为“编号”,因为以前的第一列被“吃掉”了。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound">
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                            <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
                            <asp:BoundField DataField="姓名" HeaderText="用户姓名" />
                            <asp:BoundField DataField="员工性别" HeaderText="性别" />
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
                            <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                            <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>

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

效果图:
图1-未格式化前
第四讲 GridView 72般绝技
图2-格式化后
第四讲 GridView 72般绝技
解决方法:

在asp.net 2.0中,如果要在绑定列中显示比如日期格式等,如果用下面的方法是显示不了的

<asp :BoundField DataField="CreationDate"
DataFormatString="{0:M-dd-yyyy}"
HeaderText="CreationDate" />

主要是由于htmlencode属性默认设置为true,已防止XSS攻击,安全起见而用的,所以,可以有以下两种方法解决

1、

<asp :GridView ID="GridView1" runat="server">
<columns>
<asp :BoundField DataField="CreationDate"
DataFormatString="{0:M-dd-yyyy}"
HtmlEncode="false"
HeaderText="CreationDate" />
</columns>
</asp>

将htmlencode设置为false即可

另外的解决方法为,使用模版列

<asp :GridView ID="GridView3" runat="server" >
<columns>
<asp :TemplateField HeaderText="CreationDate" >
<edititemtemplate>
<asp :Label ID="Label1" runat="server"
Text='<%# Eval("CreationDate", "{0:M-dd-yyyy}") %>'>
</asp>
</edititemtemplate>
<itemtemplate>
<asp :Label ID="Label1" runat="server"
Text=’<%# Bind("CreationDate", "{0:M-dd-yyyy}") %>'>
</asp>
</itemtemplate>
</asp>
</columns>
</asp>

前台代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="身份证号码"
            DataSourceID="SqlDataSource1" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="身份证号码" HeaderText="身份证号码" ReadOnly="True" SortExpression="身份证号码" />
                <asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" />
                <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" SortExpression="邮政编码" />
                <asp:BoundField DataField="出生日期" HeaderText="出生日期" SortExpression="出生日期" />
                <asp:BoundField DataField="起薪" HeaderText="起薪" SortExpression="起薪" />
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <RowStyle ForeColor="#000066" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:北风贸易ConnectionString1 %>"
            SelectCommand="SELECT top 5 [出生日期], [起薪], [身份证号码], [姓名], [家庭住址], [邮政编码] FROM [飞狐工作室]" DataSourceMode="DataReader"></asp:SqlDataSource>

附录-常用格式化公式:
{0:C}  货币;
{0:D4}由0填充的4个字符宽的字段中显示整数;
{0:000.0}四舍五入小数点保留第几位有效数字;
{0:N2}小数点保留2位有效数字;{0:N2}%   小数点保留2位有效数字加百分号;
{0:D}长日期;{0:d}短日期;{0:yy-MM-dd}   例如07-3-25;;{0:yyyy-MM-dd}  例如2007-3-25

 11.GridView实现用“...”代替超长字符串:

效果图:
第四讲 GridView 72般绝技
解决方法:数据绑定后过滤每一行即可
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            DataRowView mydrv;
            string gIntro;
            if (GridView1.PageIndex == 0)
            {
                mydrv = myds.Tables["飞狐工作室"].DefaultView[i];//表名
                gIntro = Convert.ToString(mydrv["家庭住址"]);//所要处理的字段
                GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
            }
            else
            {
                mydrv = myds.Tables["飞狐工作室"].DefaultView[i + (5 * GridView1.PageIndex)];
                gIntro = Convert.ToString(mydrv["家庭住址"]);
                GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
            }
        } 

调用的方法:

    public string SubStr(string sString, int nLeng)
    {
        if (sString.Length <= nLeng)
        {
            return sString;
        }
        string sNewStr = sString.Substring(0, nLeng);
        sNewStr = sNewStr + "...";
        return sNewStr;
    }

后台全部代码:
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=sa;Pwd=sa";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SortOrder"] = "身份证号码";
            ViewState["OrderDire"] = "ASC";
            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 身份证号码='" + 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 姓名='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='"
            + 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 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.DataKeyNames = new string[] { "身份证号码" };
        GridView1.DataBind();
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            DataRowView mydrv;
            string gIntro;
            if (GridView1.PageIndex == 0)
            {
                mydrv = myds.Tables["飞狐工作室"].DefaultView[i];
                gIntro = Convert.ToString(mydrv["家庭住址"]);
                GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
            }
            else
            {
                mydrv = myds.Tables["飞狐工作室"].DefaultView[i + (5 * GridView1.PageIndex)];
                gIntro = Convert.ToString(mydrv["家庭住址"]);
                GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
            }
        }
       
        sqlcon.Close();
    }
    public string SubStr(string sString, int nLeng)
    {
        if (sString.Length <= nLeng)
        {
            return sString;
        }
        string sNewStr = sString.Substring(0, nLeng);
        sNewStr = sNewStr + "...";
        return sNewStr;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ////鼠标经过时,行背景色变
            //e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
            ////鼠标移出时,行背景色变
            //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");

            ////当有编辑列时,避免出错,要加的RowState判断
            //if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            //{
            //    ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:/"" + e.Row.Cells[1].Text + "/"吗?')");
            //}

        }
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[0].Text = id.ToString();
        }

    }
}

 11.GridView一般换行与强制换行:

效果图:
第四讲 GridView 72般绝技
首先设置<asp:BoundField DataField="家庭住址" HeaderText="家庭住址"  ItemStyle-Width="100" />
gridview里有一列绑定的数据很长,显示的时候在一行里面显示,页面拉得很宽。
原因是连续英文段为一个整体导致的,在RowDataBound中添加上了一句e.Row.Cells[2].Style.Add("word-break", "break-all")就可以。

如果要给所有的列增加此属性:
 protected void Page_Load(object sender, EventArgs e)
    {
        //正常换行
        GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
        //下面这行是自动换行
        GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
        if (!IsPostBack)
        {
             bind();//调用数据绑定即可
        }
    }
总之:善用CSS的word-break:break-all;word-wrap:break-word属性即可,这个属性是通用的对于顽固的南换行问题都可以解决,不局限于GridView。

 12.GridView显示隐藏某一列:
本方案为月儿独创,不同于网上其他方式,我觉得用一个CheckBox更人性化,这样可以隐藏不必要的列,让用户自己选择需要出现的列,在处理多列时这是一个很好的解决方案!

效果图:
图1-开始
第四讲 GridView 72般绝技
图2-点击显示的CheckBox后
第四讲 GridView 72般绝技

解决方案:
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, "飞狐工作室");
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "身份证号码" };
        GridView1.DataBind();
        sqlcon.Close();
        GridView1.Columns[3].Visible = false;//一开始隐藏
        CheckBox1.Checked = false;//如果不这样后面的代码会把他True

    }

双击CheckBox1,在CheckedChanged方法里写上代码,最后代码如下:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
         GridView1.Columns[3].Visible=! GridView1.Columns[3].Visible;
         Response.Write("GridView1的第4列现在的显示隐藏状态是:"+GridView1.Columns[3].Visible.ToString());
    }

注意:CheckBox1的AutoPostBack要True

后台全部代码如下:

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=sa;Pwd=sa";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SortOrder"] = "身份证号码";
            ViewState["OrderDire"] = "ASC";
            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 身份证号码='" + 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 姓名='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='"
            + 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 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.DataKeyNames = new string[] { "身份证号码" };
        GridView1.DataBind();
        sqlcon.Close();
        GridView1.Columns[3].Visible = false;
        CheckBox1.Checked = false;
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
         GridView1.Columns[3].Visible=! GridView1.Columns[3].Visible;
         Response.Write("GridView1的第4列现在的显示隐藏状态是:"+GridView1.Columns[3].Visible.ToString());
    }
}

前台代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>GridView显示隐藏列 清清月儿http://blog.csdn.net/21aspnet </title>
</head>
<body style="font-size=12px">
    <form id="form1" runat="server">
    <div>
                   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px"  >
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                            <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
                            <asp:BoundField DataField="姓名" HeaderText="用户姓名" />
                            <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" SortExpression="邮政编码" />
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址"  />
                            <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                            <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>
        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" Font-Size="12px"
            OnCheckedChanged="CheckBox1_CheckedChanged" Text="显示隐藏家庭住址" /></div>
    </form>
</body>
</html>

 13.GridView弹出新页面/弹出制定大小位置新窗口:

效果图:
第四讲 GridView 72般绝技

方案一:简单的方法,新窗口不固定大小
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px"  >
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                            <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
                            <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" SortExpression="邮政编码" />
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址"  />
                            <asp:HyperLinkField HeaderText="姓名" Text="姓名" DataNavigateUrlFields="姓名" DataNavigateUrlFormatString="Default6.aspx?GoodsID={0}" Target="mainframe" NavigateUrl="~/Default6.aspx" DataTextField="姓名" >
                    </asp:HyperLinkField>
                            <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                            <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>
DataNavigateUrlFields是链接的字段名,DataNavigateUrlFormatString是路径。

方案二:精确控制弹出窗口大小位置
<asp:HyperLinkColumn DataNavigateUrlField="EmployeeID" DataNavigateUrlFormatString="javascript:varwin=window.open('detail.aspx?ID={0}',null,'width=300,height=200');window.Close();"
       DataTextField="LastName" HeaderText="LastName"></asp:HyperLinkColumn>

使用的是结合javascript的window.open方法,关于window.open的参数网上有很多帖子,本站也有许多参考
弹出窗口大全 http://blog.csdn.net/21aspnet/archive/2004/10/25/150231.aspx   即可!

  14.GridView固定表头(不用javascript只用CSS!,很好用)

效果图:
第四讲 GridView 72般绝技

代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>GridView固定表头 清清月儿http://blog.csdn.net/21aspnet </title>
        <style>
.Freezing
   {
   
   position:relative ;
   table-layout:fixed;
   top:expression(this.offsetParent.scrollTop);  
   z-index: 10;
   }

.Freezing th{text-overflow:ellipsis;overflow:hidden;white-space: nowrap;padding:2px;}
</style>
</head>
<body style="font-size=12px">
    <form id="form1" runat="server">
    <div style="overflow-y: scroll; height: 200px;width:300px" id="dvBody">
                   <asp:GridView ID="GridView1" runat="server"    AutoGenerateColumns="False" CellPadding="3" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowCreated="GridView1_RowCreated"  >
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                            <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
                            <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" SortExpression="邮政编码" />
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址"  />
                            <asp:BoundField DataField="姓名" HeaderText="姓名"  />
                           
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"  CssClass="ms-formlabel DataGridFixedHeader"/>
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" CssClass="Freezing"/>
                    </asp:GridView>
        </div>

    </form>
</body>
</html>

用法:CSS设如上的样式,HeaderStyle加CssClass="Freezing,套住GridView的Div设置高度宽度 <div style="overflow-y: scroll; height: 200px;width:200px" >

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

效果图:
第四讲 GridView 72般绝技


后台代码:
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;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
    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_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        sqlcon = new SqlConnection(strCon);
        string sqlstr = "update 飞狐工作室 set 姓名='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='"
            + 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 top 10 * 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[] { "身份证号码" };
        GridView1.DataBind();
        sqlcon.Close();
    }

//这里就是解决方案
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        switch (e.Row.RowType)
        {
            case DataControlRowType.Header:
                //第一行表头
                TableCellCollection tcHeader = e.Row.Cells;
                tcHeader.Clear();
                tcHeader.Add(new TableHeaderCell());
                tcHeader[0].Attributes.Add("rowspan", "3"); //跨Row
                tcHeader[0].Attributes.Add("bgcolor", "white");
                tcHeader[0].Text = "";
                tcHeader.Add(new TableHeaderCell());
                //tcHeader[1].Attributes.Add("bgcolor", "Red");
                tcHeader[1].Attributes.Add("colspan", "6"); //跨Column
                tcHeader[1].Text = "全部信息</th></tr><tr>";

                //第二行表头
                tcHeader.Add(new TableHeaderCell());
                tcHeader[2].Attributes.Add("bgcolor", "DarkSeaGreen");
                tcHeader[2].Text = "身份证号码";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[3].Attributes.Add("bgcolor", "LightSteelBlue");
                tcHeader[3].Attributes.Add("colspan", "2");
                tcHeader[3].Text = "基本信息";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[4].Attributes.Add("bgcolor", "DarkSeaGreen");
                tcHeader[4].Text = "福利";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[5].Attributes.Add("bgcolor", "LightSteelBlue");
                tcHeader[5].Attributes.Add("colspan", "2");
                tcHeader[5].Text = "联系方式</th></tr><tr>";

                //第三行表头
                tcHeader.Add(new TableHeaderCell());
                tcHeader[6].Attributes.Add("bgcolor", "Khaki");
                tcHeader[6].Text = "身份证号码";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[7].Attributes.Add("bgcolor", "Khaki");
                tcHeader[7].Text = "姓名";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[8].Attributes.Add("bgcolor", "Khaki");
                tcHeader[8].Text = "出生日期";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[9].Attributes.Add("bgcolor", "Khaki");
                tcHeader[9].Text = "薪水";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[10].Attributes.Add("bgcolor", "Khaki");
                tcHeader[10].Text = "家庭住址";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[11].Attributes.Add("bgcolor", "Khaki");
                tcHeader[11].Text = "邮政编码";
                break;
        }
    }
}

前台:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>GridView合并多重表头表头 清清月儿http://blog.csdn.net/21aspnet </title>
</head>
<body >
    <form id="form1" runat="server">
    <div  >
                   <asp:GridView ID="GridView1" runat="server"    AutoGenerateColumns="False" CellPadding="3"  OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowCreated="GridView1_RowCreated"  >
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                            <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
                            <asp:BoundField DataField="姓名" HeaderText="姓名"  />
                            <asp:BoundField DataField="出生日期" HeaderText="邮政编码"  />
                             <asp:BoundField DataField="起薪" HeaderText="起薪"  />
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址"  />
                            <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
                          
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"  CssClass="ms-formlabel DataGridFixedHeader"/>
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>
        </div>

    </form>
</body>
</html>

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

效果图:
第四讲 GridView 72般绝技

解决方案:主要是绑定后过滤
 GridView1.DataBind();
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            DataRowView mydrv = myds.Tables["飞狐工作室"].DefaultView[i];
            string score = Convert.ToString(mydrv["起薪"]);
            if (Convert.ToDouble(score) < 34297.00)//大家这里根据具体情况设置可能ToInt32等等
            {
                GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;
            }
        }
        sqlcon.Close();

全部后台代码:

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;
using System.Drawing;

public partial class Default7 : System.Web.UI.Page
{
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
    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_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        sqlcon = new SqlConnection(strCon);
        string sqlstr = "update 飞狐工作室 set 姓名='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='"
            + 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 top 10 * 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[] { "身份证号码" };
        GridView1.DataBind();
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            DataRowView mydrv = myds.Tables["飞狐工作室"].DefaultView[i];
            string score = Convert.ToString(mydrv["起薪"]);
            if (Convert.ToDouble(score) < 34297.00)//大家这里根据具体情况设置可能ToInt32等等

            {
                GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;
            }
        }
        sqlcon.Close();
    }
}

前台代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>GridView突出显示某一单元格 清清月儿http://blog.csdn.net/21aspnet </title>
</head>
<body >
    <form id="form1" runat="server">
    <div  >
                   <asp:GridView ID="GridView1" runat="server"    AutoGenerateColumns="False" CellPadding="3"  OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px"  >
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                            <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
                            <asp:BoundField DataField="姓名" HeaderText="姓名"  />
                            <asp:BoundField DataField="出生日期" HeaderText="邮政编码"  />
                             <asp:BoundField DataField="起薪" HeaderText="起薪"  DataFormatString="{0:C}" HtmlEncode="false"/>
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址"  />
                            <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
                          
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"  CssClass="ms-formlabel DataGridFixedHeader"/>
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>
        </div>

    </form>
</body>
</html>

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

效果图:第四讲 GridView 72般绝技
解决方案:    
private double sum = 0;//取指定列的数据和,你要根据具体情况对待可能你要处理的是int
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
        if (e.Row.RowIndex >= 0)
        {
            sum += Convert.ToDouble(e.Row.Cells[6].Text);
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[5].Text = "总薪水为:";
            e.Row.Cells[6].Text = sum.ToString();
            e.Row.Cells[3].Text = "平均薪水为:";
            e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
           
        }
    }

后台全部代码:
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;
using System.Drawing;

public partial class Default7 : System.Web.UI.Page
{
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
    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_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        sqlcon = new SqlConnection(strCon);
        string sqlstr = "update 飞狐工作室 set 姓名='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='"
            + 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 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.DataKeyNames = new string[] { "身份证号码" };
        GridView1.DataBind();
        sqlcon.Close();
    }
    private double sum = 0;//取指定列的数据和
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
        if (e.Row.RowIndex >= 0)
        {
            sum += Convert.ToDouble(e.Row.Cells[6].Text);
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[5].Text = "总薪水为:";
            e.Row.Cells[6].Text = sum.ToString();
            e.Row.Cells[3].Text = "平均薪水为:";
            e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
           
        }
    }
}
前台:唯一的花头就是设置ShowFooter="True" ,否则默认表头为隐藏的!
<asp:GridView ID="GridView1" runat="server"    AutoGenerateColumns="False" CellPadding="3"  OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True"  >
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                            <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
                            <asp:BoundField DataField="姓名" HeaderText="姓名"  />
                            <asp:BoundField DataField="出生日期" HeaderText="邮政编码"  />
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址"  />
                            <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
                            <asp:BoundField DataField="起薪" HeaderText="起薪"  />
                          
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"  CssClass="ms-formlabel DataGridFixedHeader"/>
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>

 

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

效果图:
第四讲 GridView 72般绝技

解决方案:
页面增加一个按钮,单击事件添加如下方法:
protected void Button1_Click(object sender, EventArgs e)
    {
        Export("application/ms-excel", "学生成绩报表.xls");
    }

    private void Export(string FileType, string FileName)
    {
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        GridView1.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }
//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
   
public override void VerifyRenderingInServerForm(Control control)
    {
    }
还有由于是文件操作所以要引入名称空间IO和Text

后台代码:
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;
using System.Drawing;
using System.IO;
using System.Text;
public partial class Default7 : System.Web.UI.Page
{
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            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, "飞狐工作室");
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "身份证号码" };
        GridView1.DataBind();
        sqlcon.Close();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Export("application/ms-excel", "学生成绩报表.xls");
    }

    private void Export(string FileType, string FileName)
    {
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        GridView1.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
   
}

前台
<asp:GridView ID="GridView1" runat="server"    AutoGenerateColumns="False" CellPadding="3" 
                         BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px"   >
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                            <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
                            <asp:BoundField DataField="姓名" HeaderText="姓名"  />
                            <asp:BoundField DataField="出生日期" HeaderText="邮政编码"  />
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址"  />
                            <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
                            <asp:BoundField DataField="起薪" HeaderText="起薪"  />
                          
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"  CssClass="ms-formlabel DataGridFixedHeader"/>
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" />

读取Excel数据的代码:这个很简单的
private DataSet CreateDataSource()
    {
        string strCon;
        strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("excel.xls") + "; Extended Properties=Excel 8.0;";
        OleDbConnection olecon = new OleDbConnection(strCon);
        OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
        DataSet myds = new DataSet();
        myda.Fill(myds);
        return myds;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = CreateDataSource();
        GridView1.DataBind();
    }

由于时间关系,这个文章先到此。有时间再写,其实还有很多技巧,不过我觉得如果能融汇贯通应该可以举一反三

 



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1540301


[收藏到我的网摘]   [发送Trackback]  清清月儿发表于 2007年03月25日 04:36:00
相关文章:

特别推荐:想在这里投放广告?点击查看详情 第四讲 GridView 72般绝技 第四讲 GridView 72般绝技 上一篇: ASP.NET2.0轻松搞定统计图表【月儿原创】  | 下一篇: C#精髓【月儿原创】第三讲 C#泛型有什么好处

评论

#  Zeilg1981 发表于2007-03-26 09:24:12  IP: 61.170.206.*
辛苦了~收藏

#  wenwenti2 发表于2007-03-26 09:53:52  IP: 222.212.103.*
高手啊.汗~~~~~~~~~~

#  iloveaspx 发表于2007-03-26 10:06:11  IP: 221.192.177.*
厉害的不得了,珍藏一下哦

#  QQ272821361 发表于2007-03-26 10:16:47  IP: 60.173.224.*
今天幸运了,碰到了个高手了,以后要多多指教啊!!

#  QQ272821361 发表于2007-03-26 11:04:37  IP: 60.173.224.*
再顶下!!!

收获不小...

#  wt_3333 发表于2007-03-26 12:34:21  IP: 222.38.97.*
向高手致敬

#  强子 发表于2007-03-26 12:53:22  IP: 218.104.39.*
谢谢了!

#  delphigbg 发表于2007-03-26 12:56:02  IP: 218.247.53.*
发现宝藏了.

#  guomaomao1981 发表于2007-03-26 13:04:54  IP: 218.12.56.*
收藏,lz辛苦了。

#  sdtsfhh 发表于2007-03-26 13:31:11  IP: 221.8.56.*
编码狂人

#  showrock 发表于2007-03-26 14:07:53  IP: 59.108.6.*
真系太厉害啦,收藏

#  chieftech 发表于2007-03-26 14:14:16  IP: 60.24.168.*
天啊!你太有才了@@@

#  C#1983 发表于2007-03-26 14:19:44  IP: 221.219.6.*
向编码写作者 致敬 

#  C#1983 发表于2007-03-26 14:22:09  IP: 221.219.6.*
向高手学习

#  gehantao 发表于2007-03-26 14:31:48  IP: 210.77.134.*
受益了,收藏!

#  Andy 发表于2007-03-26 14:41:58  IP: 218.59.67.*
数据读取却如此垃圾,海量数据试试

#  ztchen 发表于2007-03-26 14:46:26  IP: 222.66.8.*
月儿辛苦,做得不错,继续努力吧,呵呵。

#  spsx99vj 发表于2007-03-26 14:47:33  IP: 125.32.26.*
辛苦了,继续努力.

#  21aspnet 发表于2007-03-26 14:56:33  IP: 220.234.107.*
海量数据读取用存储过程的,这样的存储过程很多啊,本文只是举例啊,你可以灵活应用啊。

#  21aspnet 发表于2007-03-26 14:59:25  IP: 220.234.107.*
不过还是谢谢你提醒大家,大家如果有大量的数据用高效分页的存储过程等。这样更完美。

本文其实主要讲页面展现这个。

#  sualang 发表于2007-03-26 15:01:48  IP: 58.217.175.*
# Andy 发表于2007-03-26 14:41:58 IP: 218.59.67.*
数据读取却如此垃圾,海量数据试试
===============================================

如果有几十万条数据你也一次都读出来?

无语!

#  ditto 发表于2007-03-26 15:33:00  IP: 219.137.200.*
对于初学者,是比较好的入门指导

#  coolyhtao 发表于2007-03-26 16:10:06  IP: 221.220.191.*
辛苦了,严重感谢

#  yejianhua 发表于2007-03-26 20:00:32  IP: 61.183.172.*
索引超出范围。必须为非负值并小于集合大小。
参数名: index
string sqlstr = "update GridView set 姓名='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',员工性别='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString().Trim() + "',家庭住址='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString().Trim() + "'where 身份证号码='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
表结构
身份证号码 姓名 员工性别 家庭住址

#  菜鸟 发表于2007-03-26 23:16:27  IP: 61.159.245.*
复杂表头的字段排序怎么做呀?

#  foshan 发表于2007-03-27 08:28:35  IP: 218.13.85.*
谢谢!
用了 GridView固定表头 之后,表头各列的分隔线没有了,如何保留各列之间的分隔线啊???

#  mingyueqingfeng 发表于2007-03-27 11:22:57  IP: 218.28.136.*
谢谢,学习中

#  cyyu_ryh 发表于2007-03-27 11:43:50  IP: 222.177.14.*
强悍

#  netharry 发表于2007-03-27 13:12:50  IP: 221.220.203.*
谢谢

#  lpc444 发表于2007-03-27 13:15:17  IP: 59.34.101.*
很好!学习了。

#  wl2000wh 发表于2007-03-27 13:33:16  IP: 219.134.236.*
只能是佩服啦!!!!!!!!!!!!!

#  wjbo2006 发表于2007-03-27 16:57:06  IP: 222.66.84.*
导Excel的时候,碰到数字的时候如果长度太大,需要加$
楼主好想没有注意到?1.0001E+1.7

#  wjbo2006 发表于2007-03-27 16:57:55  IP: 222.66.84.*
学习了很多东西,非常感谢

#  C 发表于2007-03-28 09:10:20  IP: 218.12.94.*
辛苦了

#  pigmaster 发表于2007-03-28 10:12:01  IP: 218.249.35.*
不管数据提取,只需要这些技能就足够了

#  kxm_168 发表于2007-03-28 15:32:26  IP: 61.232.2.*
楼主:牛B啊!!!!!

#  webhermit 发表于2007-03-28 19:02:25  IP: 202.120.209.*
不顶不是人了!

#  huahuahai 发表于2007-03-28 19:34:23  IP: 220.234.205.*
其实你的前两讲出来时我并没有觉得你有多厉害呢,现在才发现,真的是没话说。呵呵呵!!!

#  ganlanshu_9447 发表于2007-03-29 10:29:34  IP: 210.83.223.*
好厉害呀

#  qilinshu 发表于2007-03-29 16:06:01  IP: 211.144.104.*
首先要感谢楼主.但还是补充一下,楼主有些地方还是简略了些.例如第一个gridview无代码分页. 除了控件设置外,在代码中还是要写程序的,不然你除了第一页外其他页都无法显示.

#  guest 发表于2007-03-30 09:55:59  IP: 221.219.118.*
感谢楼主!

#  cainiao 发表于2007-03-30 10:24:37  IP: 222.172.147.*
讲讲 reporting service吧,密切关注

#  sealeft 发表于2007-03-30 16:32:10  IP: 218.6.230.*
太高了
仰望

#  liucan 发表于2007-04-02 11:23:43  IP: 218.18.115.*
O...8错..顶了先....学习......有些人别太苛刻了..靠..这些都是DEMO吗.学习学习..有足的大家都相互补充吗...靠...你们能否写出来....草..没点职业精神.

#  hu_bird 发表于2007-04-03 09:19:01  IP: 222.182.95.*
高手阿,楼主辛苦了。至于海量数据就没有必要较真,至少对我这种初学者来说已经够用了,如果搞得太复杂了反而让菜鸟看不懂了。

#  zhang 发表于2007-04-03 19:07:02  IP: 124.114.131.*
xiexie

#  wqm_44944 发表于2007-04-05 13:24:08  IP: 60.216.11.*
谢谢

#  read 发表于2007-04-05 15:42:24  IP: 222.92.4.*
非常的好,有貢獻精神。。
敬佩

#  zhengmx 发表于2007-04-07 13:46:31  IP: 59.34.18.*
好东西啊,学习了...

#  zhengmx 发表于2007-04-07 13:48:35  IP: 59.34.18.*
楼主这里.net的资料很全面啊...呵呵,看来以后要多来学习了....

#  systemLife 发表于2007-04-10 15:45:16  IP: 211.140.202.*
我对你的景仰有如滔滔江水,连绵不绝...绝..绝.

#  qy3730201 发表于2007-04-11 10:52:09  IP: 202.103.49.*
您太有才了~~~~~

#  Animatrix 发表于2007-04-11 16:02:08  IP: 210.22.108.*
13.GridView弹出新页面/弹出制定大小位置新窗口:
这一段里面的第2种方法好象不行。呵呵,GRIDVIEW里好象没HyperLinkColumn吧?

#  LOVE&gt;&gt;.NET 发表于2007-04-11 16:43:03  IP: 58.63.47.*
太好了,谢谢你为我们的付出,谢谢啊!

#  Small_Huge 发表于2007-04-12 12:44:25  IP: 221.221.39.*
向高手致敬!!!!!!!!

#  skunk 发表于2007-04-12 16:16:18  IP: 219.149.220.*
谢谢

#  Mark 发表于2007-04-18 02:35:16  IP: 125.171.133.*
导出EXCL那段代码提示 当前上下文中不存在名称“Encoding”

忘解答maihuasen@163.com

#  青春发梢 发表于2007-04-18 09:45:11  IP: 125.123.72.*
高手,菜鸟向你致敬

#  过客 发表于2007-04-18 21:10:08  IP: 58.49.64.*
不的不顶呀!我汗!!!厉害!

#  学习中  发表于2007-04-19 08:45:50  IP: 59.191.125.*
感谢分享,本人收藏了!希望写出更多的文章!

#  无名 发表于2007-04-20 16:56:09  IP: 121.34.155.*
谢谢.....您........我是初学,,,,,看了这些让我有了新的认识!

#  zp89850 发表于2007-04-20 17:45:01  IP: 221.122.59.*
看完后 感觉不说声谢谢心里像少点什么似的。
多谢阿,

#  无名 发表于2007-04-21 00:17:21  IP: 121.34.153.*
看了这些技巧,我很感谢楼主.

在测试的过程中发现个问题想请教下:
在这一项里面:
2.GridView选中,编辑,取消,删除:
如:
用户编号 用户类型 审核状态 推荐排名 编辑
2001 普通个人会员 已审核 1 编辑
2003 普通个人会员 已审核 2 编辑
1020 普通个人会员 已审核 3 编辑

这是:点击"编辑"后的
用户编号 用户类型 审核状态 推荐排名 编辑
2001 普通个人会员 已审核 1 更新 取消
2003 普通个人会员 已审核 2 更新 取消
1020 普通个人会员 已审核 3 更新 取消

这里是原始值
推荐排名
3 当我改成300后,,,点击更新.....但程序里面的:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Response.Write(((TextBox)(GridViewUser.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim());
Response.End();
}
输出的值还是3而不是我改后的300....不知道是什么原因,还请赐教...谢谢

    
2007-04-21 01:16:02作者回复:
你理解似乎有问题 你点击更新以后并未做更新页面postback马上就刷回去了,所以必须更新后存数据库里。因为页面是服务器端事件,如果javascript事件就可。


#  无名:更正以上代码::::: 发表于2007-04-21 10:24:10  IP: 121.34.153.*
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Response.Write(((TextBox)(GridViewUser.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim());
Response.End();
}

#  无名:那我该怎么解决这个问题呢? 发表于2007-04-21 10:26:50  IP: 121.34.153.*
2007-04-21 01:16:02作者回复:
你理解似乎有问题 你点击更新以后并未做更新页面postback马上就刷回去了,所以必须更新后存数据库里。因为页面是服务器端事件,如果javascript事件就可。


我该怎么写.........谢谢...

#  无名:那我该怎么解决这个问题呢?  发表于2007-04-21 10:35:09  IP: 121.34.153.*
我的程序如下:
后台代码:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Response.Write(((TextBox)(GridViewUser.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim());
Response.End();
IMYSUN.UserEditInfo useredit = new IMYSUN.UserEditInfo();
useredit.Sorts = ((TextBox)(GridViewUser.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim();
useredit.Email = GridViewUser.DataKeys[e.RowIndex].Value.ToString();
useredit.addType = "edit";
IMYSUN.IMYSUN usedit = new IMYSUN.IMYSUN();

usedit.UserEdit(useredit);
GridViewUser.EditIndex = -1;
this.Get_AllUser();}


/// </summary>
public class UserEditInfo
{
public string Email;
public string UserNO;
public string Sorts;
public string UserName;
public string About;
public string addType;
}



/// <summary>
/// 编辑用户
/// </summary>
/// <param name="UserID"></param>
public void UserEdit(UserEditInfo user)
{
///定义类SQLHelper
SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
//user.Sorts=Convert(int,(user.Sorts));
///创建访问数

#  无名:那我该怎么解决这个问题呢? 续:::: 发表于2007-04-21 10:38:06  IP: 121.34.153.*
/// <summary>
/// 编辑用户
/// </summary>
/// <param name="UserID"></param>
public void UserEdit(UserEditInfo user)
{
///定义类SQLHelper
SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
//user.Sorts=Convert(int,(user.Sorts));
///创建访问数据库的参数
SqlParameter[] paramList =
{
sqlHelper.CreateInParam("@addType", SqlDbType.Char,10,user.addType),
sqlHelper.CreateInParam("@Email", SqlDbType.NVarChar,50,user.Email),
//sqlHelper.CreateInParam("@UserName", SqlDbType.NVarChar,50,user.UserName),
//sqlHelper.CreateInParam("@About", SqlDbType.VarChar,5000,user.About),
sqlHelper.CreateInParam("@Sorts", SqlDbType.Int,4,user.Sorts)
};

try
{
///执行存储过程
sqlHelper.RunProc("UserEdit", paramList);
}
catch (Exception ex)
{
///抛出执行数据库异常
SystemError.CreateErrorLog(ex.Message);
throw new Exception(ex.Message, ex);

}
finally
{
sqlHelper.Close();
}
}
存储过程:
CREATE PROCEDURE UserEd

#  无名:那我该怎么解决这个问题呢? 续:::: 发表于2007-04-21 10:38:59  IP: 121.34.153.*
CREATE PROCEDURE UserEdit
(
@addType char(10),
@Email nvarchar(50),
@Sorts int
)
AS
BEGIN
DECLARE @UserID nvarchar(50)
SELECT @UserID=UserID
FROM [User]
WHERE Email=@Email

UPDATE [UserInfo] SET Sorts=8000 WHERE Sorts=@Sorts
UPDATE [UserInfo] SET Sorts=@Sorts
WHERE UserID=@UserID
END
GO

谢谢..........

#  月 发表于2007-04-21 11:38:57  IP: 202.167.248.*
(((TextBox)(GridViewUser.Rows[e.RowIndex].Cells[6].Controls[0])).
=======================
(((TextBox)(GridViewUser.Rows[e.RowIndex].Cells[6].Controls[0])).
(((TextBox)(GridViewUser.Rows[e.RowIndex].Cells[6].Controls[1])).对应不同的

#  月 发表于2007-04-21 11:40:23  IP: 202.167.248.*
那我该怎么解决这个问题呢
================
//更新
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();
}


#  无名::::::晕,我的问题还是没解答...清楚...呵呵... 发表于2007-04-23 09:47:25  IP: 121.35.48.*
郁闷..................

#  无名::::::晕,我的问题还是没解答...清楚...呵呵... 发表于2007-04-23 09:49:01  IP: 121.35.48.*
按你的那种方式执行SQL语句也是一样的,,,不能更新........

    
2007-04-23 21:04:08作者回复:
# 无名::::::晕,我的问题还是没解答...清楚...呵呵... 发表于2007-04-23 09:47:25 IP: 121.35.48.*<br />郁闷..................<br /><br /><br /># 无名::::::晕,我的问题还是没解答...清楚...呵呵... 发表于2007-04-23 09:49:01 IP: 121.35.48.*<br />按你的那种方式执行SQL语句也是一样的,,,不能更新........<br />==================<br /><br />我的代码绝对没有问题,你注意看“杨婉君”被改为“杨婉君1”<br />2.GridView选中,编辑,取消,删除: 杨婉君<br /><br />3.GridView正反双向排序: 杨婉君1<br /><br />你先把我的代码全部拷贝过去绝对可以改,一定是你哪里有问题。<br /><br />我学习别人代码的方法是先全拷贝,再改我的!<br /><br />学习要善于发现问题。不断推敲,逐步逼近结果。你要动脑筋啊。<br /><br /><br />还有个问题 就是“SQLhelper”如果你的sql语句有错他不报错但是也不执行!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!<br /><br />所以你要把你的纯sql拿到sqlerver2005里执行一次 一定有问题!!!!!!!!!!!!!!!!!<br /><br />编程也和破案子一样啊!!!!!!!!!!!!!!!!<br /><br />还有你主键设了么 等等 都要考虑的。


#  梦 发表于2007-04-26 15:04:00  IP: 222.169.19.*
有没有用VB.NET实现的..VRIDVIEW 控制真麻烦啊

#  梦 发表于2007-04-26 15:07:12  IP: 222.169.19.*
有没有用VB.NET实现的..VRIDVIEW 控制真麻烦啊

#  teachman_999 发表于2007-04-27 15:15:30  IP: 202.64.67.*
to 版主:

在更新时,以下语句
((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim()

读出来的还是原有值

#  john_huang 发表于2007-04-28 21:15:17  IP: 222.191.242.*
不顶以后不要上CSDN

#  深蓝 发表于2007-04-29 09:12:45  IP: 58.49.225.*
没一点技术含量的垃圾代码拿出来献丑也就算了,还什么C#精髓,真是笑煞旁人!

虽然GridView没有规定使用的人,但你这样的人使用就是对它的侮辱。asp.net的开发者们知道你这样的使用他们辛苦开发的成果又是如何作想呢?本来你是人渣也好败类也罢这都没有关系,但是你这种人也能使用神殿里的圣卷,那就是全人类的悲哀。

猪也这种动物不是天生可耻,相反的还相当之可爱,但是一只会说话的猪在这里嗷嗷学语,还学人话在这里大放厥词,还在这里像教师那样教别人,这就是你的不对了,你把别人都当成你的同类吗?
CSDN软体开发者的圣殿也放了你这种连猪都不是的什么进来。这点就有点奇怪了!

----------------------------骂完分隔线---------------------------

GridView可以说是.net开发人员必用的控件之一,但有几个正常点的开发人员会像你这样用:

1 。不做任何处理,刷一下把所有数据读到前台,你想服务器死还是你死呀?
2。 数据本身不作任何处理直接绑,你也要别人学你这样吗?误人子弟
3。所有代码居然全在服务器端处理。最可笑的是行变色代码。你没看过猪走也吃过猪肉好不好,多看下别人的网站,不是看源码,而是直接打开HTML前端看别人写的,有几个不是直接在页面处理table的显示的。你知道不知道什么MVC,View层的ManageClass是作什么用的?是你用来操作这种所谓的网页特效的吗?
4。不说既是讲课,没有半行注释,就是平时写代码不作注释也直接被人以垃圾扔了,没有注释的代码在白测人员的眼里就是有缺陷的代码,这种代码你得重写,跟这种代码一同保存在VSS上的同仁一定会羞愧的想自杀。当然那种连CMMI2都没有的小公司就别论!



#  bczy 发表于2007-04-29 10:30:05  IP: 221.197.35.*
太棒了,受益匪浅

#  shuiyin999 发表于2007-04-29 14:49:24  IP: 220.231.157.*
不错

#  gejunlin 发表于2007-04-29 19:39:57  IP: 222.212.8.*
高手呀~强汗。我正在为这个控件犯愁呢。谢谢,谢谢。再次谢谢~

#  flyfish 发表于2007-05-03 21:53:14  IP: 58.51.64.*
谢谢您,高手!向你致敬,多写些图文并茂的东西出来回报社会!

#  flyandgoing 发表于2007-05-05 14:31:20  IP: 218.12.45.*
真好

#  MRPETERHXY 发表于2007-05-11 10:46:57  IP: 59.41.216.*
技术是过硬的,不过代码全部写在服务器端,真不是一件很好的事。

#  fdzxlg 发表于2007-05-14 14:32:13  IP: 124.21.205.*
能磊出这么多代码出来,一名话:辛苦了!

#  lgz_1205 发表于2007-05-17 17:13:59  IP: 221.219.24.*
LZ,你辛苦了!

#  netharry 发表于2007-05-17 20:07:35  IP: 125.34.159.*
多谢楼主

#  piscailei 发表于2007-05-18 16:30:41  IP: 222.183.142.*
辛苦了

#  axe 发表于2007-05-18 16:51:29  IP: 210.78.63.*
象这样的乐于助人的人太少拉,所以我除了顶还是顶!

#  hitspeed 发表于2007-05-19 14:02:23  IP: 211.93.58.*
哈哈,深蓝的一翻言论不得不顶啊,感同身受

#  bearhunter 发表于2007-05-19 19:00:46  IP: 125.115.13.*
学习

#  boy2007 发表于2007-05-19 21:46:43  IP: 121.69.138.*
高手,继续努力。

#  Genius1218 发表于2007-05-21 15:42:16  IP: 210.14.75.*
楼主小心你强死了

#  abest 发表于2007-05-22 11:44:32  IP: 221.224.120.*
问一个问题:
我用Gridview绑定一个List泛型的数据源,然后GridView的列标题就是英文,我现在想改为中文怎么办?还要求可以排序!
另外,不能前台里面写代码,前台只能放一个GridView,然后所有的代码都在后台做,因为我还想重用这些代码。谢谢!
如果您方便的话,发到我信箱里:Abest_000@126.com

#  我是谁 发表于2007-05-22 18:06:27  IP: 59.63.36.*
在更新时,以下语句
((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim()

读出来的还是原有值


我也遇到同样的问题,希望版主能尽快解决呀
偶将不甚感激。。。。

#  hai173 发表于2007-05-23 10:12:30  IP: 125.33.157.*
我想问一个GridView正反双向排序的问题 我是按着你的写的 但是显示不出来 代码你看一下哪有问题
CS:
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
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState("SortOrder") = "ProductID";
ViewState["OrderDire"] = "ASC";
bind();
}
}

public void bind()
{
string strconn = "server = .;database = Northwind;uid = sa;pwd = sa";
string strda = "select * from Products";
SqlConnection conn = new SqlConnection(strconn);
SqlDataAdapter da = new SqlDataAdapter(strda, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Products");
DataView view = ds.Tables["Products"].DefaultView;
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
view.Sort = sort;
GridView1.DataSource = view;
GridView1.DataBind();

}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs

#  OGLD3D 发表于2007-05-23 17:18:06  IP: 218.65.61.*
真是高手呀,厉害的不得了。不过我想知道,如何能实现既要分页,又要进行编辑删除的GridView。因为绑定的时候,要进行分页,就不能用if(!IsPostBack),而要编辑,则必须用if(!IsPostBack)。如何让二者兼得呢?请赐教!

#  ElandYang 发表于2007-05-24 11:08:34  IP: 222.92.144.*
鼠标移到GridView某一行时改变该行的背景色方法一:
for (i = 0; i < GridView1.Rows.Count; i++)
应该改为:
for (i = 0; i <=GridView1.Rows.Count; i++)
否则第一条记录不会改变颜色!

感谢楼主的资料!

#  kapokliker 发表于2007-05-25 11:06:54  IP: 218.18.18.*
学习了,如果回帖表示对作者的尊重的话,那我就要回一下了~

#  海鸟 发表于2007-05-25 17:13:46  IP: 124.240.127.*
不错,先收藏起来好好研究才行啊

#  peter824 发表于2007-06-17 19:45:08  IP: 59.40.32.*
都是一些很实用的方法,谢谢,先收藏!!

#  jansnow 发表于2007-06-18 16:25:26  IP: 60.12.88.*
向楼主致敬!

#  caitanlin 发表于2007-06-25 20:14:11  IP: 202.96.184.*
很不错啊!!
先收藏起来好好研究下!!!

#  vipwlei 发表于2007-06-26 10:30:31  IP: 218.22.88.*
高手,佩服,珍藏之。

#  qqfishqq 发表于2007-07-25 13:59:01  IP: 58.51.3.*
月儿,做最后那个Excel 时候总是 找不到可安裝的ISAM?郁闷啊!好久都找不到答案?

#  cleveryou 发表于2007-07-26 15:19:19  IP: 221.223.64.*
很不错,谢谢了

#  kissyougoodby 发表于2007-08-10 14:51:15  IP: 202.102.140.*
能把这些技术集中到一起,的确不错。

#  L_gx396696760 发表于2007-08-14 16:53:09  IP: 221.222.34.*
太棒了,谢了!

#  hsnhhsnh 发表于2007-08-16 10:23:18  IP: 221.200.253.*
辛苦楼主 ,顶一下

#  tsp300 发表于2007-08-18 10:41:14  IP: 211.69.197.*
楼主好牛哇 

#  zqw831013 发表于2007-09-01 11:18:44  IP: 125.77.178.*
mark!!

#  mallva 发表于2007-09-01 23:18:10  IP: 222.212.74.*
不错

#  Angells 发表于2007-09-05 10:40:07  IP: 219.142.130.*
清清月儿姐,辛苦你了,你写得真是太棒了,给我们这些新接触.net2005一个很好的学习教材,简直就是才女啊!你就像是一个园丁,哪里需要你你就出现在哪里。以后我会加倍努力,像你学习!

#  Angells 发表于2007-09-05 10:42:38  IP: 219.142.130.*
才女,我好佩服你啊!!!

#  Angells 发表于2007-09-05 10:45:08  IP: 219.142.130.*
希望你以后再接再厉,继续做下去,“清清姐“我们永远支持你!希望日后能看到你更多的作品!!!

#  daijun17 发表于2007-09-06 11:25:08  IP: 58.19.198.*
谢谢你的文章,我学到了不少,保存下来慢慢看,呵呵~

#  super_roma 发表于2007-09-06 15:01:48  IP: 211.137.169.*
谢谢,高手!
支持您!

#  handsome_zhg 发表于2007-09-13 15:55:18  IP: 220.249.106.*
强中自有强中手,一山更比一山高!人外有神,山外有仙!

#  beyondkko 发表于2007-09-21 16:51:47  IP: 124.118.62.*
Thank You Very Much@!!

#  daijun17 发表于2007-09-30 11:03:01  IP: 58.19.198.*
GridView合并表头多重表头无错完美版(以合并3列3行举例)
-----------------
此方法产生的代码有问题,</th></tr><tr></th> 控件会自己生成</th> 你看网页源码就会发现的.

------------
行变色不要放在服务器端进行,postback会搞死人的.

--------------
谢谢你的文章~

#  daijun17 发表于2007-09-30 11:20:56  IP: 58.19.198.*
添加属性建议使用:
tcHeader[9].Attributes.Add("style", "background-color:#4A3C8C;color:#E7E7FF;");
这种格式,XHTML标准~
表头颜色一样的话,可以在css中定义样式,不一样也可以加上class属性~

#  daijun17 发表于2007-10-17 10:17:10  IP: 58.19.198.*
反反复复看了几次,给了我很多启示,再次感谢.
你可以看看这篇关于gridview扩展的文章:
http://www.cnblogs.com/webabcd/archive/2007/01/08/614510.html
偏向于JS实现,减少了好多postpack.

#  zaxiaozhuzai 发表于2007-10-31 03:22:40  IP: 121.32.185.*
楼主,实在是佩服到。。。
收藏研读。。。
感激!!

#  aasheaa212 发表于2007-11-11 14:47:11  IP: 222.242.195.*
很好!楼主辛苦了。

#  aasheaa212 发表于2007-11-11 14:54:15  IP: 222.242.195.*
还行!
在多看看!
借鉴借鉴

#  gooddasenlin 发表于2007-11-13 13:50:56  IP: 220.231.57.*
感谢楼主!楼主是我们学习的榜样!

#  qyhxx 发表于2007-11-19 19:08:56  IP: 61.132.139.*
看完GridView合并表头多重表头后,
想请教如何合并数据行单元格,比如合并第一行内容相同的几个单元格为一个?
我E-mail:qyhxx@163.com希望能得到你的指教,
我会一直关注你的博客,谢谢!

#  yuguomin12 发表于2007-11-21 12:39:10  IP: 117.45.225.*
楼主你真行..支持你~

#  kennyfan 发表于2007-11-22 16:22:10  IP: 202.105.50.*
多谢楼主的努力!受教了!

#  wgq3890650 发表于2007-11-28 13:39:26  IP: 58.216.235.*

#  jy02870694 发表于2007-11-29 15:46:40  IP: 222.172.150.*
要实现GridView动态生成列,怎么做啊?

#  tfj0503734 发表于2007-12-05 17:24:41  IP: 222.247.76.*
的确不错

#  ccsccl 发表于2007-12-20 19:48:28  IP: 222.85.179.*
太猛了!顶!

#  outetugou 发表于2008-01-22 00:14:34  IP: 222.0.35.*
楼主辛苦了,绝对好贴!学习正需要。