在datagridview C#中仅显示日期 [英] Show only date in datagridview C#

查看:186
本文介绍了在datagridview C#中仅显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我想在我的datagridview c#中只显示日期(没有时间)。

任何帮助都将不胜感激。

我尝试过以下方法:



 dataGridView1.Columns [ 2 ]。DefaultCellStyle.Format =   yyyy'/'MM'/'dd; 





这是我的方法:

  private   void  bindListToGridView1(List< Item> list)
{
尝试
{
dataGridView1.AllowUserToAddRows = false ;
ListtoDataTableConverter converter = new ListtoDataTableConverter();
DataTable dt2 = converter.ToDataTable(listToPresent);

dataGridView1.DataSource = null ;

使用(dt2)
{
dataGridView1.AutoGenerateColumns = false ;
dataGridView1.ColumnCount = 3 ;

dataGridView1.Columns [ 0 ]。Name = ID;
dataGridView1.Columns [ 0 ]。HeaderText = ID;
dataGridView1.Columns [ 0 ]。DataPropertyName = ID;

dataGridView1.Columns [ 1 ]。Name = ITEMNAME;
dataGridView1.Columns [ 1 ]。HeaderText = 项目名称;
dataGridView1.Columns [ 1 ]。DataPropertyName = ITEMNAME;

dataGridView1.Columns [ 2 ]。Name = productionDate;
dataGridView1.Columns [ 2 ]。HeaderText = 生产日期;
dataGridView1.Columns [ 2 ]。DataPropertyName = productionDate;
dataGridView1.Columns [ 2 ]。DefaultCellStyle.Format = YYYY '/' MM '/' DD;

dataGridView1.DataSource = dt2;
}
}
catch (例外情况)
{
}
}





我的尝试:



< pre lang =c#> dataGridView1.Columns [ 2 ]。DefaultCellStyle.Format = yyyy'/'MM'/'dd;

解决方案

这段代码将以您要查找的格式提供日期。

  string  myDate = DateTime.Now。 ToString(  yyyy / MM / dd



将日期转换为所需格式的最佳位置将是将List转换为Datatable的方法,因此当数据绑定到datagrid时,您不必执行所有这些操作。



为此你需要修改fy转换器方法,您可以通过名称识别属性并更改格式。



希望这会有所帮助。


我得到了帮助解决了我的问题,我想分享解决方案。

我更改了这一行:

 dataTable.Columns.Add(prop。名称,prop.PropertyType); 


Hello all. I want to display only date( without time ) in my datagridview c#.
Any help would be appreciated.
I have tried the following:

dataGridView1.Columns[2].DefaultCellStyle.Format = "yyyy'/'MM'/'dd";



This is my method:

private void bindListToGridView1(List<Item> list)
        {
            try
            {
                dataGridView1.AllowUserToAddRows = false;
                ListtoDataTableConverter converter = new ListtoDataTableConverter();
                DataTable dt2 = converter.ToDataTable(listToPresent);

                dataGridView1.DataSource = null;

                using (dt2)
                {
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.ColumnCount = 3;
                    
                    dataGridView1.Columns[0].Name = "id";
                    dataGridView1.Columns[0].HeaderText = "ID";
                    dataGridView1.Columns[0].DataPropertyName = "id";

                    dataGridView1.Columns[1].Name = "itemName";
                    dataGridView1.Columns[1].HeaderText = "Item Name";
                    dataGridView1.Columns[1].DataPropertyName = "itemName";

                    dataGridView1.Columns[2].Name = "productionDate";
                    dataGridView1.Columns[2].HeaderText = "Production Date";
                    dataGridView1.Columns[2].DataPropertyName = "productionDate";
                    dataGridView1.Columns[2].DefaultCellStyle.Format = "yyyy'/'MM'/'dd";

                    dataGridView1.DataSource = dt2;
                }
            }
            catch (Exception ex)
            {
            }
        }



What I have tried:

dataGridView1.Columns[2].DefaultCellStyle.Format = "yyyy'/'MM'/'dd";

解决方案

This piece of code will give you the date in the format you are looking for.

string myDate = DateTime.Now.ToString("yyyy/MM/dd")


The best place to convert the date to the format you want will be in the method where you convert List to Datatable, so you don't have to do all these when the data is bound to the datagrid.

For this you have to modify the converter method, where you identify the property by name and change the format.

Hope this helps.


I got help that solved my problem and i want to share the solution.
I changed this line:

dataTable.Columns.Add(prop.Name, prop.PropertyType);


这篇关于在datagridview C#中仅显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