Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void grdv_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
          {
              e.Row.Cells[0].Visible = true;
              e.Row.Cells[1].Visible = false;
              e.Row.Cells[2].Visible = false;
              e.Row.Cells[3].Visible = true;
              e.Row.Cells[4].Visible = false;
              e.Row.Cells[5].Visible = true;
              e.Row.Cells[6].Visible = true;
              e.Row.Cells[7].Visible = true;
              e.Row.Cells[8].Visible = false;



          }
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              foreach (TableCell cell in e.Row.Cells)
              {
                  string s = cell.Text;
                  if (cell.Text == "A")
                  {
                      cell.Text = "Active";
                  }
                  if (cell.Text == "I")
                  {
                      cell.Text = "Inactive";
                  }
              }


              DateTime conv = Convert.ToDateTime(e.Row.Cells[3].Text);

              string dat = conv.ToString("dd-MMM-yy");
              e.Row.Cells[3].Text = dat;
              e.Row.Cells[6].Text = dat;

          }
          if (grdv.Rows.Count > 0)
          {
              grdv.HeaderRow.Cells[0].Text = "Edit";
              grdv.HeaderRow.Cells[1].Text = "Attendance Detail Reference";
              grdv.HeaderRow.Cells[2].Text = "Course Detail Reference";
              grdv.HeaderRow.Cells[3].Text = "Date Of Attend";
              grdv.HeaderRow.Cells[4].Text = "Attendance ";
              grdv.HeaderRow.Cells[5].Text = "Reason ";
              grdv.HeaderRow.Cells[6].Text = "Last Updated Date";
              grdv.HeaderRow.Cells[7].Text = "Last Updated By";
              grdv.HeaderRow.Cells[8].Text = "Student id";

          }
      }
DateTime conv = Convert.ToDateTime(e.Row.Cells[3].Text); --i have this line error.this error is String was not recognized as a valid DateTime.
Posted
Comments
DamithSL 7-Jun-14 1:36am    
how you store datetime column data in database? what is the column type? if you store datetime as varchar then you better change that to datetime. or otherwise you need to tell us what is the date time string format you used to store the date time as string
jeevakumar.T 7-Jun-14 2:26am    
I stored table column name DateTime Not null default (getdate()).
DamithSL 7-Jun-14 2:28am    
can you debug and check the value you get as e.Row.Cells[3].Text ?
jeevakumar.T 7-Jun-14 3:19am    
yes,I give this one txtdatofaten.Text = Server.HtmlDecode(row.Cells[3].Text);

if you know the date time format of the string in your database, you can use one of below method to convert it to a DateTime

DateTime.TryParseExact Method[^]
DateTime.ParseExact Method[^]
sample code:
C#
string dateString = "Mon 16 Jun 8:30 AM 2008"; // <-- Valid
string format = "ddd dd MMM h:mm tt yyyy";
DateTime dateTime;
if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture,
    DateTimeStyles.None, out dateTime))
{
    Console.WriteLine(dateTime);
}
 
Share this answer
 
v2
Try this code .. You will not get this error again... AARIF

DateTime date = DateTime.ParseExact(e.Row.Cells[3].Text, "dd/MM/yyyy", null);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900