11.GridView一般换行与强制换行:
效果图:
首先设置<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-开始
图2-点击显示的CheckBox后

解决方案:
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弹出新页面/弹出制定大小位置新窗口:
效果图:

方案一:简单的方法,新窗口不固定大小
 <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!,很好用):
效果图:

代码:
<!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行举例)
效果图:
 
后台代码:
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突出显示某一单元格(例如金额低于多少,分数不及格等)
效果图:

解决方案:主要是绑定后过滤
 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加入自动求和求平均值小计
效果图:
解决方案:   
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
效果图:

解决方案:
页面增加一个按钮,单击事件添加如下方法:
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", "p_w_upload;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", "p_w_upload;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();
   }
如果GridView存在模板列,其中包含子控件,例如CheckBox等,导出EXCEL后就会出现该区域的不规律。所以要对模板列单独处理(转载)

 

 

 public static void PrepareGridViewForExport(Control gv)//模式化特殊元素 flashcong
    {
        LinkButton lb = new LinkButton();
        Literal l = new Literal();
        string name = String.Empty;
        for (int i = 0; i < gv.Controls.Count; i++)
        {
            if (gv.Controls[i].GetType() == typeof(LinkButton))
            {
                l.Text = (gv.Controls[i] as LinkButton).Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            else if (gv.Controls[i].GetType() == typeof(DropDownList))
            {
                l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            else if (gv.Controls[i].GetType() == typeof(CheckBox))
            {
               l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
               gv.Controls.Remove(gv.Controls[i]);
               gv.Controls.AddAt(i, l);
            }
            else if (gv.Controls[i].GetType() == typeof(ImageButton))
            {
              l.Text = "图片";
              gv.Controls.Remove(gv.Controls[i]);
              gv.Controls.AddAt(i, l);
            }
            if (gv.Controls[i].HasControls())
            {
                PrepareGridViewForExport(gv.Controls[i]);
            }
        }
    }
 
 
 

GridView隐藏列
在Asp.net 2.0中增加了一个新的数据绑定控件:GridView,其目的用来取代Asp.net1.x中的DataGrid控件。
获取GridView中的某列值的方法为
   protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string id = GridView1.Rows[e.NewEditIndex].Cells[0].Text;
        Response.Redirect("TempletEdit.aspx?id=" + id);
    }
同理,获取GridView中的键值的方法为
   protected void GridView1_SelectChanging(object sender, GridViewEditEventArgs e)
    {
        string id = GridView1.DataKeys[e.NewSelectIndex].Value.ToString();
        Response.Redirect("TempletEdit.aspx?id=" + id);
    }
但这种方法无法获取隐藏列和模板列的值。
但有一点很不爽的是,如果把某列设置为visible=false,则不会进行数据绑定,也就是说无法直接从GridView中取到这个列内的文本。
这一功能在DataGrid时代几乎是必须的,在对列表进行批量选中操作时非常有用(比如批量删除),隐藏列通常用于存储DataGrid行对应数据记录 的关键字的值,而现在在GridView中却行不通,着实令一大批人头疼不已,网上也有了其解决方案,其中一个如下,其原理是在行创建时设置其css样式 为不可见,这样可以避免直接设置列的visible=false带来的数据不绑定问题:

void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
//让索引为0的列不可见,实际上是产生的css代码
e.Row.Cells[0].Visible = false;
//other code if neccecary
}
个人认为此方法不够优雅,Asp.net 1.x年代本人就尽量避免使用事件,宁可在数据源DataTable中做循环遍历来进行一些或烦或简的处理。一来因为vs2003对web标准支持欠佳, 而我习惯于在Html源码模式下工作,所以为DataGrid添加一个事件是极其麻烦的事情;二来因为其性能我也不大放心,毕竟每一行都要触发一次事件。 因而在GridView中仍然不喜欢事件,而且即使vs2005对web标准的支持大大改善,但添加一个事件容易,删除一个事件还是要在两个文件中进行手 动删除才能同步,比较麻烦。
就事论事,回到标题,现提供我对这个问题的解决方案,基本思想仍然是用css使得单元格不可见,而不妨碍它的数据绑定,但我的方法却不需要在cs文件中多加一行代码,其思路如下:
(1)设置一个css类:

.hidden { display:none;}
在web标准横行的现在,我想为每个aspx链接一个common.css是基本的素质,呵呵
(2)随后在GridView的列编辑对话框中,对需要进行隐藏的列进行设置,分别设置FootStyle,HeaderStyle,ItemStyle的CssClass属性为“hidden”
Ok,这样我们就实现了隐藏列的目的,同时又能保证对其进行数据绑定。
好了,看到这里,如果你有所收获,并很高兴的马上赶回去,为你的GridView添加此设置,避免了使用事件来隐藏列,并实现了批量删除的功能。那你就大错特错了,呵呵
很可惜的是,在网上看到很多人关于GridView的这个功能的讨论,很大一部分人都是为了这个功能而来的。
是的,在DataGrid中,要实现这个功能,隐藏列来存储键值是必须的,甚至是一个初学者需要摸索才能知道的技巧。然而现在GridView是用来替代 DataGrid的,微软所作的考虑更加周全。针对DataGrid无法提供行主键的问题,它提供了两个全新的属性:DataKeys和 DataKeyNames!其SDK中的描述如下:
DataKeyNames:获取或设置一个数组,该数组包含了显示在 GridView 控件中的项的主键字段的名称。
DataKeys:获取一个 DataKey 对象集合,这些对象表示 GridView 控件中的每一行的数据键值。
看到这里,也许你会有所明白,针对批量选中操作的问题,我们有了新的方案,现在来看一个简单的实现,假设主键字段是id:
//设置主键字段名称数组,可以多个字段,
//你也可以在设计器中直接设置主键字段,多个字段用逗号隔开
GridView1.DataKeyNames = new string[] { "id" };
这样,当进行数据绑定时,GridView会自动用键值填充DataKeys集合,从而在绑定或者回传后可以通过DataKeys集合来获取某一行的键值,例如假设想获取第i行的id键值,其代码如下:
//获取第i行的id键值,注意该索引即行的索引,从header到footer都算在内
string id=GridViews1.DataKeys[i]["id"].ToString();
//通过键值对数据进行处理……
由此可见,GridView提供了新的机制来绑定额外的数据到每一行。这使得可以完全放弃原来DataGrid通过隐藏列来绑定键值的做法,显然,现在的 方案要优雅的多。当然,如果你实在需要在GridView中使用隐藏列并能够进行取值,我上面的方法不防一试,虽然我很难想到有这样的需求^_^ 
 
 
 
 

/// <summary>
    /// 导出文件
    /// </summary>
    /// <param name="FileType">导出文件的类型,如:"application/ms-excel"</param>
    /// <param name="FileName">导出文件的名称,"报表.xls"</param>
    /// <param name="gv">从gridview导出</param>
    private void Export(string FileType, string FileName,GridView gv)
    {
        try
        {
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF7;       // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
            Response.AppendHeader("Content-Disposition", "p_w_upload;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());       //防止中文文件名乱码
            Response.ContentType = FileType;        //导出的文件类型,表格 application/ms-excel,文档 application/ms-word
            this.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            gv.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {
            Response.Write("<script language=javascript>alert('导出数据出错!" + ex.Message + "')</script>");
        }
    }
    //必须的,否则会报错
    public override void VerifyRenderingInServerForm(Control control)
    {
        // base.VerifyRenderingInServerForm(control);
    }
 
在GridView表头显示排序方向
代码如下:
//在GridView的RowCreated事件中
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  if (e.Row.RowType == DataControlRowType.Header) //如果是表头
    foreach (TableCell MyHeader in e.Row.Cells) //对每一单元格     
      if (MyHeader.HasControls())
        if (((LinkButton)MyHeader.Controls[0]).CommandArgument == GridView1.SortExpression)
        //是否为排序列
          if (GridView1.SortDirection == SortDirection.Ascending) //依排序方向加入方向箭头
            MyHeader.Controls.Add(new LiteralControl("↓"));
          else
            MyHeader.Controls.Add(new LiteralControl("↑"));
}
 
 
 
GridView控件笔记
1、无数据依然显示表头
    // 参数:所要绑定的GridView      所要绑定的数据集 
    public void BindNoRecords(GridView gridView, DataSet ds)
    {
        if(ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gridView.DataSource = ds;
            gridView.DataBind();
            int columnCount = gridView.Rows[0].Cells.Count;
            gridView.Rows[0].Cells.Clear();
            gridView.Rows[0].Cells.Add(new TableCell());
            gridView.Rows[0].Cells[0].ColumnSpan = columnCount;
            gridView.Rows[0].Cells[0].Text = "没有任何记录!";
            gridView.RowStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
        }
    }
2、BoundField的格式化字符串
     DataFormatString="{0:F}" 还要加上HtmlEncode="false"才会生效。
     至于{0:F}格式请参照 String.Format格式说明

3、分页
     将AllowPaging属性设置为true,pagesize设置成你要求每页显示的行数,在pagerSettings中设置分页样式
     如  :
     FirstPageText:首页
     LastPageText:尾页
     NextPageText:下一页
     PreviousPageText:上一页
    
     Mode:NextPreviousFirstLast
     给GridView1添加GridView1_PageIndexChanging事件
     写入代码如:
      protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (GridView1.EditIndex != -1)
        {
            e.Cancel = true;
        }
        else
        {
            GridView1.PageIndex = e.NewPageIndex;
            dataBind();
        }
    }
    注:dataBind();是你绑定数据的函数,接触过GridView的程序员都应该知道怎么写吧。
    这样一个有分页功能的gridview就实现了
 

