http://www.cnblogs.com/shanymen/archive/2009/05/22/1486654.html
GridView控件是.net里的一个显示数据控件,该控件制作很人性化,基本上不用编写代码就可以完成数据绑定、分页、排序、编辑、删除、选定行等操作。
主要属性:
Sort:根据指定的排序表达式和方向对 GridView 控件进行排序。
- 编程的方法绑定数据,并实现分页
页面源代码中添加一个GridView控件(GridView1),并AllowPaging="True",设置PageIndexChanging事件。
cs代码:
protected void bind()
{
//此处为GridView1绑定数据库
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from test";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
GridView1.DataSource = myDs;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.bind();
}
- 编程方法实现排序
页面源代码中添加一个GridView控件(GridView1),并设置GridView1的Sorting事件。
cs代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//设置信息字典,排序字段和排序方法
ViewState["SortOrder"] = "ID";
ViewState["OrderDire"] = "ASC";
this.bind();
}
}
public SqlConnection GetConnection()
{
//读取web.config中的连接字符串,创建SqlConnection连接
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}
protected void bind()
{
//此处为GridView1绑定数据库
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from test";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
//设置排序所用的字段和排序方法
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
GridView1.Sort = sort;
GridView1.DataSource = myDs;
GridView1.DataBind();
}
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;
}
this.bind();
}
- 选择GridView控件的行,在另一个GridView控件中显示相关数据
页面源代码中添加两个GridView控件(GridView1,GridView2),并设置GridView1的SelectedIndexChanging事件。GridView1已经绑定了一个数据表,当点击GridView1一行时,在GridView2中显示相关的另一张表中的数据。
cs代码:
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//提交编辑时,先取得要编辑行的主键值
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string sqlStr = "delete from test where ID=" + ID;
SqlConnection con = new SqlConnection();
//读取web.config中的连接字符串,创建SqlConnection连接
con.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
//将数据表绑定到GridView2
this.GridView2.DataSource = myDs;
GridView2.DataBind();
}
- 编程实现全选和全不选功能
页面源代码中添加两个GridView控件(GridView1),一个CheckBox控件(CheckBox1),设置CheckBox控件的AutoPostBack="True" ,为GridView1添加一个TemplateField列,并在编辑模版中为该列添加一个CheckBox 控件:<asp:TemplateField><ItemTemplate><asp:CheckBox ID="Check" runat="server" /></ItemTemplate></asp:TemplateField>。设置CheckBox的CheckedChanged事件。
cs代码:
- 鼠标移动到任意行时,该行自动变成指定颜色,双击打开新页
cs代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标移动到任意行时,该行自动变成指定颜色
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#BEC9F6';this.style.color='buttontext';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='';this.style.color='';");
//双击打开新页
e.Row.Attributes.Add("onclick", "window.open('FileInfo.aspx?id="+e.Row.Cells[0].Text+"')");
}
}