Asked by:
Sorting Expression of RowDataBound Fill Data inside grid-view

Question
-
User-471420332 posted
Sorting Expression of RowDataBound Fill Data inside grid-view, I have below <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15815732871779845873336">gridview</gwmw>
<asp:GridView ID="gv_lastlogindetails" runat="server" DataKeyNames="DistrictId,UserName" BorderColor="White" OnRowDataBound="gv_lastlogindetails_RowDataBound" AutoGenerateColumns="false" CssClass="table table-bordered alert-master" AllowSorting="true" OnSorting="gv_lastlogindetails_Sorting"> <Columns> <asp:TemplateField HeaderText="Role Name" SortExpression="RoleName" > <ItemTemplate> <asp:Label ID="lbl_RoleName" runat="server" Text='<%# Eval("RoleName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="District" SortExpression="District" > <ItemTemplate> <asp:Label ID="lbl_District" runat="server" Text='<%# Eval("District") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Inspection Body Name" SortExpression="FullName" > <ItemTemplate> <asp:Label ID="lbl_FullName" runat="server" Text='<%# Eval("FullName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Email Id" SortExpression="EmailId" > <ItemTemplate> <asp:Label ID="lbl_EmailId" runat="server" Text='<%# Eval("EmailId") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Mobile" SortExpression="Mobile" > <ItemTemplate> <asp:Label ID="lbl_Mobile" runat="server" Text='<%# Eval("Mobile") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="No of Times Visited" > <ItemTemplate> <asp:Label ID="lbl_NofTimesVisited" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Last Login Time" ItemStyle-Width="12%"> <ItemTemplate> <asp:Label ID="lbl_LastLoginTime" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Last Login SystemIp"> <ItemTemplate> <asp:Label ID="lbl_LastLoginSystemIp" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="No of days from last login"> <ItemTemplate> <asp:Label ID="lbl_Noofdaysfromlastlogin" runat="server" /> </ItemTemplate> </asp:TemplateField> </Columns> <EmptyDataTemplate>No Records</EmptyDataTemplate> </asp:GridView>
Below is my Sorting Method
protected void gv_lastlogindetails_Sorting(object sender, GridViewSortEventArgs e) { string sortingDirection = string.Empty; //Assigning sort condition if (direction == SortDirection.Ascending) { direction = SortDirection.Descending; sortingDirection = "Desc"; sortImage.ImageUrl = "img/view_sort_descending.png"; //var span = new HtmlGenericControl("span"); //span.Attributes["class"] = "fa fa-sort-alpha-desc"; //sortImage.Controls.Add(span); } else { direction = SortDirection.Ascending; sortingDirection = "Asc"; sortImage.ImageUrl = "img/view_sort_ascending.png"; //var span = new HtmlGenericControl("span"); //span.Attributes["class"] = "fa fa-sort-alpha-asc"; //sortImage.Controls.Add(span); } FillGrid(); DataTable dtSortTable = gv_lastlogindetails.DataSource as DataTable; //get gridview datatable if (dtSortTable != null) { DataView sortedView = new DataView(dtSortTable); sortedView.Sort = e.SortExpression + " " + sortingDirection;//sort using the direction assigned previously Session["SortedView"] = sortedView; gv_lastlogindetails.DataSource = sortedView; gv_lastlogindetails.DataBind(); int columnIndex = 0; foreach (DataControlFieldHeaderCell headerCell in gv_lastlogindetails.HeaderRow.Cells) { if (headerCell.ContainingField.SortExpression == e.SortExpression) { columnIndex = gv_lastlogindetails.HeaderRow.Cells.GetCellIndex(headerCell); } } gv_lastlogindetails.HeaderRow.Cells[columnIndex].Controls.Add(sortImage);//setting sort Image in the header row } }
Below is my 4 <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15815732922793422686831">rowdatabound</gwmw> field filling No of Times Visited<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15815732922792002463740">,</gwmw>Last Login Time<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15815732922794852188354">,</gwmw>Last Login SystemIp and No of days<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15815732922790083808105">.</gwmw>I am able to sort First 4 Template field because <gwmw class="ginger-module-highlighter-mistake-type-2" id="gwmw-15815732922790543795199">its</gwmw> directly filling the last four not able to sort because its binding from <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15815732922795275432619">rowdatabound</gwmw> field.
protected void gv_lastlogindetails_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (!string.IsNullOrEmpty(Session["Username"] as string) && !string.IsNullOrEmpty(Session["DistrictId"] as string)) { string defaultdate = "01/01/1900"; bo.Dateused = DateTime.ParseExact(defaultdate.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture); bo.Dateused1 = DateTime.ParseExact(defaultdate.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture); if (Session["RoleId"].ToString() == "2")/ { if (ddl_District.SelectedValue.ToString() == "0") { bo.Num1 = 0; } else { bo.Num1 = int.Parse(ddl_District.SelectedValue.ToString()); } } else { bo.Num1 = int.Parse(Session["DistrictId"].ToString());//districtId //OTHER ROLES } bo.Num2 = 2;//Paravalue bo.Num3 = 0;//CCId if (Session["RoleId"].ToString() == "2")/ { bo.Para1 = gv_lastlogindetails.DataKeys[e.Row.RowIndex].Values[1].ToString();//username } else { bo.Para1 = Session["Username"].ToString();//UserName other roles } DataTable dt = bl.Get_MisReports(bo); if (dt.Rows.Count > 0) { ((Label)e.Row.FindControl("lbl_NofTimesVisited")).Text = dt.Rows[0]["NofTimesVisited"].ToString(); ((Label)e.Row.FindControl("lbl_LastLoginTime")).Text = dt.Rows[0]["LastLoginTime"].ToString(); ((Label)e.Row.FindControl("lbl_LastLoginSystemIp")).Text = dt.Rows[0]["LastLoginSystemIp"].ToString(); ((Label)e.Row.FindControl("lbl_Noofdaysfromlastlogin")).Text = dt.Rows[0]["Noofdaysfromlastlogin"].ToString(); } else { ((Label)e.Row.FindControl("lbl_NofTimesVisited")).Text = "0"; ((Label)e.Row.FindControl("lbl_LastLoginTime")).Text = "0"; ((Label)e.Row.FindControl("lbl_LastLoginSystemIp")).Text = "0"; ((Label)e.Row.FindControl("lbl_Noofdaysfromlastlogin")).Text = "0"; } } else { Response.Redirect("sessionexpired.aspx"); Session.Clear(); Session.Abandon(); } }
Below <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15815732942800953952500">i</gwmw> am filling <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15815732942808931123379">gridview</gwmw>
protected void FillGrid() { if (!string.IsNullOrEmpty(Session["Username"] as string) && !string.IsNullOrEmpty(Session["DistrictId"] as string)) { string defaultdate = "01/01/1900"; bo.Dateused = DateTime.ParseExact(defaultdate.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture); bo.Dateused1 = DateTime.ParseExact(defaultdate.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture); if (Session["RoleId"].ToString() == "2") { if (ddl_District.SelectedValue.ToString() == "0") { bo.Num1 = 0; } else { bo.Num1 =int.Parse(ddl_District.SelectedValue.ToString()); } } else { bo.Num1 = int.Parse(Session["DistrictId"].ToString());// } bo.Num2 = 1;//Paravalue bo.Para1 = Session["Username"].ToString();//UserName bo.Num3 = 0;//CCId DataTable dt = bl.Get_MisReports(bo); gv_lastlogindetails.DataSource = dt; gv_lastlogindetails.DataBind(); if (dt.Rows.Count > 0) { } else { DataTable dtempty = new DataTable(); gv_lastlogindetails.DataSource = dtempty; gv_lastlogindetails.DataBind(); if (Session["RoleId"].ToString() == "2")//IF SIC { pnl_district.Attributes.CssStyle.Add("display", "block"); } else { pnl_district.Attributes.CssStyle.Add("display", "none"); } } } else { Response.Redirect("sessionexpired.aspx"); Session.Clear(); Session.Abandon(); } }
My question is I want to keep
SortExpression
like first four and same like last four template fields<gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15815732982813279765740">.</gwmw>First four <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15815732982813035612636">sortexpression</gwmw> working <gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15815732982816743946306">fine but</gwmw> <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15815732982813203961795">i</gwmw> want to add last four sorting <gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15815732982817131085585">expression</gwmw> which <gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15815732982819845853401">is filling</gwmw> from <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15815732982825903896913">rowdatabound</gwmw> field.Thursday, February 13, 2020 5:55 AM
All replies
-
User288213138 posted
Hi mazhar,
My question is I want to keepSortExpression
like first four and same like last four template fields.First four sortexpression working fine but i want to add last four sorting expression which is filling from rowdatabound fieldAccording to your post code, I cannot reproduce your problem.
First of all, you did not give some session values in your code. What are "bo" and "bl"? Some controls are missing in the aspx page, such as ddl_District, ddl_District.
So please post the complate code than can reproduce your question.
Best regards,
Sam
Thursday, February 13, 2020 8:37 AM -
User-471420332 posted
I am not getting why you are asking every think did you understand my requirements?
Friday, February 14, 2020 7:08 AM -
User288213138 posted
Hi mazhar,
I am not getting why you are asking every think did you understand my requirements?Don't you want to sort the 4 columns behind the GridView like the first 4 columns?
Best regards,
Sam
Friday, February 14, 2020 7:12 AM -
User-471420332 posted
I want to sort all <gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15816648761007470458209">column</gwmw> of the grid-view that is my question. And starting four <gwmw class="ginger-module-highlighter-mistake-type-3" id="gwmw-15816649154988685845320">column</gwmw> already <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15816649154982998158245">i</gwmw> am sorting last four <gwmw class="ginger-module-highlighter-mistake-type-1" id="gwmw-15816649154983989018069">i</gwmw> am not able to do sorting, because data is coming from data bound.
Friday, February 14, 2020 7:22 AM -
User288213138 posted
Hi mazhar,
I want to sort all column of the grid-view that is my question. And starting four column already i am sorting last four i am not able to do sorting, because data is coming from data bound.This demo for you as a reference.
<asp:GridView AllowSorting="true" runat="server" ID="GridView1" AutoGenerateColumns="false" DataKeyNames="Number" OnSorting="GridView1_Sorting" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:TemplateField HeaderText="Actual Value"> <ItemTemplate> <%# Eval("Number") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Calculated Value" SortExpression="Value"> <ItemTemplate> <asp:Label ID="Label1" runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GridView1.DataSource = new[] { new { Number = 8 }, new { Number = 1 }, new { Number = 6 }, new { Number = 4 } }; GridView1.DataBind(); } } protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { //Get GridViewRows var rows = GridView1.Rows.Cast<GridViewRow>().Select(a => new { Number = Convert.ToInt32(GridView1.DataKeys[a.RowIndex].Value), NumberText = ((Label)a.FindControl("Label1")).Text }); //Get Sort Direction accordingly SortDirection sortDirection = ViewState["SortOrder"] == null ? SortDirection.Descending : (SortDirection)Enum.Parse(typeof(SortDirection), ViewState["SortOrder"].ToString()) == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending; ViewState["SortOrder"] = sortDirection; if (sortDirection == SortDirection.Ascending) rows = rows.OrderBy(a => a.NumberText); else rows = rows.OrderByDescending(a => a.NumberText); GridView1.DataSource = rows.ToList(); GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Label Label1 = (Label)e.Row.FindControl("Label1"); //Check if first time data binding or sorting if (ViewState["SortOrder"] == null) Label1.Text = string.Format("Number {0}", e.Row.RowIndex + 1); else Label1.Text = DataBinder.Eval(e.Row.DataItem, "NumberText").ToString(); } }
Best regards,
Sam
Friday, February 14, 2020 9:44 AM