在asp.net 2.0下,gridview是十分方便的了,加一個DATASOURCE系列的控件的話,就可以馬上和gridview綁定,十分方便。但其實也可以
使用datatable或者dataview的,這個時候就不是用datasource系列控件了。下面講下如何在asp.net 2.0下,實現(xiàn)gridview控件的翻頁,各列排序,
編輯的功能。
首先,我們讀取的是northwind數(shù)據(jù)庫中的employee表。放置一個gridview后,添加幾個綁定的列,代碼如下
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None"
Width="100%" DataKeyNames="EmployeeID" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnSorting="GridView1_Sorting" PageSize="3" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="employeeid" HeaderText="Employee ID" ReadOnly="True" />
<asp:BoundField DataField="firstname" HeaderText="First Name" SortEx <asp:BoundField DataField="lastname" HeaderText="Last Name" SortExpression="lastname" />
<asp:CommandField ShowEditButton="True" />
</Columns>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
首先,我們要實現(xiàn)分頁,把AllowPaging設置為true,并設置每頁的分頁條數(shù),最后在codebehind中寫入
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
為了實現(xiàn)每列都可以自動點擊排序,可以設置allowsorting=true,然后設置OnSorting="GridView1_Sorting",其中gridview_sorting
代碼為
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["sortexpression"] = e.SortExpression;
if (ViewState["sortdirection"] == null)
{
ViewState["sortdirection"] = "asc";
}
else
{
if (ViewState["sortdirection"].ToString() == "asc")
{
ViewState["sortdirection"] = "desc";
}
else
{
ViewState["sortdirection"] = "asc";
}
}
BindGrid();
}
很明顯,設置viewsate來保存每次排序時的順序,上面的相信很容易理解。
最后,實現(xiàn)編輯功能,因為在aspx頁面中已經(jīng)設置了OnRowEditing="GridView1_RowEditing",其中GridView1_RowEditing的代碼為
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int empid;
string fname, lname;
empid = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);
fname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
lname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
SqlConnection cnn = new SqlConnection(@"data source=localhost;initial catalog=northwind;user id=sa;passWord=123456");
cnn.Open();
SqlCommand cmd = new SqlCommand("update employees set firstname=@fname,lastname=@lname where employeeid=@empid", cnn);
cmd.Parameters.Add(new SqlParameter("@fname",fname));
cmd.Parameters.Add(new SqlParameter("@lname", lname));
cmd.Parameters.Add(new SqlParameter("@empid", empid));
cmd.ExecuteNonQuery();
cnn.Close();
GridView1.EditIndex = -1;
BindGrid();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
可以看到,上面的代碼和asp.net 1.1版本的其實原理是差不多的。最后,bindgrid()的過程很簡單,為綁定咯
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from employees", @"data source=localhost;initial catalog=northwind;user id=sa;password=123456");
da.Fill(ds,"employees");
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["sortexpression"] != null)
{
dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
}
GridView1.DataSource=dv;
GridView1.DataBind();
這里gridview綁定的是dataview,并且用dv.sort設定了每次排序的順序,也就是說,每次排序后其順序都是保持不變的。
當然最后是page_load事件咯
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
http://jackyrong.VEVb.com/archive/2006/07/20/455996.html
新聞熱點
疑難解答