Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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.
Posted
Updated 17-Jan-16 22:40pm
v2
Comments
BillWoodruff 18-Jan-16 5:28am    
In addition to the good advice on debugging here from OriginalGriff and FES SiteCore, why not use:

DateTime dateTime;

if(DateTime.TryParse(sourceString, out dateTime))
{
// valid
}

Also, take a look at DateTime.TryParseExact
else
{
// throw new .... ?
}

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!
 
Share this answer
 
Comments
Member 8010354 19-Jan-16 2:05am    
I tried in multiple ways but still i'm seeing the error. Please help me with any alternative
OriginalGriff 19-Jan-16 4:34am    
We can't - you need to use the debugger to see exactly what is going on.
I can't run your code - only you can do that - and you need to see exactly what is being converted from what and to what in order to work out why it's a problem.

Think about it: I have a lot of pockets. Which pocket is my car key in?
You can't tell me because you can't look in the pockets - heck, you can't even see if I'm wearing the garment that contains the pocket! I can't look in your variables either!
Member 8010354 19-Jan-16 23:40pm    
Yeah i understood your point and i got it. i should be using rowcount instead of i.
C#
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[^]
 
Share this answer
 
Comments
Member 8010354 18-Jan-16 7:49am    
Actually, while debugging i observed that CODE: DateTime Time = Convert.ToDateTime(column[i]);
dataGridView1.Rows[i].Cells[txtColumnName.Text.Replace(" ", "")].Value = Time.ToString("HH:mm:ss");

Initially Time is taking
Date = {1/1/0001 12:00:00 AM} value and later on it's reading the dates in file but the DATE in the file is 5/4/2009 3:45 PM.

After cursor moving to the second line, then the TIME is having 5/4/2009 3:45 PM date.
You need to check that the cell name in the line below (bolded part) is valid.

C#
dataGridView1.Rows[i].Cells[txtColumnName.Text.Replace(" ","")].Value = Time.ToString("HH:mm:ss");
 
Share this answer
 
Comments
Member 8010354 18-Jan-16 5:14am    
Yeah it's a valid one.

Drpdownbox: No: of columns displayed (Have to select DateTime column)
TextBox: Need to enter new column value

DataGridView: It should display as a 2 columns.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900