รับทำเว็บไซต์ครบวงจร
เลือกภาษา Thai English     facebook
รายการหลัก



บทความ


Page Ranking Tool

การเรียงลำดับ Gridview Sorting Using C#


สอน ASP net MS SQL
> การเรียงลำดับ Gridview Sorting Using C#


ASP.Net 2.0 provides the sorting feature also in GridView Control. You can sort the records displayed using Gridview control in ascending or descending order retrieved from sql database. You can use C# code to bind the SQL data with GridView control and follow the following simple steps to make your ASP.Net GridView control with sorting enabled.

GridView Examples:
You can see the live samples and examples of GridView from the following links:
ASP.Net GridView Sorting
ASP.Net GridView Paging
Nested GridView Control
GridView DataFormatString Date
GridView Control Edit Update Cancel Commands

First of all drag the GridView control from Data controls menu. It will add the GridView control HTML source code as given above. Now click on GridView control to load the control properties at right side panel.



  Code
<asp:GridView id="GridView1" runat="server"></asp:GridView>


รับทำเว็บ  webUB.com



To allow the sorting in GridView control select True from the dropdown list of AllowSorting property of Gridview as shown in above image.
This will add AllowSorting="True" in HTML source code of GridView Control.
Next step is to bind the Sorting event for GridView Control.
รับทำเว็บ  webUB.com


To bind the Sorting event of GridView control, double click on the Sorting event in the properties viewer of GridView. This will add the Sorting event in the HTML source code and C# code for Gridview.



  Code
<asp:GridView ID="GridView1"
runat="server"
CellPadding="2"
AllowSorting="True"
OnSorting="GridView1_Sorting"
AutoGenerateColumns="False"
Width="500px">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" SortExpression="ProductName">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>

<asp:BoundField DataField="ProductName" HeaderText="Product Name" SortExpression="ProductName">
<HeaderStyle HorizontalAlign="Left" Width="200px" />
</asp:BoundField>

<asp:BoundField DataField="UnitsInStock" HeaderText="Units In Stock" SortExpression="UnitsInStock">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>

<asp:BoundField DataField="UnitPrice" HeaderText="Price Per Unit" SortExpression="UnitPrice">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
</asp:GridView>



C# Code for GridView Sorting


  Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["sortOrder"] = "";
bindGridView("","");
}
}

public void bindGridView(string sortExp,string sortDir)
{
// string variable to store the connection string
// defined in ConnectionStrings section of web.config file.
string connStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

// object created for SqlConnection Class.
SqlConnection mySQLconnection = new SqlConnection(connStr);

// if condition that can be used to check the sql connection
// whether it is already open or not.
if (mySQLconnection.State == ConnectionState.Closed)
{
mySQLconnection.Open();
}

SqlCommand mySqlCommand = new SqlCommand("select top 10 ProductID, ProductName, UnitsInStock, UnitPrice from products", mySQLconnection);
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);

DataView myDataView = new DataView();
myDataView = myDataSet.Tables[0].DefaultView;

if (sortExp!=string.Empty)
{
myDataView.Sort = string.Format("{0} {1}", sortExp, sortDir);
}

GridView1.DataSource = myDataView;
GridView1.DataBind();

// if condition that can be used to check the sql connection
// if it is open then close it.
if (mySQLconnection.State == ConnectionState.Open)
{
mySQLconnection.Close();
}

}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{

bindGridView(e.SortExpression, sortOrder);

}

public string sortOrder
{
get
{
if (ViewState["sortOrder"].ToString() == "desc")
{
ViewState["sortOrder"] = "asc";
}
else
{
ViewState["sortOrder"] = "desc";
}

return ViewState["sortOrder"].ToString();
}
set
{
ViewState["sortOrder"] = value;
}
}



Output:
Get the output for above discussed code from the following link:
http://www.ezineasp.net/Samples/ASP-Net-cs/GridView-Control/GridView-Sorting/Default.aspx

Continue to next tutorial: ASP.Net Bind GridView to DataTable to learn how to bind the records stored in a datatable to gridview.

refer: http://www.ezineasp.net/post/ASP-Net-2-0-Gridview-Sorting-Using-C-sharp.aspx

Short URL click!
<< กลับคืน : เข้าชม 15,524 ครั้ง : ขึ้นไปด้านบน

รับทำเว็บไซต์ รับสร้างเว็บไซต์ รับออกแบบเว็บ รับเขียนเว็บ รับสอนทำเว็บ รับเช่า hosting รับเช่าพื้นที่เว็บไซต์ จดชื่อเว็บ รับโปรโมทเว็บไซต์ รับดูแลเว็บ SiteMap
สปริงเกอร์http://www.xn--22c2c4blb9n.xn--o3cw4h/