Solved

write data to DataGridView

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

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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

13 Experts available now in Live!

Get 1:1 Help Now