Sorting Data gridview by clicking header in asp.net - asp.net

Please help me to sort the data in a grid view by clicking header in asp.net. i have used linq for bind data to gridview. Please help me.

You can do it very easily.
So lets say you have a GridView, to which you assign the datasource at the server side.
you can make use of GridView_Sorting event, some thing like given below:
firstly, save the current applied sort somewhere. because, you need to know, whether you have to sort ascending or descending. something like below.
public SortDirection CurrentSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection) ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}
and then use this property inside the sorting event of the GridView:
protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
if (CurrentSortDirection== SortDirection.Ascending)
{
CurrentSortDirection = SortDirection.Descending;
var myDataSource = GetDataThroughLinq()
.OrderByDescending(s=>s.Id)
.ToList();
GridView1.DataSource = myDataSource;
GridView1.DataBind();
}
else
{
CurrentSortDirection = SortDirection.Ascending;
var myDataSource = GetDataThroughLinq()
.OrderBy(s=>s.Id)
.ToList();
GridView1.DataSource = myDataSource;
GridView1.DataBind();
}
}

You can directly choose a datasource for the gridview, its in gridview tasks just below the auto format, after selecting datasouce more option is provided it includes sorting, paging and selection too, and by clicking on every heading, you will get the data sorted according to it

Try this for sorting.....
protected void RadgvData_SortCommand(object sender, GridSortCommandEventArgs e)
{
GridTableView tableView = e.Item.OwnerTableView;
e.Canceled = true;
GridSortExpression expression = new GridSortExpression();
expression.FieldName = e.SortExpression;
if (tableView.SortExpressions.Count == 0 || tableView.SortExpressions[0].FieldName != e.SortExpression)
{
expression.SortOrder = GridSortOrder.Descending;
}
else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Descending)
{
expression.SortOrder = GridSortOrder.Ascending;
}
else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Ascending)
{
expression.SortOrder = GridSortOrder.Descending;
}
tableView.SortExpressions.AddSortExpression(expression);
RadgvData.Rebind();
}

Related

GridVIew does not refresh after paging

My GridView is populated with a certain filter criteria.
I have PageIndexChanging event to perform pagination of my data.
protected void gvPeople_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
lblAddError.Text = String.Empty;
List<People> list = (List<People>)ViewState[_vsPeopleList];
gvPeople.DataSource = list;
gvPeople.PageIndex = e.NewPageIndex;
gvPeople.EditIndex = -1;
gvPeople.DataBind();
}
After that I perfrom Search without changing the filter criteria.
protected void btnPeopleSearch_Click(object sender, CommandEventArgs e)
{
if(e.CommandName.Equals("Search"))
{
PopulatePeople();
}
}
private void PopulatePeople()
{
lblAddError.Text = String.Empty;
if (ViewState["_message"] != null)
{
lblAddError.Visible = true;
lblAddError.Text = ViewState["_message"].ToString();
}
ViewState["_message"] = null;
int portfolio = int.Parse(ddlPortfolio.SelectedItem.Value);
ViewState["portfolioID"] = portfolio;
string year = ddlYear.SelectedItem.Text;
string month = ddlMonth.SelectedItem.Text;
List<People> list = People.GetPeople(portfolio, year, month);
ViewState[_vsPeopleList] = list;
if(list.Count == 0)
{
gvPeople.Visible = false;
lblAddError.Visible = true;
lblAddError.Text = "No data available for current selection";
}
else
{
gvPeople.Visible = true;
gvPeople.DataSource = list;
gvPeople.DataBind();
}
}
I'm supposed to get all the data in the GridView as I had before paging to the next page. However, my page does not refresh to show all the data, it only shows the data for that page index, however, my list has count equals to the number of records populated from the database. Also, if I change search filter, data is displayed correctly.
What am I doing wrong?
Looks like the gvHolidays_PageIndexChanging event is related to some other grid as the code written within it is for grid gvPeople.
If not, then try the following.
//Write the following code inside gridviews PageIndexChanging event.
gvPeople.PageIndex = e.NewPageIndex;
gvPeople.EditIndex = -1;
gvPeople.SelectedIndex = -1;
//Write the following code inside gvHolidays_PageIndexChanged event.
lblAddError.Text = String.Empty;
List<People> list = (List<People>)ViewState[_vsPeopleList];
gvPeople.DataSource = list;
gvPeople.DataBind();

Add To The Top in the DataList

