RIAS
asked on
Dates in the excel column
Hello,
Excel Sheet import changes the format of the column.
I am importing a excel sheet in to grid , the format of the date field is set automatically to Datetime.
Please find the code below.
The requirement is that it treats the column as a text column.
Example:
Dates in the column are 18/06 - 23/06 . When I import the excel sheet , the cell is blank.
Excel Sheet import changes the format of the column.
I am importing a excel sheet in to grid , the format of the date field is set automatically to Datetime.
Please find the code below.
The requirement is that it treats the column as a text column.
Example:
Dates in the column are 18/06 - 23/06 . When I import the excel sheet , the cell is blank.
Private Sub PopulateExcelInDatagrid(ByVal Filename As String)
Dim oledbCon As OleDb.OleDbConnection
Dim dt As New DataTable
Try
Dim csExcel As String = ("Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" & Filename & " '; " & "Extended Properties=Excel 8.0;")
oledbCon = New OleDbConnection(csExcel)
oledbCon.Open()
dt = oledbCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
oledbCon.Close()
If dt.Rows.Count <= 0 Then Exit Sub
Dim excelSheets As [String]() = New [String](dt.Rows.Count - 1) {}
Dim row As DataRow = dt.Rows(0)
oledbCon.Open()
Dim oledbAdapter As New OleDbDataAdapter("select * from [" + row("TABLE_NAME").ToString() + "]", oledbCon)
oledbCon.Close()
Dim excelDataSet As New DataSet()
oledbAdapter.Fill(excelDataSet, "Test")
Dim excelTable As DataTable = excelDataSet.Tables("Test")
Dgv.DataSource = excelTable
With Dgv
.MasterTemplate.BestFitColumns()
.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
If IsNothing(.DataSource) = False Then
For i = 0 To .Columns.Count - 1
.Columns(i).BestFit()
Next
End If
If IsNothing(.DataSource) = False Then
For i = 0 To .Columns.Count - 1
.Columns(i).HeaderText.Trim
Next
.Columns(0).AutoSizeMode = BestFitColumnMode.DisplayedDataCells
End If
End With
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
I am pretty sure you can prefix the column data with ' and it will be treated as a general string and not auto convert
ASKER
I cannot edit the excel sheet.
Thanks
Thanks
ASKER
Yes, i have dates in there like 18/06 - 23/06
Meant : 18/06/2018 - 23/06/2018
Meant : 18/06/2018 - 23/06/2018
ASKER
When i import the excel usin the above code. The Datatable cell is blank.
Its reating that column as DateTime and does not understand the value '18/06 - 23/06' and just leaves it blank.
If I treat this column as text column , then I might be able to retrieve the value.
Cheers
Its reating that column as DateTime and does not understand the value '18/06 - 23/06' and just leaves it blank.
If I treat this column as text column , then I might be able to retrieve the value.
Cheers
If the value(s) in the cell are in this format 18/06 - 23/06 they are not dates, no matter how the cell is formatted.
Are you getting these values be combining dates from other cells?
Are you getting these values be combining dates from other cells?
ASKER
Nope.Thats how the excel column has it.
ASKER
I really don't understand how this column is treated as Date column when
Dim excelTable As DataTable = excelDataSet.Tables("Test" )
exceltable has the column as Datetime.
Dim excelTable As DataTable = excelDataSet.Tables("Test"
exceltable has the column as Datetime.
the suggestion deals with altering/modifying or casting the data on import. You do not need to alter the source to change what it is following import.
ASKER
How to altering/modifying or casting the data on import?
ASKER
Can you suggest any code?
Within the above you have full access to the data, you currently assign what you read in to
I think lines 17,18 is where you have the entire data set, you currently blindly send it into your grid.
You have to iterate, row by row, then modifying the column where the dates are.....
I think lines 17,18 is where you have the entire data set, you currently blindly send it into your grid.
You have to iterate, row by row, then modifying the column where the dates are.....
ASKER
Thanks Arnold.
The problem here is that even in the dataset, if the date is in format 14/5-12/6 it does not import, so the cell is blank.
If it is blank then how can I format it.
Please refer to my code above .
The problem here is that even in the dataset, if the date is in format 14/5-12/6 it does not import, so the cell is blank.
If it is blank then how can I format it.
Please refer to my code above .
Is it possible to upload / attach a sample Excel file so we can have a look?
Have you tried to create data grid view columns manually before importing and set the column type to text?
The idea is before you import.
Am I reading it right that the data pulled is on line 14.
Instead of assigning all into a "variable", you can iterate through row by row making the adjustments to the dates.
The other things are lines 16-19. the other option is to make the corrections within the select line 14.
select column1, column2, (work on column3 to correctly reflect a date range, or split the single column into two)
Am I reading it right that the data pulled is on line 14.
Instead of assigning all into a "variable", you can iterate through row by row making the adjustments to the dates.
The other things are lines 16-19. the other option is to make the corrections within the select line 14.
select column1, column2, (work on column3 to correctly reflect a date range, or split the single column into two)
ASKER
Fernando Soto,
If you create a excel sheet with Date Column having data 18/06 - 23/06 and use my code , it will be a test.
Thanks
If you create a excel sheet with Date Column having data 18/06 - 23/06 and use my code , it will be a test.
Thanks
What control is the gridview? the standard windows forms DataGridView does not have the .MasterTemplate property/member
ASKER
Its telerik Radgridview.
Thanks
Thanks
Have you tried their support system, maybe its a bug with the control. I can't understand why an obvious none date value would treat the build of the grid as a date. does your source data have ANY rows with a real date in? maybe the telerik grid is pre-prosessing the data and is determining that the column is a date column?
I am building a test app and downloading a free trial of the winforms controls to test
I am building a test app and downloading a free trial of the winforms controls to test
My test shows the code works perfectly
I think you need to provide a sample of the excel file you are using
I have used a xls with 20 columns, and I placed the string values you have mentioned . I have a second column named it Date and that was fine, and I also placed a few real dates and that was also fine.
I think to further understand the issue you need to build a little test application and sample data
I think you need to provide a sample of the excel file you are using
I have used a xls with 20 columns, and I placed the string values you have mentioned . I have a second column named it Date and that was fine, and I also placed a few real dates and that was also fine.
I think to further understand the issue you need to build a little test application and sample data
ASKER
Thanks. Will use normal gridview in that case and check
I suspect its your excel data, can you provide a sample which shows the problem?
ASKER
Sure, give me couple of hours will brb
Why don't you utilize query statement to parse date range to string and then fill datatable, i hope it will work in that case.
e.g.,
This way you don't need to switch to some other grid control.
e.g.,
"select col1, col2, CONVERT_DATE_COLUMN_HERE_TO_STRING, remainingCol1, remainingCol2 from [" + row("TABLE_NAME").ToString() + "]"
This way you don't need to switch to some other grid control.
ASKER
Shahan Thanks!
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Is it values like 18/06 - 23/06?
Or like 18/06 and 23/06?
Neither of these represent true date values, the first example essentially being a string and the second examples being, possibly, dates without the year (or month).