Hi
I have below Gridview. In below line it shows value . Last column is Date. It should be either space or Date
gr.Cells[gr.Cells.Count - 1]
C#
SQLUtility sQLUtility = new SQLUtility();
DataTable dt = sQLUtility.GeBookPlanningRecommendation(HRpl, 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);
}
}
*******************************
foreach (GridViewRow gr in grdPlanning.Rows)
{
//string sessionDate = "";
if (gr.RowType == DataControlRowType.DataRow)
{
if (gr.Cells[gr.Cells.Count - 1].Text != "")
{
}
}
}Thanks

Rajkiran SwainRank #165 · 14.4K reputationPosted May 5, 2023, 12:49 AM EDT
The
is an HTML non-breaking space code that is used to create a space between two words that cannot be separated by a line break. It seems like the value in the last column of yourGridViewis being displayed asinstead of a space or a valid date.To fix this, you can check the data that is being passed into the
GridViewand ensure that the last column contains a validDateTimevalue. You can also try removing any formatting or encoding that may be causing theto appear.As for the code snippet you provided, it seems like you are adding two columns "Remarks" and "Date" to a
DataTableobject and then looping through the rows of aGridViewto check if the value in the last cell is not empty. However, without seeing the entire code and the data being passed, it's difficult to determine the root cause of the issue.gr.Cells[gr.Cells.Count - 1].Text = myDate.ToString("MM/dd/yyyy");
Ramco RamcoRank #462 · 5.4K reputationPosted May 5, 2023, 12:54 AM EDT
Hi Rajkiran
In Gridview RowDataBound i have written this but still it is showing
row[e.Row.Cells.Count - 1] = "";
User will enter datein dd-mm-yyyy so what i should change. If it is valid Date then it should go further. Value in below should be valid Date
gr.Cells[gr.Cells.Count - 1].Text
Even if type of Date is declared as string thean also it shows . It should show empty if nothinh is entered in the column.
Thanks
Rajkiran SwainRank #165 · 14.4K reputationPosted May 5, 2023, 1:31 AM EDT
foreach (GridViewRow gr in grdPlanning.Rows) { if (gr.RowType == DataControlRowType.DataRow) { string dateString = gr.Cells[gr.Cells.Count - 1].Text.Trim(); if (!string.IsNullOrEmpty(dateString)) { DateTime date; if (DateTime.TryParseExact(dateString, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) { // The value is a valid date } else { // The value is not a valid date } } else { // The value is empty or contains only white space } } }Ramco RamcoRank #462 · 5.4K reputationPosted May 5, 2023, 1:39 AM EDT
Hi Rajkiran
Issue is it show value in dateString. I don't want to consider these rows. I have changed Date as string type also but still it is not working.
string dateString = gr.Cells[gr.Cells.Count - 1].Text.Trim();
if (!string.IsNullOrEmpty(dateString))
Ramco RamcoRank #462 · 5.4K reputationPosted May 5, 2023, 2:38 AM EDT
Hi RajKiran
I have below code in GridView RowDataBound
TextBox txtDate = new TextBox(); txtDate.CssClass = "form-control"; txtDate.DataBind(); e.Row.Cells[e.Row.Cells.Count - 1].Controls.Add(txtDate);Thanks