Jump to content

DATAGRIDVIEW


csantos
 Share

Recommended Posts

bom dia

Tenho esta datagridview:

datagrid1.jpg

Não sei como fazer o seguinte. Carregar numa qualquer célula do Cod_Produto, coluna 0, e obter o respetivo Nome, coluna 1. E vice versa, selecionar um qualquer Nome do Produto e obter o respetivo código.

Já consegui carregar os códigos dos produtos e os nomes.

Alguém me pode ajudar?

Link to comment
Share on other sites

http://www.vbforums.com/showthread.php?656274-RESOLVED-Datagridview-Combobox-SelectedIndexChanged

http://www.codeproject.com/Questions/157356/Perform-action-when-changing-selected-item-in-a-co

Através do seguinte código consegues obter algumas propriedades da Combobox.

Dim comboBoxCell As DataGridViewComboBoxCell = DataGridView1(1, 0)

Isto está em C# ... n percebo muito ...

Link to comment
Share on other sites

O 1º link está em VB.NET. Quanto ao 2º link, tens sempre bom remédio. :)

http://www.developerfusion.com/tools/convert/csharp-to-vb/

Converti o código que me interessa mas dá-me erros "+=". O que devo aqui colocar?

' We will handle these events of the DataGridView
  dataGridView1.CellEndEdit += New DataGridViewCellEventHandler(AddressOf dataGridView1_CellEndEdit)
  dataGridView1.EditingControlShowing += New DataGridViewEditingControlShowingEventHandler(AddressOf dataGridView1_EditingControlShowing)
 End Sub
 Private Sub dataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs)
  If cbm IsNot Nothing Then
   ' Here we will remove the subscription for selected index changed
   cbm.SelectedIndexChanged -= New EventHandler(AddressOf cbm_SelectedIndexChanged)
  End If
 End Sub
 Private Sub dataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
  ' Here try to add subscription for selected index changed event
  If TypeOf e.Control Is ComboBox Then
   cbm = DirectCast(e.Control, ComboBox)
   If cbm IsNot Nothing Then
    cbm.SelectedIndexChanged += New EventHandler(AddressOf cbm_SelectedIndexChanged)
   End If
   currentCell = Me.dataGridView1.CurrentCell
  End If
 End Sub
 Private Sub cbm_SelectedIndexChanged(sender As Object, e As EventArgs)
  ' Invoke method if the selection changed event occurs
  BeginInvoke(New MethodInvoker(AddressOf EndEdit))
 End Sub
 Private Sub EndEdit()
  ' Change the content of appropriate cell when selected index changes
  If cbm IsNot Nothing Then
   Dim drv As DataRowView = TryCast(cbm.SelectedItem, DataRowView)
   If drv IsNot Nothing Then
    Me.dataGridView1(currentCell.ColumnIndex + 1, currentCell.RowIndex).Value = drv(2).ToString()
    Me.dataGridView1.EndEdit()
   End If
  End If
 End Sub
End Class
End Namespace
Edited by thoga31
GeSHi
Link to comment
Share on other sites

Isto:

dataGridView1.EditingControlShowing += New DataGridViewEditingControlShowingEventHandler(AddressOf dataGridView1_EditingControlShowing)

Passa a ser isto:

AddHandler DataGridView1.EditingControlShowing, AddressOf editingControl
Private Sub editingControl(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
' Código
End Sub
Link to comment
Share on other sites

Isto:

dataGridView1.EditingControlShowing += New DataGridViewEditingControlShowingEventHandler(AddressOf dataGridView1_EditingControlShowing)

Passa a ser isto:

AddHandler DataGridView1.EditingControlShowing, AddressOf editingControl
Private Sub editingControl(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
' Código
End Sub

Tendo em conta o problema inicial que coloquei não estou mesmo a conseguir fazer isto 😕 Será que podes concretizar? Obrigado

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.