Click here to Skip to main content
15,904,817 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,

in my application i wanted to print the contents of a data gridview which contains some columns like name, address,date etc...so for taking print i am using print document option and all the fields are coming in the print preview page also, but the problem is in the date field dates are coming along with a system generated time like 1988-02-05 12:0000AM, i wanted to avoid this time portion what is the solution for that in the grid view it is 1988-02-05 only at the time of printing only it is coming like that

Thanks
Ginnas
Posted
Comments
CHill60 19-Apr-13 5:43am    
How are you populating the cells that contain a datetime on the datagridview? If you format them correctly first they should appear in the preview in that format

for formtting the string you can use DateTime.ToShortDateString();
 
Share this answer
 
Comments
ginnas 19-Apr-13 10:12am    
thanks

here the problem only at the time of displaying in the print preview page,in the database and in the grid view and all displaying in correct format

Regards
Ginnas
HI,

Use the string.Format() or variable.Tostring("dd/mm/yyyy");

This will help you...
Thanks
 
Share this answer
 
Hi Ginnas,

I have added comments for you to understand the Code well .

Before binding it to the Grid View or Data Grid
// Get Clone of Data (only structure)
C#
   DataTable mydt = dt.Clone();

          //Convert the datetime column to string
          foreach (DataColumn column in mydt.Columns)
          {
              if (column.DataType == typeof(System.DateTime))
              {
                  column.DataType = typeof(System.String);
              }
          }

          //copy the data iterating through row and column
          for (int i = 0; i < dt.Rows.Count; i++)
          {
              // Add a new data row
              DataRow dr = mydt.NewRow();
              for (int j = 0; j < dt.Columns.Count; j++)
              {
                  //if not datetime copy directly
                  if (dt.Columns[j].DataType != typeof(System.DateTime))
                  {
                      dr[j] = dt.Rows[i][j];
                  }

//if datetime convert to string, split by ' ' and get the first element of the resulting array.
                  else
                  {
                      dr[j] = Convert.ToString(Convert.ToString(dt.Rows[i][j]).Split(' ')[0]);
                  }
              }
              // add data row to the table
              mydt.Rows.Add(dr);

              mydt.AcceptChanges();
          }



          GridView1.DataSource = mydt;
 
Share this answer
 
v2
You can also changed format of time like that DateTime.Now.ToString("yyyy/dd/mm");

DateTime.Now.ToString("dd/mm/yyyy");
 
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