Reputation: 147
I have a datagridview with informations of employees, I want to show the teams of every employee in a comboboxcell
the MsgBox is working fine and showing the employee(collab) with his Teams(Equipe) but I have a problem in adding the teams to the cell It added all the teams in every row and clear() was not resolving the problem it can be with using DataGridViewComboBoxCell but I can't find what is missing here is the code:
Try
Conn.Open()
Dim i As Integer
DataGridView2.ReadOnly = False
For i = 0 To DataGridView2.Rows.Count - 1
Dim collab As String = DataGridView2.Rows(i).Cells("RefDataGridViewTextBoxColumn").Value
Dim query As String = "Select Label From equipe_collab where ref_collab='" + collab + "'"
Dim cmd As New OleDbCommand(query, Conn)
Dim dr As OleDbDataReader = cmd.ExecuteReader()
Dim cell As DataGridViewComboBoxCell = DataGridView2.Rows(i).Cells("Poles")
Do While dr.Read = True
MsgBox("collab :" + collab + "| Equipe :" + dr.Item(0))
' Poles.Items.Add(dr.Item(0)) '
cell.Items.Add(dr.Item(0))
Loop
Next i
Conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Thank you
Upvotes: 0
Views: 2587
Reputation: 147
Finally it works :D
Both of you Graffito and Gennaro helped me to get it
Here the code:
Private Sub dataGridView2_RowEnter(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView2.RowEnter
Dim collab As String = DataGridView2.Rows(e.RowIndex).Cells("RefDataGridViewTextBoxColumn").Value
Dim cell As DataGridViewComboBoxCell = DataGridView2.Rows(e.RowIndex).Cells("Poles")
Try
Conn.Open()
Dim query As String = "Select Label From equipe_collab where ref_collab ='" + collab + "' "
Dim cmd As New OleDbCommand(query, Conn)
Dim dr As OleDbDataReader = cmd.ExecuteReader()
cell.Items.Clear()
Do While dr.Read = True
cell.Items.AddRange(dr.Item(0))
Loop
cell.Value = cell.Items(0)
Conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Upvotes: 0
Reputation: 73
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
//Insert 3 rows
DataGridView1.Rows.Add()
DataGridView1.Rows.Add()
DataGridView1.Rows.Add()
//set ReadOnly = False
DataGridView1.ReadOnly = False
For i = 0 To DataGridView1.Rows.Count - 1
Dim cell As DataGridViewComboBoxCell = DataGridView1.Rows(i).Cells("Poles")
//insert values 1,2,3 in first row and set selected value of combobox
If i = 0 Then
DataGridView1.Rows(i).Cells(0).Value = "Employ1"
cell.Items.Add("1")
cell.Items.Add("2")
cell.Items.Add("3")
cell.Value = cell.Items(0)
End If
//insert values 4,5,6 in first row and set selected value of combobox
If i = 1 Then
DataGridView1.Rows(i).Cells(0).Value = "Employ2"
cell.Items.Add("4")
cell.Items.Add("5")
cell.Items.Add("6")
cell.Value = cell.Items(0)
End If
//insert values 7,8,9 in first row and set selected value of combobox
If i = 2 Then
DataGridView1.Rows(i).Cells(0).Value = "Employ3"
cell.Items.Add("7")
cell.Items.Add("8")
cell.Items.Add("9")
cell.Value = cell.Items(0)
End If
Next i
End Sub
Result:
Upvotes: 0
Reputation: 147
Graffito
I want to add items to combobox (col 2) using the ref of collab (col1 = textbox) in the same line
so I will not convert the same column from textbox to columnbox
Private Sub DataGridView2_RowEnter(sender As Object, e As DataGridViewCellEventArgs)
' get here the row values to be used to determine the ComboBox content
' Adapt the 2 following lines
Dim col1Value As Integer = CInt(DataGridView2.Rows(e.RowIndex).Cells("col1Name").Value)
Dim collab As String = DirectCast(DataGridView2.Rows(e.RowIndex).Cells("RefDataGridViewTextBoxColumn").Value, String)
Dim PoleValues As List(Of String) = New List(Of [String])()
' Here, your code to add to PoleValues the ComboBox values from colXValues
' ...
Try
Conn.Open()
Dim query As String = "Select Label From equipe_collab where ref_collab='" + collab + "'"
Dim cmd As New OleDbCommand(query, Conn)
Dim dr As OleDbDataReader = cmd.ExecuteReader()
Do While dr.Read = True
PoleValues.Add(dr.Item(0))
Loop
Conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
'
' I want to add items to combobox(col 2) using the ref of collab (col1) in thee same line
' so I will not convert the same column from textbox to columnbox
'
Dim cb As DataGridViewComboBoxColumn = DirectCast(DataGridView2.Columns("RefDataGridViewTextBoxColumn"), DataGridViewComboBoxColumn)
cb.Items.Clear()
cb.Items.AddRange(PoleValues)
End Sub
Upvotes: 0
Reputation: 1718
Skeletton of the rowEnter eventhandler used to update the DatagridView ComboBox items (C# code):
private void DataGridView2_RowEnter(object sender, DataGridViewCellEventArgs e)
{
// get here the row values to be used to determine the ComboBox content
// Adapt the 2 following lines
int col1Value = (int )dataGridView1.Rows[e.RowIndex].Cells["col1Name"].Value ;
string col2Value = (string)dataGridView1.Rows[e.RowIndex].Cells["col2Name"].Value ;
List<string> PoleValues = new List<String>() ;
// Here, your code to add to PoleValues the ComboBox values from colXValues
...
DataGridViewComboBoxColumn cb = (DataGridViewComboBoxColumn)DatagridView2.Columns["RefDataGridViewTextBoxColumn"] ;
cb.Items.clear() ;
cb.Items.AddRange(PoleValues) ;
}
Automatic conversion of above code to vb.net:
Private Sub DataGridView2_RowEnter(sender As Object, e As DataGridViewCellEventArgs)
' get here the row values to be used to determine the ComboBox content
' Adapt the 2 following lines
Dim col1Value As Integer = CInt(dataGridView1.Rows(e.RowIndex).Cells("col1Name").Value)
Dim col2Value As String = DirectCast(dataGridView1.Rows(e.RowIndex).Cells("col2Name").Value, String)
Dim PoleValues As List(Of String) = New List(Of [String])()
' Here, your code to add to PoleValues the ComboBox values from colXValues
' ...
Dim cb As DataGridViewComboBoxColumn = DirectCast(DatagridView2.Columns("RefDataGridViewTextBoxColumn"), DataGridViewComboBoxColumn)
cb.Items.clear()
cb.Items.AddRange(PoleValues)
End Sub
Upvotes: 0
Reputation: 147
the problem is that Poles.Items.Add(dr.Item(0))
(CBcolumn) is working but with adding all the items in all rows
and cell.Items.Add(dr.Item(0))
is not working
Upvotes: 0
Reputation: 73
try this..
First set your datagridview2 "readonly" property to "False"
Then u can add items in this way
Dim combocell As New DataGridViewComboBoxCell
combocell = DataGridView2.Rows(i).Cells("Poles")
combocell.Items.Add(dr.Item(0))
Upvotes: 1