【问题标题】:Extract Date form DataGridView to DateTimePicker从 DataGridView 中提取日期到 DateTimePicker
【发布时间】:2021-09-18 07:47:26
【问题描述】:

当我在datagridview 中选择一行时,我希望我的数据库中的日期显示在我的应用程序中的DateTimePicker 上。

但我遇到了一个错误,告诉我'String was not recognized as a valid DateTime.'

由于 MySQL 采用 'yyyy-MM-dd' 格式,我将 DateTimePicker 格式更改为相同。

我尝试将DataGridView的值提取为字符串,发现我得到的格式是"dd-MM-yyyy hh:MM:ss t"

我能做些什么来解决它?

private void dtv_dis1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        txt_id.Text = dtv_dis1.SelectedRows[0].Cells[0].Value.ToString();
        txt_amt.Text = dtv_dis1.SelectedRows[0].Cells[6].Value.ToString();
        txt_remarks.Text = dtv_dis1.SelectedRows[0].Cells[5].Value.ToString();
        //Error with date
        String date = dtv_dis1.SelectedRows[0].Cells[1].Value.ToString();
        dtp_date.Value = DateTime.ParseExact(date, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
        //textBox1.Text = dtv_dis1.SelectedRows[0].Cells[1].Value.ToString();

        tot = dtv_dis1.SelectedRows[0].Cells[2].Value.ToString();
        mop = dtv_dis1.SelectedRows[0].Cells[4].Value.ToString();
        cur = dtv_dis1.SelectedRows[0].Cells[3].Value.ToString();
        if (tot == "Domestic")
        {
            rb_dom.Checked = true;
        }
        else
        {
            rb_in.Checked = true;
        }
        if (mop == "Cash")
        {
            rb_csh.Checked = true;
        }
        else if (mop == "Credit Card")
        {
            rb_cc.Checked = true;
        }
        else
        {
            rb_nb.Checked = true;
        }
        if (cur == "Doller")
        {
            rb_doller.Checked = true;
        }
        else if (cur == "Euro")
        {
            rb_euro.Checked = true;
        }
        else
        {
            rb_Rupees.Checked = true;
        }

    }

【问题讨论】:

  • DateTime 值是如何首先进入网格的?如果网格使用像DataTable 这样的数据源,那么我会假设日期值列的类型是DateTime…如果不是…,它应该是。网格中日期列的数据类型是什么?
  • 它的DATE 在网格@JohnG 的日期列中

标签: c# .net visual-studio datagridview datetimepicker


【解决方案1】:

如果网格数据源是DataTable,而“日期”列的类型是DateTime,那么,应该不需要“解析”日期……

只需从数据源中获取“数据”行,然后直接从单元格中获取DateTime 值……类似……

if (dtv_dis1.CurrentRow != null) {
    DataRowView drv = (DataRowView)dtv_dis1.CurrentRow.DataBoundItem;
    dtp_date.Value = (DateTime)drv["Date"];
}

如评论,如果列类型是string 类型……那么我建议您将其更改为DateTime 类型。

【讨论】:

    【解决方案2】:

    建议,将数据库数据加载到 DataTableList 然后使用 BindingSource 分配 DataTable 或 List

    DataBinding 添加到DataTimePicker,将Format 属性设置为Custom,将CustomFormat 设置为yyyy-MM-dd。

    如果使用 List 并希望在 DataGridView 中反映更改,请在表示 T 的类中实现 INotifyPropertyChanged 接口。

    List 和 BindingSource 有一个怪癖来反映对源的更改,因此请订阅 DateTimePicker 的 ValueChanged 事件并设置 DateTime 列值。

    下面的屏幕截图显示了 DataGridView 中显示的日期,其中包含一个自定义 DataGridView 列,其中出生日期与 DateTimePicker 同步。

    一般来说,通常不希望通过单元格访问 DataGridView 中的数据,而是通过上述 BindingSource 中的 DataSource 来访问。

    要通过 BindingSource 获取 DataGridView 中的当前行,首先断言 .Current 属性不为空,如果不为空,则将 Current 设置为类型,例如。 DataRow 用于 DataTable,类类型用于列表。

    下面的 DataSource 是一个 List,使用 Person1 类从上面显示的 DataGridView 中获取当前行信息。

    private void CurrentPersonButton_Click(object sender, EventArgs e)
    {
    
        if (_bindingSource is null || _bindingSource.Current is null) return;
    
        var current = (Person1)_bindingSource.Current;
    
        MessageBox.Show(
            $"{current.FirstName} {current.LastName}\n" + 
            $"{current.BirthDate.Value:yyyy-MM-dd}");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      相关资源
      最近更新 更多