拆分日期时间字段时出错 [英] Error while splitting the datetime fields

查看:72
本文介绍了拆分日期时间字段时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何编程接受日期时间列的空白值到网格中,以下是我遇到错误的代码行:



dataGridView1.Rows [RowCount - 1] .Cells [cmbIdentity.Text] .Value - 现在我得到的索引值超出了范围。





我的总代码是:

Hi,

How to program to accept a blank values for datetime columns into the grid and below is the line of code i'm getting error:

dataGridView1.Rows[RowCount - 1].Cells[cmbIdentity.Text].Value - Now i'm getting Index value is out of range.


My total code is:

for (int RowCount = 1; RowCount <= strfile.Length - 1; RowCount++)
            {
                if (strfile[RowCount].ToString() != "")
                {
                    if (RowCount != 0)
                    {
                        string[] column = strfile[RowCount].Split('þ');
                        for (int i = 1; i < column.Length - 1; i++)
                        {
                            if (cmbColumnCombo.SelectedIndex == ((i - 1) / 2))
                            {
                                if (!string.IsNullOrEmpty(column[i]) && column[i].ToString() != "\u0014")
                                    {
                                        
                                        dataGridView1.Rows.Add();
                                        
                                        DateTime Time = Convert.ToDateTime(column[i].ToString());
                                        dataGridView1.Rows[RowCount - 1].Cells[txtColumnName.Text.Replace(" ", "")].Value = Time.ToString("HH:mm:ss");
                                        dataGridView1.Rows[RowCount - 1].Cells[cmbColumnCombo.Text].Value = Time.ToString("dd/MM/yyyy");
                                        //dataGridView1.Rows[RowCount - 1].Cells[cmbColumn1.Text].Value += column[i].ToString();

                                }
                            }
                        }

                        for (int i = 1; i < column.Length - 1; i++)
                        {
                            if (cmbIdentity.SelectedIndex == ((i - 1) / 2))
                            {
                                if (!string.IsNullOrEmpty(column[i]) && column[i].ToString() != "\u0014")
                                {
                                    dataGridView1.Rows[RowCount - 1].Cells[cmbIdentity.Text].Value += column[i].ToString();
                                }
                            }
                        }
                    }
                }
            }









有谁能帮助我如何实现这个目标?



我是什么尝试过:



是的,我尝试更改代码,但没有用。





Can anyone please help me how to achieve this?

What I have tried:

Yeah i tried changing the code and it didn't worked.

推荐答案

首先,在尝试转换之前检查列是否为空。其次,TryParse方法在无法转换对象时不会抛出异常。最后,您不必分割日期时间对象,您只需要以不同的格式显示它。

例如:

如果您的日期2016-02-15 09:35:55在您的

First, check that the column isn't empty before you try to convert. Second, there is TryParse method that will not throw an exception when the object cannot be converted. Finally, You don't have to split date time object, you just need to show it in different format.
For example:
If you have date 2016-02-15 09:35:55 in your
DateTime t = DateSerial(2016, 2, 15, 9,35,55);





您只能使用



You can show date only with

t.ToString(dd.MM.yyyy");



仅限时间(无秒数):


Time only (without seconds) with:

t.ToString("HH:mm");





有很多简写方法可供选择



And there are plenty of shorthand methods to choose from

t.ToShortDateString(); 



例如将根据客户端计算机上的当前文化格式化您的日期并且只显示日期组件。


for example will format your date by the current culture on the client computer and will show only date component.


这篇关于拆分日期时间字段时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