![]() |
严肃的苹果 · LINUX使用ps命令获取对应PID_ps ...· 2 月前 · |
![]() |
怕老婆的皮带 · 林夕写的这首歌遭到陈奕迅的嫌弃,只因歌名太低 ...· 7 月前 · |
![]() |
腹黑的核桃 · “湘医保”上自主办理异地就医备案操作流程_通 ...· 1 年前 · |
![]() |
霸气的书签 · 我在海贼世界画漫画_(极光帷帐)小说最新章节 ...· 1 年前 · |
![]() |
任性的紫菜汤 · 2022款雪佛兰畅巡新增车型上市,车叔为何不 ...· 1 年前 · |
在这里,当我在我的
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;
rb_in.Checked = true;
if (mop == "Cash")
rb_csh.Checked = true;
else if (mop == "Credit Card")
rb_cc.Checked = true;
rb_nb.Checked = true;
if (cur == "Doller")
rb_doller.Checked = true;
else if (cur == "Euro")
rb_euro.Checked = true;
rb_Rupees.Checked = true;
}
如果网格数据源是
DataTable
,而“date”列的类型是
DateTime
,则不需要“解析”日期…
只需从数据源获取" data“行,然后直接从单元格…获取
DateTime
值像…这样的东西
if (dtv_dis1.CurrentRow != null) {
DataRowView drv = (DataRowView)dtv_dis1.CurrentRow.DataBoundItem;
dtp_date.Value = (DateTime)drv["Date"];
}
如上所述,如果列类型是
string
类型…那么我建议您将其更改为一种
DateTime
类型。
建议将数据库数据加载到 DataTable 或 List 中,然后使用BindingSource分配DataTable或List。
将
DataBinding
添加到DataTimePicker,将
Format
属性设置为自定义,将
CustomFormat
设置为yyyy-MM-dd。
如果使用列表并希望反映DataGridView中的更改,请在表示
T
的类中实现
INotifyPropertyChanged
接口。
List和BindingSource有一个怪癖,可以反映对源的更改,因此可以订阅DateTimePicker的
ValueChanged
事件并设置DateTime列值。
下面的屏幕截图显示了带有自定义DataGridView列的DataGridView中显示的日期,其中出生日期与DateTimePicker同步。
通常,人们不希望通过单元格访问DataGridView中的数据,而是通过上述BindingSource中的DataSource来访问。
要通过BindingSource获取DataGridView中的当前行,首先断言.Current属性is not null,如果不为空,则当前类型为e.case。DataTable的DataRow,列表的类类型。
下面的DataSource是一个使用Person1类从上面显示的DataGridView中获取当前行信息的列表。
private void CurrentPersonButton_Click(object sender, EventArgs e)
if (_bindingSource is null || _bindingSource.Current is null) return;