如何GridView动态改变行列的背景色?
Posted on 2007-07-23 15:34 赣江源 阅读(251) 评论(0)  编辑 收藏 所属分类: ASP.NET2.0 GridView 
当GridView希望依据单元格的值来动态改变行列的背景色时,可以将判断写在RowDataBound 事件中,在此事件中可以取得该单元格的值来做判断并改变显示的颜色;
例如有一个 value列,要依此列的值大小来決定列的颜色,当value大于0时背景色为蓝色,反之为红色;后台代码如下(*.aspx.cs):
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView oRow;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            oRow = (DataRowView)e.Row.DataItem;
            //依 value 值来决定单元格的颜色,>0,蓝×××;<0,红色;
            if (Convert.ToDouble(oRow["value"].ToString()) < 0)
            {
                e.Row.BackColor = System.Drawing.Color.Red;
            }
            else
            {
                e.Row.BackColor = System.Drawing.Color.Blue;
            }
            //或者对某一单元格着色,最后一列
            if (Convert.ToDouble(oRow["value"].ToString()) < 0)
            {
                e.Row.Cells[e.Row.Cells.Count - 1].BackColor = System.Drawing.Color.Red;
            }
            else
            {
                e.Row.Celle[e.Row.Cells.Count - 1].BackColor = System.Drawing.Color.Blue;
            }
        }
    }
 

如何对GridView行自动编号?
Posted on 2007-07-23 15:11 赣江源 阅读(346) 评论(0)  编辑 收藏 所属分类: ASP.NET2.0 GridView 
有时候会遇到这样的情况,就是需要对GridView表格显示的结果增加一列自动递增编号列,以标示每一行的序号。要实现这一功能,首先在 GridView 第一列加入一个 TemplateField,并在 TemplateField 的 ItemTemplate 加入一个 Label (ID=lblNo),*.aspx 对应代码如下:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" 
    DataKeyNames="Flag,ID" DataSourceID="SqlDataSource1" EmptyDataText="无记录。">  
    <Columns>  
        <asp:TemplateField HeaderText="编号">  
            <ItemTemplate>  
                <asp:Label ID="lblNo" runat="server" Text="Label"></asp:Label>  
            </ItemTemplate>  
            <ItemStyle Wrap="True" />  
            <HeaderStyle Wrap="False" />  
        </asp:TemplateField>  
    </Columns>  
</asp:GridView>
然后在 GridView 的 RowDataBound 事件中,设定每一列的 lblNo 的 Text 属性值为 RowIndex + 1(因为 RowIndex 起始编号为 0 ,所以每一行的自动编号为 RowIndex + 1) 。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Label olabel;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            olabel = (Label)e.Row.Cells[0].FindControl("lblNo");
            olabel.Text = Convert.ToString(e.Row.RowIndex + 1);
        }
    }
如果遇到有分页时,以上代码对每一页都是从1开始编号,所以对于有分页的情况需要修改成:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Label olabel;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            olabel = (Label)e.Row.Cells[0].FindControl("lblNo");
            olabel.Text = Convert.ToString(GridView1.PageIndex * GridView1.PageSize + e.Row.RowIndex + 1);
        }
    }