473,573 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with a DataGridView

Hi all,
I'm loading a datagridview from an Excel file using a dataadapter.fil l()
method. A few questions:

1) Is there any way to control the column datatypes? The columns are
defined by the .fill method based on the excel file, and the data is read in
during the .file method, so everything happens with this one command. If I
try to define a column.datatype before calling .fill I get an error because
the column doesn't exist yet, if I try to define the column.datatype after
..fill I get an error because you can't change a columns datatype once data
exists in the datatable.

2) Is there any way to have a progress bar monitor the progress of the .fill
method. Again everything happens with this single command so I don't have
anywhere to place code.

Heres my code:

Dim connString As String = "Driver={Micros oft Excel Driver (*.xls)};DBQ=" &
Me.OpenFileDial og1.FileName
Dim da As New System.Data.Odb c.OdbcDataAdapt er

Dim ds As DataSet = New DataSet

Dim myRecords As Integer

Using cn As New System.Data.Odb c.OdbcConnectio n(connString)

cn.Open()

da.SelectComman d = New System.Data.Odb c.OdbcCommand(" Select * from
[Sheet1$]", cn)

myRecords = da.Fill(ds)

Me.DataGridView CoFile.DataSour ce = ds.Tables(0)

End Using

Thanks,

Ryan
Jan 31 '07 #1
2 3402
Hi Ryan,

As for your first question, you could create columns with the data types
you'd like and add them to the DataTable before you call the Fill method of
the DataAdapter.

If each column you add into the DataTable has the same name as the
corresponding column in the source table does in the Excel file, you could
just pass the DataTable's name as the second parameter in the Fill method.
For example, if the DataTable's name is DataTable1, the statement should
look like blow:

da.Fill(ds,"Dat aTable1")

Note that you should ensure the data in the source table could be converted
to the data type you specify in the DataTable.

In addition, if all the names of the columns you create and add into the
DataTable are not same as those in the source table, you should use
DataColumn mappings to specify the mapping between the columns in the
source table and those in the DataTable.

For more information on DataTableMappin g and DataColumn Mapping, you may
visit the following link.
'Setting Up DataTable and DataColumn Mappings'
http://msdn2.microsoft.com/en-us/ks92fwwh.aspx

As for your second question, since you'd like a progress bar to monitor the
progress of filling the DataTable, you'd better do the work of 'filling' in
a separate thread. I suggest that you use a BackgroundWorke r component to
do it, which is very suitable to execute an operation on a separate thread
and provides a good mechanism to report the progress of the underground
work.

For more information on BackgroundWorke r class and how to use it, you may
visit the following link.
'BackgroundWork er Class'
http://msdn2.microsoft.com/en-us/lib....backgroundwor
ker.aspx

There remains another question,i.e. how to know the progress of filling the
DataTable. I suggest that you use a timer component to do that. Start the
timer just before the filling work begins, and then check the progress of
the work in the Tick event handler of the timer (you may get the total row
count in the source table in advance and check the current row count in the
DataTable, then return the percent). When the underground work finishes,
stop the timer.

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 1 '07 #2
Thanks Linda! This works :)

"Linda Liu [MSFT]" <v-****@online.mic rosoft.comwrote in message
news:K1******** ********@TK2MSF TNGHUB02.phx.gb l...
Hi Ryan,

As for your first question, you could create columns with the data types
you'd like and add them to the DataTable before you call the Fill method
of
the DataAdapter.

If each column you add into the DataTable has the same name as the
corresponding column in the source table does in the Excel file, you could
just pass the DataTable's name as the second parameter in the Fill method.
For example, if the DataTable's name is DataTable1, the statement should
look like blow:

da.Fill(ds,"Dat aTable1")

Note that you should ensure the data in the source table could be
converted
to the data type you specify in the DataTable.

In addition, if all the names of the columns you create and add into the
DataTable are not same as those in the source table, you should use
DataColumn mappings to specify the mapping between the columns in the
source table and those in the DataTable.

