【问题标题】:custom date format in data grid view数据网格视图中的自定义日期格式
【发布时间】:2015-02-04 05:44:25
【问题描述】:

当我在 datagridview 中从数据库中获取数据时,日期列中的日期会显示为例如。 2/1/2020 12:00:00 AM,我希望它仅以“MM/yyyy”格式显示。我试过了

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

但它只显示像这样 (2/1/2020 12:00:00 AM)。这条线在我的其他形式的项目中有效,但在此无效,我不知道为什么..

我只想显示月份和年份部分,所以应该是这样的

dataGridView1.Columns["P_exp_date"].DefaultCellStyle.Format = "MM/yyyy";

但以上都不起作用。请帮忙。

这是我的代码

private void LoadData(string P_id)
        {try{
            mode = FormMode.EditMode; //by editmode

            DataTable dt = DataBase.getDataTable("SELECT * FROM PRO_BILL INNER JOIN PRO ON PRO.P_id=PRO_BILL.P_id WHERE PRO_BILL.P_id=" + P_id + "");

            dataGridView1.Columns["P_exp_date"].DefaultCellStyle.Format = "dd/MM/yyyy";
            foreach (DataRow dr in dt.Rows)

            {
                int i = 0;
                i = dataGridView1.Rows.Add();
                dataGridView1.Rows[i].Cells[1].Value = dr["P_name"].ToString();
                dataGridView1.Rows[i].Cells["P_pack"].Value = dr["P_pack"].ToString();
                dataGridView1.Rows[i].Cells["P_comp"].Value = dr["P_comp"];
                dataGridView1.Rows[i].Cells["P_batch_no"].Value = dr["P_batchno"].ToString();
                dataGridView1.Rows[i].Cells["P_exp_date"].Value = dr["P_expdate"].ToString();
                dataGridView1.Rows[i].Cells["P_qty"].Value = dr["P_qty"].ToString();
                dataGridView1.Rows[i].Cells["P_free"].Value = dr["P_free"].ToString();
                dataGridView1.Rows[i].Cells["P_rate"].Value = dr["P_rate"].ToString();
                dataGridView1.Rows[i].Cells["P_mrp"].Value = dr["P_mrp"].ToString();
                dataGridView1.Rows[i].Cells["P_min_stock"].Value = dr["P_min_stk"].ToString();
                dataGridView1.Rows[i].Cells["P_max_stock"].Value = dr["P_max_stk"].ToString();
                dataGridView1.Rows[i].Cells["P_vat"].Value = dr["P_vat"].ToString();
                dataGridView1.Rows[i].Cells["P_total"].Value = dr["P_total"].ToString();
                dataGridView1.Rows[i].Cells["P_tabs"].Value = dr["P_tabs"].ToString();
                dataGridView1.Rows[i].Cells["P_sh"].Value = dr["P_sh"].ToString();
                dataGridView1.Rows[i].Cells["Pro_id"].Value = dr["Pro_id"].ToString();
                tbSupname.Text = dr["P_sname"].ToString();
                tbBILLNO.Text = dr["P_billno"].ToString();
                tbCHLNno.Text = dr["P_chno"].ToString();
                dateTimePicker1.Text = dr["P_purdate"].ToString();
                tbtot1.Text = dr["P_ttotal"].ToString();
                tbTOtvat.Text = dr["P_vatto"].ToString();
                tbDisTOT.Text = dr["P_totdis"].ToString();
                tbGT.Text = dr["P_gt"].ToString();
                tbPID.Text = dr["P_id"].ToString();
                dataGridView1.Rows[i].Cells["tabsNofree"].Value = dr["tabsNofree"].ToString();
                dataGridView1.Rows[i].Cells["Shelf_id"].Value = dr["Shelf_id"].ToString();
                dataGridView1.Rows[i].Cells["P_tabs"].Value = dr["P_tabs"].ToString();
                dataGridView1.Rows[i].Cells["P_otabs"].Value = dr["P_otabs"].ToString();
            }
        }
        catch (Exception ex)
        { MessageBox.Show(ex.Message); }
        }

【问题讨论】:

  • 在赋值时可以试试.ToString("yourformat")
  • @CoderofCode 你的意思是这样吗 dr["P_expdate"].ToString("MM/yyyy"); ?
  • 您好,您可以通过此链接获得帮助:stackoverflow.com/questions/22449788/…

标签: c# sql visual-studio-2010 date datagridview


【解决方案1】:

您可以尝试,将您的数据转换为 DateTime,然后使用您的格式应用 ToString

  dataGridView1.Rows[i].Cells["P_exp_date"].Value = (DateTime.Parse(dr["P_expdate"].ToString())).ToString("MM/yyyy");

【讨论】:

  • 这显示以下错误 1) 'System.DateTime.Parse(string)' 的最佳重载方法匹配有一些无效参数 参数 1:2) 无法从 'object' 转换为 'string'
  • 你在dr["P_expdate"].ToString()上添加了.ToString
猜你喜欢
  • 1970-01-01
  • 2014-08-17
  • 2017-10-23
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
  • 2021-03-31
  • 1970-01-01
相关资源
最近更新 更多