Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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.
Posted
Updated 15-Feb-16 0:05am
v3
Comments
Jochen Arndt 15-Feb-16 3:19am    
Check if the string is empty and assign a defined value (NULL or a fixed date time value) if so. Then check for this defined value upon further processing (e.g. creating an empty string when converting back to string).
Member 8010354 15-Feb-16 4:24am    
Can you help how to program to accept the blank values....
Jochen Arndt 15-Feb-16 4:36am    
First you have to decide how to handle those empty fields. This is not related to any code or even the programming language. Once that is stated, you can implement the code.

Checking for an empty field can be done as already suggested by using DateTime.TryParse or String.IsNullOrEmpty.
Member 8010354 15-Feb-16 6:20am    
Can you please help me now as i have used the IsNull
Empty but the error is different
Jochen Arndt 15-Feb-16 6:34am    
What kind of error?
In which line?

Note also that ToDateTime fails when the string can't be converted (invalid format).

Not related to your problem but unneccessary:
column[i].ToString()

column[] is an array of strings. So accessing an element already return a string.

1 solution

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
C#
DateTime t = DateSerial(2016, 2, 15, 9,35,55);


You can show date only with
C#
t.ToString(dd.MM.yyyy");

Time only (without seconds) with:
C#
t.ToString("HH:mm");


And there are plenty of shorthand methods to choose from
C#
t.ToShortDateString(); 

for example will format your date by the current culture on the client computer and will show only date component.
 
Share this answer
 
Comments
Member 8010354 15-Feb-16 4:04am    
Actually there is no fixed value to add. If there is any blank value, it should just show the blank value.
Member 8010354 15-Feb-16 4:40am    
Yeah you are right! But it's giving the error.

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