To fix the issue, you need to update the ViewState["dirState"] with the sorted data table in the grdPlanning_Sorting event handler. Here's how you can do it:
protected void grdPlanning_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dtresult = (DataTable)ViewState["dirState"];
if (dtresult.Rows.Count > 0)
{
if (Convert.ToString(ViewState["sortdr"]) == "Asc")
{
dtresult.DefaultView.Sort = e.SortExpression + " Desc";
ViewState["sortdr"] = "Desc";
}
else
{
dtresult.DefaultView.Sort = e.SortExpression + " Asc";
ViewState["sortdr"] = "Asc";
}
dtresult = dtresult.DefaultView.ToTable(); // update the data table with sorted data
grdPlanning.DataSource = dtresult;
grdPlanning.DataBind();
ViewState["dirState"] = dtresult; // update the ViewState with sorted data table
}
}
Additionally, I noticed that you are adding two new columns to the data table dt in the GetData method. However, you are not setting the values for these columns in the foreach loop. You need to set the values for these columns in the foreach loop before binding the data to the grid view. Here's how you can do it:
if (dt != null)
{
DataColumn Remarks = new DataColumn("Remarks", typeof(string));
dt.Columns.Add(Remarks);
DataColumn Dates = new DataColumn("Date", typeof(DateTime));
dt.Columns.Add(Dates);
foreach (DataRow row in dt.Rows)
{
row["Remarks"] = ""; // set the value of Remarks column
row["Date"] = DateTime.MinValue; // set the value of Date column
}
dt.DefaultView.Sort = "Remarks Asc";
grdPlanning.DataSource = dt;
grdPlanning.DataBind();
ViewState["dirState"] = dt;
ViewState["sortdr"] = "Asc";
}
If still it didn't work then revert your changes in page laod and try
Based on the code provided, it seems that the sorting functionality is working correctly. However, you mentioned that the data is not sorted when you click on the Remarks column.
One possible reason for this issue could be that the Remarks column is added after the data is bound to the GridView, so it might not be included in the sorting logic. To fix this, you can modify the GetData() method to add the Remarks column before binding the data to the GridView. Here is an updated version of the method:
private void GetData()
{
SQLUtility sQLUtility = new SQLUtility();
DataTable dt = sQLUtility.GeBookPlanningRecommendation(vRpl, BookCollection, StudentCollection).Tables[0];
if (dt != null)
{
DataColumn Remarks = new DataColumn("Remarks", typeof(string));
dt.Columns.Add(Remarks);
DataColumn Dates = new DataColumn("Date", typeof(DateTime));
dt.Columns.Add(Dates);
// Add Remarks data to each row
foreach (DataRow row in dt.Rows)
{
row["Remarks"] = "some value";
}
// Sort the data by Remarks column initially
dt.DefaultView.Sort = "Remarks Asc";
grdPlanning.DataSource = dt;
grdPlanning.DataBind();
}
ltrlReportHead.Text = "Sessions Book Planning (" + dt.Rows.Count + " Records)";
ViewState["dirState"] = dt;
ViewState["sortdr"] = "Asc";
}
In this updated method, we first add the Remarks column to the DataTable and populate it with some values for each row. We also sort the data by the Remarks column initially so that it appears sorted when the page is first loaded.
Now, when you click on the Remarks column header to sort the data, the sorting should work correctly.
Rajkiran SwainPosted May 2, 2023, 5:56 AM
To fix the issue, you need to update the
ViewState["dirState"]with the sorted data table in thegrdPlanning_Sortingevent handler. Here's how you can do it:Additionally, I noticed that you are adding two new columns to the data table
dtin theGetDatamethod. However, you are not setting the values for these columns in theforeachloop. You need to set the values for these columns in theforeachloop before binding the data to the grid view. Here's how you can do it:If still it didn't work then revert your changes in page laod and try
Ramco RamcoPosted May 2, 2023, 5:44 AM
Hi RajKiran
I have written below code but still not working. Secondly in gr_RowDataBound Remarks value gets updated.
Only after that Gridview must be getting binded.
Rajkiran SwainPosted May 2, 2023, 5:33 AM
Based on the code provided, it seems that the sorting functionality is working correctly. However, you mentioned that the data is not sorted when you click on the Remarks column.
One possible reason for this issue could be that the Remarks column is added after the data is bound to the GridView, so it might not be included in the sorting logic. To fix this, you can modify the GetData() method to add the Remarks column before binding the data to the GridView. Here is an updated version of the method:
In this updated method, we first add the Remarks column to the DataTable and populate it with some values for each row. We also sort the data by the Remarks column initially so that it appears sorted when the page is first loaded.
Now, when you click on the Remarks column header to sort the data, the sorting should work correctly.
Ramco RamcoPosted May 2, 2023, 5:31 AM
Hi
Thanks
Rajkiran SwainPosted May 2, 2023, 5:27 AM
Need more information with code ?