Live Chat Icon For mobile
Live Chat Icon

How to do Simple sorting using DataGrid

Platform: ASP.NET| Category: DataGrid

<asp:DataGrid id='DataGrid1' AllowSorting =True OnSortCommand ='SortData'  runat='server'></asp:DataGrid>

VB.NET


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	’Put user code to initialize the page here
	If Not Page.IsPostBack Then
	            BindDataGrid('ProductId')
	End If
End Sub

Protected Sub SortData(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs)
	BindDataGrid(e.SortExpression.ToString())
End Sub

Sub BindDataGrid(ByVal sortfield As String)
	’Fill the Dataset
	’.....
	Dim dv As DataView = ds.Tables(0).DefaultView
	dv.Sort = sortfield
	DataGrid1.DataSource = dv
	DataGrid1.DataBind()
End Sub

C#


private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	if(! Page.IsPostBack  )
	{
		BindDataGrid('ProductId');
	}
}

protected void SortData(Object source,  System.Web.UI.WebControls.DataGridSortCommandEventArgs  e )
{
	BindDataGrid(e.SortExpression.ToString());
}

void BindDataGrid(string sortfield)
{
	//Fill the Dataset
	//.....
	DataView dv    = ds.Tables[0].DefaultView;
	dv.Sort = sortfield;
	DataGrid1.DataSource = dv;
	DataGrid1.DataBind();
}

Share with

Share on twitter
Share on facebook
Share on linkedin