I am trying to display a SQL server table in asp.net gridview. There are lot of tables in SQL server database and user selects a table and that table should be displayed in gridview. For all the datetime columns in sql server table, I need to display dates only.
I read the data by using SQLDataReader and loaded the data into a datatable. If the cell is of date type, then I am trying to convert it to date only format as shown in below image.
I tired converting the date using date.toString("MM/dd/yyyy") but unable to edit it in the datatable. The variable a in the image is showing the entire date with time. Can anyone help with the changes required to made so that only date is displayed in the date column.
I initially tried pushing variable b value to datatable but unable to succeed, then I tried to convert it to date again and tried. Nothing changed :(
edit: Here's the code
for (int i =0; i<table.Rows.Count;i++)
{
DataRow row = table.Rows[i];
for (int j = 0; j < table.Columns.Count; j++)
{
string t = table.Rows[i][j].GetType().ToString();
if (table.Rows[i][j].GetType().Equals(typeof(DateTime)))
{
mydate = DateTime.Parse(table.Rows[i][j].ToString());
var b = mydate.Month + "/" + mydate.Day + "/" + mydate.Year;
var c = DateTime.Parse(b.ToString());
var d = c.ToString("MM/dd/yyyy");
table.Rows[i].SetField(j, c.ToString("MM/dd/yyyy"));
var a = table.Rows[i][j];
}
}
}