This My code For binding My DataList ,and every item in datalist have a different button ,
the items sorted correctly by date but the index for each item not sorted with it ,
ex:
when insert a new data in employees table the data shows correct(sorted by date),the last employee was insert into employee table shows in the first item and take the index 0.
I want to know how i can to make his index The Last Index in my old data + 1 ?
private void bind()
{
da2 = new SqlDataAdapter("select * from employees order by insert_date desc", m_SqlConnection);
DataSet dataSet2 = new DataSet();
da2.Fill(dataSet2, "det");
DataList1.DataSource = dataSet2.Tables["det"];
DataList1.DataBind();
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
Button btn = (Button)e.Item.FindControl("button4");
Button btn2 = (Button)e.Item.FindControl("button1");
da2 = new SqlDataAdapter("select insert_stat from insert_detail where user_id='" + int.Parse(Session["id"].ToString()) + "'", m_SqlConnection);
DataSet dataSet2 = new DataSet();
da2.Fill(dataSet2, "chk");
if (dataSet2.Tables["chk"].Rows[e.Item.ItemIndex]["insert_stat"].ToString() == "accept")
{
btn.Visible = true;
btn2.Visible = true;
}
else
{
if (dataSet2.Tables["chk"].Rows[e.Item.ItemIndex]["insert_stat"].ToString() == "reject")
{
btn.Visible = false;
btn2.Visible = false;
}
}
}
This is more a SQL problem than a DataList issue.
I recommend that you rewrite your SQL in the bind() method to do a JOIN on the insert_detail table to get the accept or reject value out of the table and into the bound data set for the data list. This serves two benefits:
It eliminates the database call in the DataList1_ItemDataBound() method, which if you have dozens or hundreds of rows, then that is dozens or hundreds less database calls.
It allows you to put the insert_stat value into a HiddenField control in your DataList and then check the value in the DataList1_ItemDataBound event, like this:
HiddenField theHiddenField = e.Item.FindControl("HiddenField1") as HiddenField;
// Make sure we found the control, because the as operator
// returns null for a failed cast
if(theHiddenField != null)
{
if(theHiddenField.Value.ToLower() == "accept")
{
btn.Visible = true;
btn2.Visible = true;
}
else
{
btn.Visible = false;
btn2.Visible = false;
}
}

accessing selected checkboxes in gridview

i have a gridview in which i am using checkbox in each row. i am trying to access checkbox of each row and trying to find out which checkboxes have been checked.buut when i try to run the below code.the condition always stands to be false and the inner if condition is never reached by the code.kindly help me.thanks in advance.
protected void btn_3id_Click(object sender, EventArgs e)
{
string str = "";
string srr = "";
for (int i = 0; i < GridView1.Rows.Count;i++ )
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (chk.Checked==true)
{
if (str == "")
{
str = GridView1.Rows[i].Cells[0].Text.ToString();
}
else
{
srr = str + "," + GridView1.Rows[i].Cells[0].Text.ToString();
}
}
}
Session["Card_id"] = str;
Response.Redirect("ID.aspx");
}
The code looks fine.
The problem could be you are binding the gridview at page load.
Try grid binding in the following section of page load
if(!Page.IsPostBack)
{
//code to bind the gridview
}
I can only guess that you are binding your gridview on each page load without checking PostBack. That is causing the checkbox to loose its current state. So where you are assigning the DataSource to the Gridview , Check for PostBack like:
if(!Page.IsPostBack)
{
GridView1.DataSource = yourDataSource;
GridView1.DataBind();
}
also you can do some minor improvements in your code like your check:
if(chk.Checked == true)
can be replaced as:
if(chk.Checked) //Since it returns a bool value.
You can omit multiple string variables for concatenation. Its better if you use StringBuilder, (See why it is better) so your code would be:
protected void btn_3id_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < GridView1.Rows.Count;i++ )
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (chk.Checked==true)
{
sb.Append() GridView1.Rows[i].Cells[0].Text.ToString();
}
}
Session["Card_id"] = sb.ToString();
Response.Redirect("ID.aspx");
}
if(!Page.IsPostBack)
{
//
}
Postback plays important role in cs file. If you are clearing values on page load , you will null values of checkbox.
You code is fine.
Just try to do this...

How to add an extra row (containing a Button and corresponding event handler) to the GridView

