如何从打印预览页面中的日期字段中删除时间部分

问题描述:

你好,



在我的应用程序中我想打印数据gridview的内容,其中包含一些列,如名称,地址,日期等......所以打印我正在使用打印文档选项,所有字段也在打印预览页面中,但问题出在日期字段日期和系统生成的时间一样,如1988-02-05 12:0000AM,我想要为了避免这个时间部分什么是网格视图中的解决方案它是1988-02-05只在打印时才会出现这样的情况



谢谢

Ginnas

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

用于构建可以使用的字符串DateTime.ToShortDateString();
for formtting the string you can use DateTime.ToShortDateString();


HI,



使用string.Format()或variable.Tostring(dd / mm / yyyy);



这对你有帮助......

谢谢


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

This will help you...
Thanks


你好Ginnas,



我已添加评论供您理解他代码很好。



在将它绑定到网格视图或数据网格之前

//获取数据克隆(仅限结构)

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)
   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;