For more information on DataTableMappin g and DataColumn Mapping, you may
visit the following link.
'Setting Up DataTable and DataColumn Mappings'
http://msdn2.microsoft.com/en-us/ks92fwwh.aspx

As for your second question, since you'd like a progress bar to monitor
the
progress of filling the DataTable, you'd better do the work of 'filling'
in
a separate thread. I suggest that you use a BackgroundWorke r component to
do it, which is very suitable to execute an operation on a separate thread
and provides a good mechanism to report the progress of the underground
work.

For more information on BackgroundWorke r class and how to use it, you may
visit the following link.
'BackgroundWork er Class'
http://msdn2.microsoft.com/en-us/lib....backgroundwor
ker.aspx

There remains another question,i.e. how to know the progress of filling
the
DataTable. I suggest that you use a timer component to do that. Start the
timer just before the filling work begins, and then check the progress of
the work in the Tick event handler of the timer (you may get the total row
count in the source table in advance and check the current row count in
the
DataTable, then return the percent). When the underground work finishes,
stop the timer.

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no
rights.

Feb 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3151
by: sapkal.manish | last post by:
Question : How can I find actual row position of DataTable in DataGridView when DataTable is sorted / My source code like this Private WithEvent dgvInwDet as new DataGridView Private WithEvent DataTable as New DataTable
1
4158
by: Riley | last post by:
Need some help. I am using datagridview in 2005 with C#. I am calling a web service and filling the datagridview with the data returned. This all works fine. Next step is to take a value from one of the cells in the datagridview and return it back with some other values in an object for another webservice call. The problem is I dont know how...
0
2161
by: MKBender | last post by:
Hello everyone, What I'm currently trying to do is allow a user to add a row to my datagridview. I have AllowUserToAddRows set to true. And the empty now does appear at the bottom of my data. The datagridview has a data source set to a datatable. The code currently allows a user to add the row properly and the data is making it into the...
14
1624
by: giddy | last post by:
ok i have a datagridview in my app and i have a function defined like so: public void FillColumns(ref DataGridView theDGV) when i call it ... like this : FillColumns(ref dataGridView2); i get : Object reference not set to an instance of an object.
9
2673
by: trint | last post by:
How can I retrieve all of the data from a datagridview? Any help is appreciated. Thanks, Trint
1
2492
by: lbelkova | last post by:
Hello, I've created a custom DataGridViewColumn. Everything work well, except for some reason the column doesn't accept some of the chars: "q", "." and "'". Did anybody have a similar problem? Please let me know. Any help will be gratefully appreciated. Thanks. my code:
1
2518
by: slikrik98 | last post by:
Greetings, I believe I have narrowed down my issues to one simple question, and I'm hoping someone with async events experience can help me out. My question is: how is using EventsHelper.FireAsync() different from using EventsHelper.Fire() BUT having the listener's event handler spawn a new thread to do the work? In my program, I have a...
6
8740
by: Simon Harvey | last post by:
Hi all, I'm really hoping someone can help me with this as it's causing me some serious problems. I have a Windows Forms application using the gridview control. When the user selects a row, the SelectionChanged event fires and I load some more data that's particular to that row. The problem I'm having is that the SelectionChanged event...
9
3410
by: Miro | last post by:
My current headache is proper is with the datagridview I am starting to realize that a DataGridView within vs2008 is not as 'robust' as a 'textboxfield' by default for example. Example: A Textbox field can have masking, you can add easy validation and so on. Just adding a dummy datagridview to a form, databounding it, and allowing...
3
5718
by: Andrus | last post by:
I have DataGridView in virtual mode containing 3500 rows. In code below, assigning to RowCount value to 3500 takes 8 seconds. CPU usage goes high at this time. Stepping by F11 into user code shows few celltemplate property getters and combobox/datecombo constructor calls without database access which does not take a lot of time. Debug...
0
7767
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7684
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7768
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5579
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5288
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3729
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2191
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1035
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.