Solved

write data to DataGridView

Posted on 2006-11-21
3
2,954 Views
Last Modified: 2008-01-09
Hi,

I am new to DataGridView contol in VB.NET 2005.  Here is the question:  I used a datatable to populate DataGridView first:

Dim dt as datatable
'logic to fill datatable
DataGridView.DataSource = dt

This worked fine.

I then need to populate the same DataGridView with another set of data:

Dim sColumnHeader() As String = {"ColA", "ColB", "ColC", "ColD"}
For i = 0 To DataGridView.Columns.Count - 1
      DataGridView.Columns(i).Dispose()
Next

DataGridView.ColumnCount = sColumnHeader.GetUpperBound(0) + 1
For i = 0 To sColumnHeader.GetUpperBound(0)
     DataGridView.Columns(i).Name = sColumnHeader(i)
Next

For i = 0 To 10
      sColA = A & i
      sColB = B & i
      sColC = C & i
      sColD = D & i
      Dim rows() As Object = {sColA, sColB, sColC, sColD}
      DataGridView.Rows.Add(rows)
Next

I received error message of "ColumnCount property cannot be set on a data-bound DataGridView control".  How do I get a fresh start of a DataGridView after data bounding?

Thanks in advance for your help.

Heartland
0
Comment
Question by:heartland
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 15

Expert Comment

by:Solar_Flare
ID: 17992257
why can you not just databind the table to the datagridview?

datagridview.Datasource = dt

that will rebind the entire datagrid, and if you have autogenerate columns = true then it will also generate the datagridview's column based on dt's columns.
0
 
LVL 6

Accepted Solution

by:
manch earned 100 total points
ID: 17994120

just reset the Data Binded by using Nothing



DataGridView.DataSource = Nothing

Now write ur code

Dim sColumnHeader() As String = {"ColA", "ColB", "ColC", "ColD"}
For i = 0 To DataGridView.Columns.Count - 1
      DataGridView.Columns(i).Dispose()
Next

DataGridView.ColumnCount = sColumnHeader.GetUpperBound(0) + 1
For i = 0 To sColumnHeader.GetUpperBound(0)
     DataGridView.Columns(i).Name = sColumnHeader(i)
Next

For i = 0 To 10
      sColA = A & i
      sColB = B & i
      sColC = C & i
      sColD = D & i
      Dim rows() As Object = {sColA, sColB, sColC, sColD}
      DataGridView.Rows.Add(rows)
Next

It will work fine
Try It
0
 

Author Comment

by:heartland
ID: 17995838
Hi Manch,

Yes, it works fine.  Stupid me.  Well, as a matter of fact, I found it by myself after sending my message.  But, your solution is good.  it was too easy for you. Would you mind give me another help.  Why I get a extra empty row in the last row of DataGridView after loading data into it.  For example, the DataGridView will have 11 rows with 11th row empty if I want to write 10 rows of data into it?  Is there any way not writing this extra row?   I know it is not the original question anymore.

Thanks in advance.

heartland
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question