Thursday, June 26, 2008

paging and sorting in Datagrid

     

 


div><asp:DataGrid id="dg" runat="server" AutoGenerateColumns="False" width="90%"

 HorizontalAlign="Center" AllowSorting="True"AllowPaging="True"PageSize="5"

     

 


PagerStyle-Mode="NumericPages" PagerStyle-PageButtonCount="5"

     

 


PagerStyle-Position="Bottom" pagerStyle-HorizontalAlign="Center"

     

 


PagerStyle-NextPageText="Next" PagerStyle-PrevPageText="Prev"

     

 


PagerStyle-CssClass="pagerText" OnPageIndexChanged="data_PageIndexChanged"

OnSortCommand="data_SortCommand" CellPadding="4" ForeColor="#333333"

 GridLines="None" >

<HeaderStyle HorizontalAlign="Center" BackColor="#507CD1"

     

 


Font-Bold="True" ForeColor="White" />

     

 


<ItemStyle BackColor="#EFF3FB" />

<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<EditItemStyle BackColor="#2461BF" />

<SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

     

 


<PagerStyle NextPageText="Next" PageButtonCount="5" PrevPageText="Prev"

     

 


HorizontalAlign="Center" BackColor="#2461BF" ForeColor="White"></PagerStyle>

<AlternatingItemStyle /><Columns>

<asp:BoundColumn DataField="companyname" HeaderText="companyname"

     

 


SortExpression="companyname" />

     

 


<asp:BoundColumn DataField="companyaddress"

     

 


ItemStyle-HorizontalAlign="center" SortExpression="companyaddress">

     

 


<ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundColumn>

<asp:BoundColumn DataField="companycity"ItemStyle-HorizontalAlign="Center"

     

 


SortExpression="companycity" >

     

 


<ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundColumn>

     

 


</Columns></asp:DataGrid></div>

codebehind pages

 
private enum SortOrder


{soAscending = 0,


soDescending = 1

}




static readonly String SORTEXPRESSION = "SortExpression";



 
  

 


static readonly String SORTORDER = "SortOrder" 

 

 

private void Page_Load(object sender, System.EventArgs e)

{String  defaultSortExpression;

SortOrder defaultSortOrder; 

 

 

if (!Page.IsPostBack){
  

 






defaultSortExpression =



 
"companyname";  

 



SortOrder.soAscending;    

 


this.ViewState.Add(SORTEXPRESSION, defaultSortExpression);

this.ViewState.Add(SORTORDER, defaultSortOrder);bindData(defaultSortExpression,defaultSortOrder);

 
}}   
protected void data_SortCommand(Object source, DataGridSortCommandEventArgs e){

 

 


 String newSortExpression = null;

 

String currentSortExpression = null;SortOrder currentSortOrder;

currentSortExpression =(String)(this.ViewState[SORTEXPRESSION]);currentSortOrder =(  

 
SortOrder)(this.ViewState[SORTORDER]); 

newSortExpression = e.SortExpression; 

if (newSortExpression == currentSortExpression){

if (currentSortOrder == SortOrder.soAscending){

currentSortOrder =SortOrder.soDescending;}

else{currentSortOrder =SortOrder.soAscending;}

} else{currentSortExpression =newSortExpression;

currentSortOrder =SortOrder.soAscending;}

this.ViewState.Add(SORTEXPRESSION, currentSortExpression);

ViewState.Add(SORTORDER, currentSortOrder); 

bindData(currentSortExpression,currentSortOrder);}

protected void data_PageIndexChanged(Object source, DataGridPageChangedEventArgs

e){String currentSortExpression;

SortOrder currentSortOrder; dg.CurrentPageIndex = e.NewPageIndex;

currentSortExpression =( String)(this.ViewState[SORTEXPRESSION]);

currentSortOrder =(SortOrder)(this.ViewState[SORTORDER]);

bindData(currentSortExpression,currentSortOrder);

 
} 

 
private void bindData(String sortExpression, SortOrder sortOrder){


DataTable dTable = null; 

int index = 0;DataGridColumn col = null;

String colImage = null;

String strSortOrder = null;


SqlConnection dbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);dbConn.Open(); 

if (sortOrder == SortOrder.soAscending){strSortOrder =" ASC";}

strSortOrder =" DESC";}

SqlDataAdapter da = new SqlDataAdapter("SELECT companyname,companyaddress,companycity FROM companydetail ORDER BY "

+ sortExpression + strSortOrder, dbConn);

new DataTable();da.Fill(dTable);

for (index = 0; index < dg.Columns.Count; index++)

{col = dg.Columns[index]; 

if (col.SortExpression == sortExpression){

if (sortOrder == SortOrder

.soAscending){colImage ="<img src='images/sort_ascending.gif' border='0'>";} else{colImage =" <img src='images/sort_descending.gif' border='0'>";} 
}


else{colImage ="";}

col.HeaderText = col.SortExpression + colImage;}

dg.DataSource = dTable;dg.DataBind();} 



No comments :

Post a Comment

AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath