当前位置: 首页 > 编程语言 > C#

C#DataGridView列的日期时间格式分享

时间:2023-04-10 11:43:17 C#

C#DataGridView列的日期时间格式想要做的是在加载datagridview时,我想将此格式更改为其他格式,比如dd-MM-yy表示日期和时间hh-mm-ss。我想知道是否有人可以指导我如何做到这一点。我不能用上面的方法通过gridview.columns[x].defaultcellstyle.format="dd-MM-yy"我没有收到任何错误但gridview没有任何变化......谢谢注意:我不'thaveoptiontochangeColumnlengthsinthedatabase..:-(没有语法问题Microsoft建议您拦截CellFormatting事件(其中DATED是您要重新格式化的列):privatevoiddataGridView1_CellFormatting(objectsender,DataGridViewCellFormattingEventArgse){//如果列是DATED列,检查//值。if(this.dataGridView1.Columns[e.ColumnIndex].Name=="DATED"){ShortFormDateFormat(e);}}privatestaticvoidShortFormDateFormat(DataGridViewCellFormattingEventArgsformatting){if(formatting.Value!=null){try{DateTimetheDate=DateTime.Parse(formatting.Value.ToString());StringdateString=theDate.ToString("dd-MM-yy");formatting.Value=dateString;formatting.FormattingApplied=true;}catch(FormatException){//如果有其他处理程序有兴趣尝试格式化此DataGridViewCellFormattingEventArgs,则设置为falseinstance.formatting.FormattingApplied=false;DataGridView格式中的语法与DateTime略有不同,但您可以获得相同的结果在我的示例中,我有一个时间列,默认显示HH:mm:ss,我只想显示小时和分钟:以上就是C#学习教程的全部内容:C#DataGridView列的日期时间格式。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注——yourDataGridView.Columns["Time"].DefaultCellStyle.Format=@"hh:mm";本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: