Solved

Writing a value to a datagrid

Posted on 2016-08-04
1
31 Views
Last Modified: 2016-08-04
Hello,

I have an application that allows user to add a new record in a datagrid view for a list of users; small table.

I check to see if the user 'forgot' to add certain fields required by my program. If they forgot the field I write a default value for them. The situation is this:

1. I ADD a new record
2. The user is entered just fine while leaving out these two fields that I'm checking for.
3. This code errors out telling me that:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll. Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

I sure hope that you can help me!


Dim x
        x = Log_In_CredentialsDataGridView.SelectedRows(0).Cells(5).Value.ToString
        If x = "" Then Log_In_CredentialsDataGridView.SelectedRows(0).Cells(5).Value = 5

        Dim y
        y = Log_In_CredentialsDataGridView.SelectedRows(0).Cells(6).Value.ToString
        If y = "" Then Log_In_CredentialsDataGridView.SelectedRows(0).Cells(6).Value = 0
0
Comment
Question by:SchoolTeacher54
1 Comment
 
LVL 80

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
ID: 41743438
if you try and convert a NULL to a string you will get problems, test for it first

if Log_In_CredentialsDataGridView.SelectedRows(0).Cells(5).Value is Nothing Then
Log_In_CredentialsDataGridView.SelectedRows(0).Cells(5).Value = 5
Endif

if Log_In_CredentialsDataGridView.SelectedRows(0).Cells(6).Value is Nothing Then
Then Log_In_CredentialsDataGridView.SelectedRows(0).Cells(6).Value = 0
Endif

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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