Under asp.net 2.0, gridview is very convenient. If you add a DATASOURCE series control, you can immediately bind it to the gridview, which is very convenient. But in fact, you can also use datatable or dataview. At this time, you will not use the datasource series controls. Let's talk about how to realize page turning and sorting of each column in the gridview control under asp.net 2.0.
Editing function.
First, we read the employee table in the northwind database. After placing a gridview, add several bound columns, the code is as follows
<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="G ridView1_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" SortExpression="firstname" />
<asp:BoundField DataField="lastname" HeaderText="Last Name" SortExpression="lastname" />
<asp:CommandField ShowEditButton="True" />
</Columns>
<RowStyle BackColor="#FFFFD6" 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>
First, we need to implement paging, set AllowPaging to true, and set the number of paging items per page, and finally write it in codebehind
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
In order to achieve automatic click sorting of each column, you can set allowsorting=true, and then set OnSorting="GridView1_Sorting", where gridview_sorting
The code is
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();
}
Obviously, setting viewsate to save the order of each sorting, I believe the above is easy to understand.
Finally, the editing function is implemented, because OnRowEditing="GridView1_RowEditing" has been set in the aspx page, and the code of GridView1_RowEditing is
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 employee whereid=@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();
}
As you can see, the above code is actually the same as the asp.net 1.1 version. Finally, the process of bindgrid() is very simple, it is binding
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();
Here the gridview is bound to the dataview, and dv.sort is used to set the order of each sort. That is to say, the order remains unchanged after each sort.
Of course, the last thing is the page_load event.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}