Question Assign values to DataGridViewComboBox at runtuime through a For loop

Benniit

Member
Joined
Mar 14, 2009
Messages
12
Programming Experience
Beginner
Please I have been able to create a DataGridViewComboBox at runtime with the code below and usernames from the users' table get populated in the DataGridVoewComboBox. Now my problem is how to assign values to the comboboxes through a loop. Retrieving the values through a for loop is not a problem. The problem is how to assign values to DataGridViewComboBoxes as I tried something like Me.DataGridView1.Rows(ctr).Cells("Username").Value = "Already forwarded". But this does not work. Any better way, please? Thanks in advance

VB.NET:
  'Connection established and objects initialized already
  daAdapter.Fill(Table) : daUsers.Fill(TableU)
            If Table.Rows.Count > 0 Then
                frmForwardFilesOrMails.txtQuery.Text = Query
                frmForwardFilesOrMails.DataGridView1.AutoGenerateColumns = False
                frmForwardFilesOrMails.DataGridView1.DataSource = Table
                frmForwardFilesOrMails.lblDays.Text = ""
                frmForwardFilesOrMails.lblDays.Text = Table.Rows.Count & " row(s) affected"
                frmForwardFilesOrMails.Show()
                frmForwardFilesOrMails.WindowState = FormWindowState.Normal
                frmForwardFilesOrMails.BringToFront()
            Else
                MsgBox("No Files/Mails to forward", MsgBoxStyle.Critical)
            End If

            Dim combo As New DataGridViewComboBoxColumn
            If TableU.Rows.Count > 0 Then
                Dim ctr As Integer
                For ctr = 0 To TableU.Rows.Count - 1
                    combo.Items.Add(TableU.Rows(ctr).Item("Username").ToString.Trim)
                Next

                combo.HeaderText = "To"
                combo.DisplayIndex = 4
                combo.Name = "Username"
                frmForwardFilesOrMails.DataGridView1.Columns.Add(combo)
 
            End If
 
Back
Top