Solved

write data to DataGridView

Posted on 2006-11-21
3
2,856 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hide Tab Page 3 33
Copy/Clone an object. 9 29
Trying to get PDF to open as "Open,Save,Cancel" instead of as a pop-up. 6 31
Exit the loop 4 31
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now