Writing a value to a datagrid

SchoolTeacher54 used Ask the Experts™

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
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
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

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

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial