//-------------------------------------------------- Code for Query a DataSet
public DataSet QueryTransacationDS(string TableName,string ModifySystem,string Items,string OPID,string Begin,string End,int SortValue)
{
try
{
End = Convert.ToDateTime(End).AddDays(1).ToString("yyyy/MM/dd");
string SQLStr=String.Empty;
SQLStr = "select TransacationTime,TransacationName,OPID,Description from ZT_TransacationLog ";
SQLStr += "Where TransacationTime Between '" + Begin + "' And '" + End + "'";
if(ModifySystem=="1" || ModifySystem == "2")
{
SQLStr += "And TransacationSystemType= '" + ModifySystem + "'";
}
SQLStr += " And OPID = '" + OPID + "'";
if((SortValue%2)==0)
SQLStr += " Order By TransacationTime Desc";
else
SQLStr += " Order By TransacationTime ASC";
ArrayList Paramset=new ArrayList();
return DBConn.GetObject().getData(SQLStr, Paramset, TableName);
}
catch (Exception E)
{
return null;
}
}
and also.. I'm not sure if I write it correctly .. When Exception occur in query a Dataset does the catch should return a "null"? or maybe something else?
please show me your suggestion , thank you very much
What you can do here is to implement custom paging for the datagrid. In that case it will just bring the current page data only from database and then your sorting will also be happen for the same page.
Actually what I was saying is to implement the custom paging for the datagrid. Suppose if you just implement the inbuilt paging then it will get all the data from the database and then if you try to sort the same then it will sort the complete set of the
data and then will display the data for you thats why it will not be sorted page wise. But if you use custom paging then you will just get only the records for one page and then if you try to sort it will sort the data for that page only. In order to know
how to implement custom paging you can take help from this article.
Yes still you can use custom paging and along with custom paging you can also implement custom sorting which will make you to make certain columns sortable.
Apologies for not clarifying the matter initially. Actually what exactly is required for that is first when bind the datagrid with the first page data then store the same result set (first page data) in viewstate (in form of datatable) and then when you
click on sort link sort the data in the datatable and bind it with the datagrid. Steps for this can be like this:
first: Get the first page data from the database (preferably using custom paging) and bind it with the datagrid. Also store this datatable (first page data) and put this in viewstate.
Second: When user clicks on the sort link then just sort the data available in the viewstate datatable and bind that to the datagrid.
jcjcjc
Member
64 Points
639 Posts
How to use DataGrid to sort Data per page?
Jan 18, 2008 06:01 AM|LINK
Assume the Result of DataGrid like below
Column1 Column 2 ----------Page1
A 1
B 2
C 3
D 4
E 5 ----------Page2
F 6
G 7
when I'm in Page1 , I click the Sorting Button , I think result should be this
Column1 Column 2 ----------Page1
D 4
C 3
B 2
A 1
But I got
Column1 Column 2
G 7
F 6
E 5
D 4
Why? and How to avoid this kind of error?
private void Page_Load(object sender, System.EventArgs e) { button_StartedDate.Attributes.Add("onclick", "set_date('txtDateBeg')"); button_EndedDate.Attributes.Add("onclick", "set_date('txtDateEnd')"); if(!this.Page.IsPostBack) { this.txtDateBeg.Text=DateTime.Now.AddDays(-7).ToString("yyyy/MM/dd", DateTimeFormatInfo.InvariantInfo ); this.txtDateEnd.Text=DateTime.Now.ToString("yyyy/MM/dd", DateTimeFormatInfo.InvariantInfo); ViewState["SortValue"] =0; // this ViewState is use to check DESC or Esc ViewState["PageIndex"] =0; } } private void dg_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { int SortKey = Int32.Parse(ViewState["SortValue"].ToString()); SortKey ++; ViewState["SortValue"] = SortKey; getDataBind(); } private void getDataBind() { try { ZT_TransacationLogDAO TransacationLogDAO = (ZT_TransacationLogDAO)WebRemotingClient.getRemoteObject(typeof(ZT_TransacationLogDAO),"ZT_TransacationLogDAO"); if( TransacationLogDAO == null ) { this.Response.Write("<script>alert('The Server not work!!')</script>"); } else { DataSet DS= TransacationLogDAO.QueryTransacationDS("ZT_ModifyLog",ddlSystemType.SelectedIndex.ToString(),ddlTransactionItem.SelectedIndex.ToString(),txtUser.Text,txtDateBeg.Text,txtDateEnd.Text,Int32.Parse(ViewState["SortValue"].ToString())); DataTable dtModifyLog = DS.Tables["ZT_ModifyLog"]; DataView dv = new DataView(dtModifyLog); dg.DataSource = dv; dg.DataBind(); if(dg.Items.Count >1) { Panel1.Visible = true; lblErr.Text = ""; } else { Panel1.Visible = false; lblErr.Text = "查無資料!"; } } } catch(Exception ex) { this.Response.Write("<script>alert('"+ex.Message+"\r\n"+ex.StackTrace+"')</script>"); } }//--------------------------------------------------
Code for Query a DataSet
and also.. I'm not sure if I write it correctly .. When Exception occur in query a Dataset does the catch should return a "null"? or maybe something else?please show me your suggestion , thank you very much
apurva kaush...
Participant
1390 Points
302 Posts
Re: How to use DataGrid to sort Data per page?
Jan 18, 2008 10:21 AM|LINK
What you can do here is to implement custom paging for the datagrid. In that case it will just bring the current page data only from database and then your sorting will also be happen for the same page.
jcjcjc
Member
64 Points
639 Posts
Re: How to use DataGrid to sort Data per page?
Jan 21, 2008 05:51 AM|LINK
thank you , but I can't really understand from your post..
did you mean it's impossible to sort data per page? or I just use wrong way to write wrong code for my request?
can you please teach me how to fix my code ? thank you very much
apurva kaush...
Participant
1390 Points
302 Posts
Re: How to use DataGrid to sort Data per page?
Jan 21, 2008 07:41 AM|LINK
Actually what I was saying is to implement the custom paging for the datagrid. Suppose if you just implement the inbuilt paging then it will get all the data from the database and then if you try to sort the same then it will sort the complete set of the data and then will display the data for you thats why it will not be sorted page wise. But if you use custom paging then you will just get only the records for one page and then if you try to sort it will sort the data for that page only. In order to know how to implement custom paging you can take help from this article.
http://www.c-sharpcorner.com/UploadFile/sd_patel/CustomPagingInDataGrid11232005232703PM/CustomPagingInDataGrid.aspx
http://aspnet.4guysfromrolla.com/articles/012903-1.aspx
jcjcjc
Member
64 Points
639 Posts
Re: How to use DataGrid to sort Data per page?
Jan 22, 2008 02:08 AM|LINK
why I didn't use custom paging of DataGrid is because datagrid give all the columns of Title have sorting function
if I only want to allow one of them can be sorting .. is still good to use custom paging?
thank you again
apurva kaush...
Participant
1390 Points
302 Posts
Re: How to use DataGrid to sort Data per page?
Jan 22, 2008 06:31 AM|LINK
Yes still you can use custom paging and along with custom paging you can also implement custom sorting which will make you to make certain columns sortable.
jcjcjc
Member
64 Points
639 Posts
Re: How to use DataGrid to sort Data per page?
Jan 22, 2008 06:50 AM|LINK
hm.. I was confused ...
I think I am using Custom paging now?
or.. ? (I am sorry ... still can't really understand what do you mean.. can you give a sample example for me? )
thank you
apurva kaush...
Participant
1390 Points
302 Posts
Re: How to use DataGrid to sort Data per page?
Jan 22, 2008 07:24 AM|LINK
Apologies for not clarifying the matter initially. Actually what exactly is required for that is first when bind the datagrid with the first page data then store the same result set (first page data) in viewstate (in form of datatable) and then when you click on sort link sort the data in the datatable and bind it with the datagrid. Steps for this can be like this:
first: Get the first page data from the database (preferably using custom paging) and bind it with the datagrid. Also store this datatable (first page data) and put this in viewstate.
Second: When user clicks on the sort link then just sort the data available in the viewstate datatable and bind that to the datagrid.
Hope this make sense.