Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6529103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:35:53+00:00 2026-05-25T09:35:53+00:00

Preamble: Having not worked with WinForms for a long while and also never before

  • 0

Preamble:
Having not worked with WinForms for a long while and also never before in VB I am trying to add a DataGridView to an application written in VB which will display a grid of data from a DataTable.

I followed the documentation here, here and here and in a simple test example I have the code

Public Class Form1

    Private count As Integer

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        count = count + 1

        Dim table As DataTable = BindingSource2.DataSource

        Dim row As DataRow
        row = table.NewRow()
        row("Col1") = "foo" + count.ToString()
        row("Col2") = "bar" + count.ToString()

        table.Rows.Add(row) 'throws System.InvalidOperationException here

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'BindingSource2.DataSource = New DataTable()
        'Dim table As DataTable = BindingSource2.DataSource
        Dim table As New DataTable

        Dim column1 As DataColumn = New DataColumn()
        column1.ColumnName = "Col1"
        column1.Caption = column1.ColumnName
        column1.DataType = System.Type.GetType("System.String")
        table.Columns.Add(column1)

        Dim column2 As DataColumn = New DataColumn()
        column2.ColumnName = "Col2"
        column2.Caption = column2.ColumnName
        column2.DataType = System.Type.GetType("System.String")
        table.Columns.Add(column2)

        'Dim keys(0) As DataColumn
        'keys(0) = column1
        'table.PrimaryKey = keys

        ' first row
        Dim row As DataRow = table.NewRow()
        row("Col1") = "beep"
        row("Col2") = "boop"

        table.Rows.Add(row)

        BindingSource2.DataSource = table
    End Sub
End Class

The code gets through Form1_Load okay however the entries added there are not shown in the DataGridView. Then when Timer1_Tick is called it throws a System.InvalidOperationException exception at the line indicated above. I cannot see what I am doing wrong based on the examples given in the docs.

Question: Can anybody help please, with (a) why does the DataGridView not reflect the added data at the end of Form1_Load and (b) why is adding a row causing an exception?

P.s. I’ve checked the debug and at the table.Rows.Add(row) table contains the correct information as does row.

Edit: The BindingSource was added and connected to the DataGridView using the designer so the code for it is shown instead in From1.Designer.vb which I haven’t shown here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:35:53+00:00Added an answer on May 25, 2026 at 9:35 am

    The solution (at least in my case here) was that the DataGridView.AutoGenerateColumns is not shown in the design panel and is by default set to False. I simply added the line

    DataGridView1.AutoGenerateColumns = True
    

    to my code (in Form1_Load) and it worked perfectly. I found the solution on a forum but now cannot find the link. I’ll add it if I find it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently discovered, to my astonishment (having never really thought about it before), machine-sorting
Preamble: I know, disabling warnings is not a good idea. Anyway, I have a
Sorry for long description, however the questions aren't so easy... My project written without
Preamble As a part of a project I'm working on I am trying to
Preamble I have written GLR-parser with error recovery. When it encounters an error it
I am having trouble displaying data in my table. Preamble: TableView placed on the
PREAMBLE: this question is quite obsolete, it was written when the preferred Android dev
Preamble: I am using ASP.NET 3.5 and AJAXControlToolkit. I am trying to use the
I am trying to add a simple figure into my latex code .I have
Preamble So, this question has already been answered, but as it was my first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.