我有一个包含员工信息的数据网格视图, 我想向组合框中的每个员工展示团队
MsgBox工作正常并向他的团队(Equipe)展示员工(合作) 但是我将团队添加到单元格时遇到了问题 它添加了每一行中的所有团队 而clear()并没有解决问题 它可以使用DataGridViewComboBoxCell,但我无法找到遗漏的东西 这是代码:
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
谢谢
答案 0 :(得分:0)
试试这个..
首先将datagridview2“readonly”属性设置为“False”
然后你可以用这种方式添加项目
Dim combocell As New DataGridViewComboBoxCell
combocell = DataGridView2.Rows(i).Cells(“Poles”)
combocell.Items.Add(dr.Item(0))
答案 1 :(得分:0)
用于更新DatagridView ComboBox项(C#代码)的rowEnter事件处理程序的Skeletton:
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) ;
}
自动将上述代码转换为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
答案 2 :(得分:0)
问题是Poles.Items.Add(dr.Item(0))
(CBcolumn)正在运行但是添加了所有行中的所有项目
且cell.Items.Add(dr.Item(0))
无效
答案 3 :(得分:0)
乱画
我想使用同一行中的参考ref(col1 = textbox)将项目添加到组合框(第2列)
所以我不会将同一列从文本框转换为列框
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
答案 4 :(得分:0)
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
结果:
答案 5 :(得分:0)
最后它起作用:D
Graffito和Gennaro都帮我搞定了
这里是代码:
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