数据网格视图中的自定义日期格式

时间:2015-02-04 05:44:26

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

当我从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); }
        }

1 个答案:

答案 0 :(得分:2)

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

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