在C#中转换DateTime时出现异常 [英] Exception while converting DateTime in C#

查看:96
本文介绍了在C#中转换DateTime时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我确实需要将DateTime列分为Date和Time列。所以,当我试图转换DateTime时,它给我的错误声明ArgumentOutofRangeException未处理在线

dataGridView1.Rows [i] .Cells [txtColumnName.Text.Replace(, )]。值= Time.ToString(HH:mm:ss);



以下是我的完整代码:



  private   void  btnSplit_Click( object  sender,EventArgs e)
{
DataGridViewTextBoxColumn dgvcdate = new DataGridViewTextBoxColumn();
dgvcdate.Name = cmbColumnCombo.Text;
dgvcdate.HeaderText = cmbColumnCombo.Text;
dataGridView1.Columns.Add(dgvcdate);

DataGridViewTextBoxColumn dgvctime = new DataGridViewTextBoxColumn();
dgvctime.Name = txtColumnName.Text.Replace( );
dgvctime.HeaderText = txtColumnName.Text;
dataGridView1.Columns.Add(dgvctime);

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 )))
{
dataGridView1.Rows.Add();

if (column [i] .ToString()!= < span class =code-string> \ u0014)
{
DateTime Time = Convert.ToDateTime(column [i]);
// DateTime Time =(DateTime)column [i];
dataGridView1.Rows [i] .Cells [txtColumnName.Text.Replace( )]。Value = Time.ToString( HH:mm:ss);
dataGridView1.Rows [i] .Cells [cmbColumnCombo.Text] .Value = Time.ToString( dd /月/年);
// dataGridView1.Rows [RowCount - 1] .Cells [cmbColumn1.Text] .Value + = column [i] .ToString();
}
}
}
}
}





请告诉我如何解决此错误。在此先感谢。

解决方案

首先使用调试器。

在行上放置断点,然后运行程序。当它到达生产线时,执行将停止并等待您告诉它该做什么。

此时您可以查看您在那里的各个部分并准确计算出正在转换的内容什么,或者是无效的数据。

最可能的问题是文本框包含错误的列名,或者DGV列配置为不喜欢您传递它的字符串的数据类型。



但我们无法说明:我们无法运行您的代码,如果可以的话,我们无法获得您执行的完全相同的条件它没有您的数据,表格,也可能是用户!



试一试;看看你能找到什么。当你有实际的数据来帮助你时,它应该是非常明显的!


 dataGridView1.Rows [i] .Cells [txtColumnName.Text.Replace(  )]。Value = Time.ToString(  HH:mm:ss); 





对于行[i],i可能指的是不存在的行。例如,你有4行,我是7.我是column.Length上的循环,所以你可能并不是要将它用于行计数,你的意思是行[RowCount]而是?



正如Griff所说,我们无法运行您的代码或了解您的逻辑,只有您知道这样才能习惯调试自己的代码



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]


您需要检查下面一行中的单元格名称(粗体部分)是否有效。



 dataGridView1.Rows [i] .Cells [ txtColumnName.Text.Replace(  ]。值=时间.ToString(  HH:mm:ss); 


Hi All,

I do have a requirement where i need to divide DateTime column as Date and Time column. So when i'm trying to convert the DateTime, it's giving me error stating "ArgumentOutofRangeException was unhandled" on line
dataGridView1.Rows[i].Cells[txtColumnName.Text.Replace(" ", "")].Value = Time.ToString("HH:mm:ss");

Below is my complete code:

private void btnSplit_Click(object sender, EventArgs e)
{
    DataGridViewTextBoxColumn dgvcdate = new DataGridViewTextBoxColumn();
    dgvcdate.Name = cmbColumnCombo.Text;
    dgvcdate.HeaderText = cmbColumnCombo.Text;
    dataGridView1.Columns.Add(dgvcdate);

    DataGridViewTextBoxColumn dgvctime = new DataGridViewTextBoxColumn();
    dgvctime.Name = txtColumnName.Text.Replace(" ", "");
    dgvctime.HeaderText = txtColumnName.Text;
    dataGridView1.Columns.Add(dgvctime);

    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)))
                    {
                        dataGridView1.Rows.Add();

                        if (column[i].ToString() != "\u0014")
                        {
                            DateTime Time = Convert.ToDateTime(column[i]);
                   //         DateTime Time = (DateTime)column[i]; 
                     dataGridView1.Rows[i].Cells[txtColumnName.Text.Replace(" ", "")].Value = Time.ToString("HH:mm:ss");
                         dataGridView1.Rows[i].Cells[cmbColumnCombo.Text].Value = Time.ToString("dd/MM/yyyy");
                            //dataGridView1.Rows[RowCount - 1].Cells[cmbColumn1.Text].Value += column[i].ToString();
                        }
                    }
                }
            }
        }



Please let me know how to resolve this error. Thanks in advance.

解决方案

Start by using the debugger.
Put a breakpoint on the line, and run your program. When it gets to the line, execution will stop and wait for you to tell it what to do.
At this point you can look at the various parts you have there and work out exactly what is being converted to what, or is invalid data.
The most likely problem is either the textbox contains the wrong column name, or the DGV column is configured to a datatype that doesn't like the string you are passing it.

But we can't tell that: we can't run your code, and if we could, we couldn;t get the exact same conditions you are executing it under without your data, forms, and probably user as well!

Give it a try; see what you can find out. It should be pretty obvious when you have actual data to help you!


dataGridView1.Rows[i].Cells[txtColumnName.Text.Replace(" ", "")].Value = Time.ToString("HH:mm:ss");



For Rows[i], "i" probably refers to a row that doesn't exist. Eg you have 4 rows and i is 7. i is a loop on column.Length so you probably don't mean to use it for the row count as well, did you mean Rows[RowCount] instead?

As Griff said, we can't run your code or know your logic, only you can know that so get used to debugging your own code

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]


You need to check that the cell name in the line below (bolded part) is valid.

dataGridView1.Rows[i].Cells[txtColumnName.Text.Replace(" ","")].Value = Time.ToString("HH:mm:ss");


这篇关于在C#中转换DateTime时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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