Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

write data to DataGridView

Posted on 2006-11-21
3
Medium Priority
?
3,012 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 400 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

610 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