This must be something that a lot of people have done. Basically, it's a custom GridView (i.e. inherited control) with the ability to update all rows at once. I've tried putting the "update all" button in various places (footer, pager, outside the grid), but it looks neatest (to me) when the button is in an extra row as the last row of the GridView.
NB: The pager row is not a suitable place for this button because this custom control could be used in a situation where paging is false. Similarly, the normal footer may be required for some other purpose (e.g. totals).
Here's my code for putting the button in the correct place (with apologies for the terse variables etc.):
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
//Add an extra row to the table...
if (_updateAllEnabled)
{
GridViewRow r = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
Button btn = new Button();
TableCell c = new TableCell();
btn.ID = "UpdateAllButton"; // tried with and without this line
btn.Text = "Update All";
btn.Click += new EventHandler(UpdateAll);
r.Cells.Add(c);
c.Controls.Add(btn);
Table t = this.Controls[0] as Table;
c.ColumnSpan = this.Columns.Count;
t.Rows.Add(r);
}
}
This gives the appearance that I want, but the click event (UpdateAll) does not fire.
I assume that the stuff is being added too late in the life cycle (PreRender), but where else can I do this to ensure that the row is at the end of the GridView? I also thought that there might be trouble identifying the button, so I tried setting the ID. In any case, the ID in the generated HTML looks OK (consistent with "working" buttons in the pager row.
Is there a way for me to achieve this or am I attempting the impossible?
The best place to create your footer-controls is RowCreated since that's early enough in the lifecycle and also ensures that their recreated on every postback:
Footer approach:
protected void Grid_RowCreated(Object sender, GridViewRowEventArgs e) {
if(e.Row.RowType == DataControlRowType.Footer) {
Button btn = new Button();
TableCell c = new TableCell();
btn.ID = "UpdateAllButton";
btn.Text = "Update All";
btn.Click += new EventHandler(UpdateAll);
var firstCell=e.Row.Cells[0];
firstCell.ColumnSpan =e.Row.Cells.Count;
firstCell.Controls.Add(btn);
while(e.Row.Cells.Count > 1)e.Row.Cells.RemoveAt(e.Row.Cells.Count-1);
}
}
Of course you have to set ShowFooter to true:
<asp:GridView ID="GridView1"
ShowFooter="true"
OnRowCreated="Grid_RowCreated"
runat="server"
</asp:GridView>
Pager approach:
In my opinion this is the purpose of the FooterRow. But if you really want to ensure that your Button is in the very last row of a GridView(even below Pager as commented), i would try my next approach.
Here I'm using the pager for your costom control(s) by adding another TableRow to the PagerTable which inherits from Table.
protected void Grid_RowCreated(Object sender, GridViewRowEventArgs e) {
switch(e.Row.RowType){
case DataControlRowType.Pager:
Button btnUpdate = new Button();
btnUpdate.ID = "UpdateButton";
btnUpdate.Text = "Update";
btnUpdate.Click += new EventHandler(UpdateAll);
var tblPager = (Table)e.Row.Cells[ 0 ].Controls[ 0 ];
var row = new TableRow();
var cell = new TableCell();
cell.ColumnSpan = tblPager.Rows[ 0 ].Cells.Count;
cell.Controls.Add(btnUpdate);
row.Cells.Add(cell);
tblPager.Rows.Add(row);
break;
}
}
To ensure that the pager is visible even if only one page is shown(note that the real pager is invisible if PageSize==1):
protected void Grid_PreRender(object sender, EventArgs e){
GridView gv = (GridView)sender;
GridViewRow gvr = (GridViewRow)gv.BottomPagerRow;
if(gvr != null) {
gvr.Visible = true;
var tblPager = (Table)gvr.Cells[ 0 ].Controls[ 0 ];
//hide real pager if unnecessary
tblPager.Rows[ 0 ].Visible = GridView1.PageCount > 1;
}
}
Of course now you have to set AllowPaging=true:
<asp:GridView ID="GridView1"
AllowPaging="true"
PagerSettings-Mode="NumericFirstLast"
OnRowCreated="Grid_RowCreated"
OnPreRender="Grid_PreRender"
OnPageIndexChanging="Grid_PageChanging"
runat="server">
</asp:GridView>
Final approach(working for a custom GridView and all PagerPositions):
public PagerPosition OriginalPagerPosition{
get { return (PagerPosition)ViewState[ "OriginalPagerPosition" ]; }
set { ViewState[ "OriginalPagerPosition" ] = value; }
}
protected void Page_Load(object sender, EventArgs e){
if(!IsPostBack) OriginalPagerPosition = GridView1.PagerSettings.Position;
GridView1.PagerSettings.Position = PagerPosition.TopAndBottom;
GridView1.AllowPaging = true;
// databinding stuff ...
}
Keep the RowCreated the same as above in Pager approach.
Visibility of top/bottom pagers will be controlled in PreRender according to the OriginalPagerPosition property. Both pagers are created even with PagerPosition=TOP, the bottom pager is required for your additional control(s):
protected void Grid_PreRender(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
GridViewRow tpr = (GridViewRow)gv.TopPagerRow;
GridViewRow bpr = (GridViewRow)gv.BottomPagerRow;
tpr.Visible = gv.PageCount > 1 && (OriginalPagerPosition == PagerPosition.Top || OriginalPagerPosition == PagerPosition.TopAndBottom);
bpr.Visible = true;
var tblBottomPager = (Table)bpr.Cells[ 0 ].Controls[ 0 ];
tblBottomPager.Rows[ 0 ].Visible = gv.PageCount > 1 && (OriginalPagerPosition == PagerPosition.Bottom || OriginalPagerPosition == PagerPosition.TopAndBottom);
var tblTopPager = (Table)tpr.Cells[ 0 ].Controls[ 0 ];
tblTopPager.Rows[1].Visible = false;
}
Note: if you are extending the GridView control, you have to replace all occurences of GridView1(my test-grid) with this.
It would be easy to add an extra row into the grid. But the difficulty in your requirement is that the GridView's RowCollection should not contain this row since that would be error-prone. It should also be the very last row even if paging is enabled. This is (afaik) not possible.
Hence i've chosen to extend the pager with this functionality.
I'll add this as separate answer since my other is already too detailed and describes two different ways(footer,pager) to add controls to a GridView without extending it.
This approach extends a GridView as in your own requirement and is similar to my other pager-approach. But it's cleaner and only adds the additional row to the BottomPager. It woks also for every setting(AllowPaging=false,Pager-Position: Top,Bottom,BottomTop):
[DefaultProperty("EnableUpdateAll")]
[ToolboxData("<{0}:UpdateGridView runat=server></{0}:UpdateGridView>")]
public class UpdateGridView : GridView
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("true")]
[Localizable(true)]
public bool EnableUpdateAll
{
get
{
Object val = ViewState["EnableUpdateAll"];
return ((val == null) ? true : (bool)val);
}
set
{
ViewState["EnableUpdateAll"] = value;
}
}
private bool OriginalAllowPaging
{
get
{
Object val = ViewState["OriginalAllowPaging"];
return (bool)val;
}
set
{
ViewState["OriginalAllowPaging"] = value;
}
}
private PagerPosition OriginalPagerPosition
{
get
{
Object val = ViewState["OriginalPagerPosition"];
return (PagerPosition)val;
}
set
{
ViewState["OriginalPagerPosition"] = value;
}
}
protected override void OnInit(System.EventArgs e)
{
if (ViewState["OriginalPagerPosition"] == null)
OriginalPagerPosition = base.PagerSettings.Position;
if(OriginalPagerPosition != PagerPosition.Bottom)
PagerSettings.Position=PagerPosition.TopAndBottom;
if (ViewState["OriginalAllowPaging"] == null)
OriginalAllowPaging = base.AllowPaging;
base.AllowPaging = true;
}
protected override void OnRowCreated(GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.Pager:
//check if we are in BottomPager
if (this.Rows.Count != 0 && this.EnableUpdateAll)
{
Button btnUpdate = new Button();
btnUpdate.ID = "BtnUpdate";
btnUpdate.Text = "Update";
btnUpdate.Click += new EventHandler(UpdateAll);
var tblPager = (Table)e.Row.Cells[0].Controls[0];
var row = new TableRow();
var cell = new TableCell();
cell.ColumnSpan = tblPager.Rows[0].Cells.Count;
cell.Controls.Add(btnUpdate);
row.Cells.Add(cell);
tblPager.Rows.Add(row);
}
break;
}
}
protected override void OnPreRender(EventArgs e)
{
bool bottomPagerVisible =
OriginalAllowPaging &&
PageCount > 1 &&
(OriginalPagerPosition == PagerPosition.Bottom || OriginalPagerPosition == PagerPosition.TopAndBottom);
BottomPagerRow.Visible = bottomPagerVisible || EnableUpdateAll;
var tblBottomPager = (Table)BottomPagerRow.Cells[0].Controls[0];
tblBottomPager.Rows[0].Visible = bottomPagerVisible;
}
private void UpdateAll(Object sender, EventArgs e)
{
// do something...
}
}

