djbarbas Posted June 20, 2012 Report Share Posted June 20, 2012 Boa tarde, Precisei de criar um objecto em runtime, neste caso uma datagridview. Após criar o objecto tive a necessidade de aceder aos eventos do mesmo, e ai é que estou a ter problemas. Segue um pequeno exemplo abaixo para exemplificar. PS: Considera-se que a grid tem registos, apenas não meti o código aqui. A questão é, consigo executar o evento clik que dispara a msgbox, mas não consigo ir buscar o indice da row clicada. Estou a tentar preceber o Addhandler e de que modo posso passar não so o indice mas outro tipo de informação para um evento criado em runtime. Dim WithEvents grid As New DataGridView Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' DEFINO OS ATRIBUTOS DA DATAGRIDVIEW AQUI With grid .Columns.Add("cod", "Código") .Columns.Add("nome", "Nome do Cliente") .Columns.Item("cod").Width = panelgrid.Width * 0.27 .Columns.Item("nome").Width = panelgrid.Width * 0.6 .ReadOnly = True End With ' CRIO A DATAGRIDVIEW me.controls.add(grid) End Sub Private Sub grid_click(ByVal sender As Object, ByVal e As EventArgs) Handles grid.Click MsgBox("Correu Evento") ' FUNCIONA End Sub Obrigado a todos. Link to comment Share on other sites More sharing options...
ribeiro55 Posted June 20, 2012 Report Share Posted June 20, 2012 Não uses o evento Click da grid. Usa antes o CellClick. A assinatura é sender As Object, e As DataGridViewCellEventArg Tens tudo o que precisas na variável "e". Com o Click, seria disparado até se clicasses numa área fora da grelha, por exemplo (e à partida) 1 Report Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips" Link to comment Share on other sites More sharing options...
djbarbas Posted June 20, 2012 Author Report Share Posted June 20, 2012 Funcionou bem! Obrigado Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now