C#精髓-- GridView 72 般绝技
C#精髓-- GridView 72 般绝技
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 无代码分页排序:
效果图:
1.AllowSorting 设为 True,aspx 代码中是 AllowSorting="True";
2.默认 1 页 10 条,如果要修改每页条数,修改 PageSize 即可,在 aspx 代码中是 PageSize="12"。
3.默认的是单向排序的,右击 GridView 弹出“属性”,选择 AllowSorting 为 True 即可。
2.GridView 选中,编辑,取消,删除:
效果图:
后台代码:
你可以使用 sqlhelper,本文没用。代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
//清清月儿http://blog.csdn.net/21aspnet
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Val
ue.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, GridViewCancelEditEventAr
gs 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" CellPaddin
g="4"
ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_Ro
wDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridVi
ew1_RowCancelingEdit">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White
" />
<Columns>
<asp:BoundField DataField="身份证号码" HeaderText="用户 ID" ReadO
nly="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="Whit
e" />
</asp:GridView>
3.GridView 正反双向排序:
效果图:点姓名各 2 次的排序,点其他也一样可以。