Results 1 to 5 of 5

Thread: Add ID# To Each New Entry in DataGridView

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    279

    Add ID# To Each New Entry in DataGridView

    I have a datagridview and when I add a new member in this case, I would like to assign an incremental number to each assigned cell for the member ID# number per new member entry.

    so if cell number one is ID# I want the number to be added here but how do I go about this?
    If I add one member it would obviously be assigned number 1 for ID#
    Member two would be ID# 2 and so on for each member added..

    where I have "ID# Here" I do not know what I need to do:

    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            If TextBox1.Text = "" Then
                MsgBox("Please Enter First Name..")
                Exit Sub
            Else
                If TextBox2.Text = "" Then
                    MsgBox("Please Enter Last Name..")
                    Exit Sub
                Else
                    If TextBox3.Text = ("") Then
                        MsgBox("Please Enter Email Address..")
                        Exit Sub
                    Else
                        If TextBox4.Text = ("") Then
                            MsgBox("Please Enter Phone Number..")
                            Exit Sub
                        Else
                            If TextBox5.Text = ("") Then
                                MsgBox("Please Enter Street Address..")
                                Exit Sub
                            Else
                                If TextBox6.Text = ("") Then
                                    MsgBox("Please Enter City..")
                                    Exit Sub
                                Else
                                    If TextBox7.Text = ("") Then
                                        MsgBox("Please Enter State..")
                                        Exit Sub
                                    Else
                                        If TextBox8.Text = ("") Then
                                            MsgBox("Please Enter Zip Code..")
                                            Exit Sub
                                        Else
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
                End If
    
            Form1.DataGridView1.Rows.Add("ID# Here",
                                            TextBox1.Text,
                                            TextBox2.Text,
                                            TextBox3.Text,
                                            TextBox4.Text,
                                            TextBox5.Text,
                                            TextBox6.Text,
                                            TextBox7.Text,
                                            TextBox8.Text,
                                            DateTime.Now,
                                            "Pending",
                                            "Pending",
                                            "Pending",
                                            "Pending",
                                            "Sales",
                                            "Total",
                                            "Inviter",
                                            "Status")
    
            'Clear All Textboxes After Added
            TextBox1.Clear()
            TextBox2.Clear()
            TextBox3.Clear()
            TextBox4.Clear()
            TextBox5.Clear()
            TextBox6.Clear()
            TextBox6.Clear()
            TextBox7.Clear()
            TextBox8.Clear()
    
            Form1.Label1.Text = Form1.DataGridView1.RowCount
        End Sub

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,484

    Re: Add ID# To Each New Entry in DataGridView

    Code Review: There is absolutely no need for that nested If statement mess. As as soon as you call End Sub or Return, the code after it will not be executed. Not only that, but you can create a dictionary that represents the control's name as the key and the control itself as the value and get all invalid fields. Take a look at using this over your current nested If/Then chuck:
    Code:
    Dim requiredFields = New Dictionary(Of String, TextBox)() From {
        { "First Name", TextBox1 },
        { "Last Name", TextBox2 },
        { "Email Address", TextBox3 },
        { "Phone Number", TextBox4 },
        { "Street Address", TextBox5 },
        { "City", TextBox6 },
        { "State", TextBox7 },
        { "Zip Code", TextBox8 }
    }
    Dim invalidFields = From field In requiredFields Where String.IsNullOrWhitespace(field.Value) Select field.Key
    If (invalidFields.Any()) Then
        MessageBox.Show($"Please enter the following fields: {String.Join(", ", invalidFields)}", "Invalid Form", MessageBoxButton.Ok, MessageBoxIcon.Error)
        Return
    End If
    As far as you actual issue, what I would do is create a DataTable to store your data with a DataColumn where the AutoIncrement property is true. You would bind the DataTable to the DataGridView (usually via a BindingSource), then when you would need to add a record you would add it to the DataTable and refresh your data binding.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,607

    Re: Add ID# To Each New Entry in DataGridView

    Autoincrement probably solves a couple problems, as long as you want to solve them the way that autoincrement does. The big issue is what do you want to do if a record is deleted. Autoincrement will ignore it. For example, if you have IDs 1, 2, 3, and 4, then delete 2, the next ID would be 5. If you want to fill in gaps, then autoincrement won't do that, but it's usually better that way.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    279

    Re: Add ID# To Each New Entry in DataGridView

    Thanks, dday9, much neater than what I had been doing.
    ok I am trying it but I am getting 2 errors with your code:

    1: Value of type 'TextBox' cannot be converted to 'String'.
    2: 'MessageBoxButton' is not declared.

    Yes Shaggy I thought about that but if I deleted a user would I really want or need to fill in the ID# that was deleted?
    Not sure if I have any important reason for that so it should be fine I think..

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,484

    Re: Add ID# To Each New Entry in DataGridView

    Those are issues due to the fact that I free-typed the code.

    For number 1, change field.Value to field.Value.Text

    For number 2, change MessageBoxButton to MessageBoxButtons
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width