Solved

Datagridview  **insert rows

Posted on 2007-12-03
3
1,995 Views
Last Modified: 2013-11-26
I am trying to populate a datagridview with rows obtained from a file.  
The file has rows broken up by a semi-colon.  The information i need populated in the cells of the datagridview are the values after the semicolon.

What my program does is read the lines of the code.  Then i am trying to remove from the input string all the values to the left of the semi-colon and then inputting that value into the datagridview row.  For some reason VB is telling me to create an instance of a "new" object.  I am not seeing why however.
DataGridView1.Rows.Add()    'ROW ADDED
            varcount = DataGridView1.Rows.Count  'MAX ROWS OF DATAGRIDVIEW
            For xvar As Integer = 0 To 5   'LOOP INPUT FILE
                Dim varstr  
                Dim line  'STRING CONTAINING INPUT
                Dim pos As Integer 'POSITION OF SEMI-COLON
 
                line = sr.ReadLine   'READS LINE
                pos = InStr(line, "; ")  'GETS POSITION OF SEMI-COLON
                varstr = line.Remove(0, pos)  '>>>>>ERROR HERE<<<<<
                DataGridView1.Rows(varcount - 1).Cells(xvar).Value = varstr  'INPUT COLUMN CELLS
            Next xvar

Open in new window

0
Comment
Question by:alexander001
[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
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
aperion earned 250 total points
ID: 20401414
I'd try a couple of different things.  If you have to keep the code the way you have it then try removing the space after the semi-colon in this line:

pos = InStr(line, "; ")  'GETS POSITION OF SEMI-COLON

Declaring your varstr variable as an array allows you to use the split function.   If neither one of these will work for you then I'd try stepping through the code using breakpoints and see what the variables values are and which one if any is throwing the error.

DataGridView1.Rows.Add()    'ROW ADDED
        varcount = DataGridView1.Rows.Count  'MAX ROWS OF DATAGRIDVIEW
        For xvar As Integer = 0 To 5   'LOOP INPUT FILE
            Dim varstr() As String
            Dim line As String = ""  'STRING CONTAINING INPUT
            line = sr.ReadLine   'READS LINE
            varstr = line.Split(";")
            DataGridView1.Rows(varcount - 1).Cells(xvar).Value = varstr  'INPUT COLUMN CELLS
        Next xvar

Open in new window

0
 

Author Comment

by:alexander001
ID: 20403145
Still getting an error.  It's a weird one.  When i run it there's an immediate window that states " A first chance exception of type 'System.NullReferenceException' occured"

Plus when i run through the code i get the error "User the "new" keyword to create an object instance"

These messages come in on my original code at line:
           varstr = line.Remove(0, pos)
and when i use the suggested code for the string array at line:
           varstr = line.Split(";")

Any ideas?
0
 
LVL 1

Expert Comment

by:aperion
ID: 20405958
Set a breakpoint at that line and see which variales are nothing.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

735 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