How to Sort on a GridView using ObjectDataSource with TemplateFields

Background:
I am working with a GridView and an ObjectDataSource. I am implementing Paging and Sorting.
On the ObjectDataSource:
objectDataSource.TypeName = value;
objectDataSource.SelectMethod = "Select";
objectDataSource.SelectCountMethod = "SelectCount";
objectDataSource.SortParameterName = "sortExpression";
objectDataSource.EnablePaging = true;
On the GridView:
gridView.AllowPaging = true;
gridView.AllowSorting = true;
gridView.DataSource = objectDataSource;
To get paging and sorting to work, I set "EnableSortingAndPagingCallbacks" to True. Before, I was getting a "System.Web.HttpException: The GridView fired event Sorting which wasn't handled." and this fixes it.
If I use only BoundFields in my GridView, this is great and works fine.
However, if I used TemplateFields, I get a "NotSupportedException: Callbacks are not supported on TemplateField because some controls cannot update properly in a callback. Turn callbacks off on GridView."
Which, makes sense. I just need to know how to make sorting work, without using EnableSortingAndPagingCallbacks.
If EnableSortingAndPagingCallbacks = True:
Paging Works
Sorting Works
BoundFields Work
TemplateFields do Not Work
If EnableSortingAndPagingCallbacks = False:
Paging Works
Sorting does Not Work
BoundFields Work
TemplateFields Work
My Question:
How do I go about getting Paging, Sorting, and TemplateFields to work, all at the same time?
Clarification on the implementation:
Using an ObjectDataSource with a GridView requires implementing a method called Select that provides a sort expression, the number of rows to return, and the start row:
public IEnumerable<CountyAndStateGridRow> Select(string sortExpression, int maximumRows, int startRowIndex)
{
string oql = "select County order by {" + sortExpression + "}" ;
var counties = QueryProvider.ExecuteQuery(oql).Cast<County>();
var page = counties.Skip(startRowIndex).Take(maximumRows);
var rows = page.Select(
county => new CountyAndStateGridRow
{
CountyName = county.Name,
StateName = county.State.Name,
});
return rows;
}
The specific SortExpression is defined in the aspx/ascx:
<Columns>
<asp:BoundField HeaderText="County Name" DataField="CountyName" SortExpression="Name" />
<asp:BoundField HeaderText="State Name" DataField="StateName" SortExpression="State.Name" />
</Columns>
This is supposed to be passed in and call the Select method on the ObjectDataSource when the column is clicked, but it does not seem to work if EnableSortingAndPagingCallbacks = true, and instead I get the exception about the Sorting event not being defined.
For sorting functionality to work:
<asp:GridView GridView ID="GvCountryDetails" AllowPaging="True"
OnPageIndexChanging="GvCountryDetails_PageIndexChanging" AllowSorting="True"
onsorting="GvCountryDetails_Sorting">
in .cs file you need to write
protected void GvCountryDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GvCountryDetails.PageIndex = e.NewPageIndex;
isPageIndexChanged = true;
BindData();
}
protected void GvCountryDetails_Sorting(object sender, GridViewSortEventArgs e)
{
sortExpression = e.SortExpression;
isPageIndexChanged = false;
BindData();
}
private void SortGridData()
{
string sSortdir;
if (isPageIndexChanged == true)
{
sSortdir = ViewState["SortDirection"] as string;
}
else
{
sSortdir = GetSortDirection(sortExpression);
}
string sSortExp = sortExpression;
if (sSortdir == "ASC")
{
lstCountryDetails = Sort<Country>(lstCountryDetails, sSortExp, SortDirection.Ascending);
}
else
{
lstCountryDetails = Sort<Country>(lstCountryDetails, sSortExp, SortDirection.Descending);
}
}
private List<CountryBO> Sort<TKey>(List<CountryBO> list, string sortBy, SortDirection direction)
{
PropertyInfo property = list.GetType().GetGenericArguments()[0].GetProperty(sortBy);
if (direction == SortDirection.Ascending)
{
return list.OrderBy(e => property.GetValue(e, null)).ToList<CountryBO>();
}
else
{
return list.OrderByDescending(e => property.GetValue(e, null)).ToList<Country>();
}
}
private string GetSortDirection(string column)
{
string sortDirection = "ASC";
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection = "DESC";
}
}
}
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
The property EnableSortingAndPagingCallbacks tells the control to do a client side sort of the data, so that the control appears to automatically sort without a page postback. TemplateFields are not supported with this method. In order to use TemplateFields and perform sorting, you need to wire up the GridView.Sorting event, and set the AllowSorting property to true. Once that is done, the event should fire when the column header is clicked and the sorting logic can be handled from there.
Change the SortExpression value with the value of DataField.
Set AllowPaging and AllowSorting to true.
Set EnableSortingAndPagingCallbacks to true.

Resources