vascoc Posted June 14, 2012 at 08:45 AM Report Share #462621 Posted June 14, 2012 at 08:45 AM (edited) A minha datagrid é criada através de um botão, mas gostava que fosse possível adicionar o evento click na datagrid, como ? dg = New DataGrid() dg.Location = New Point(300, 300) dg.ReadOnly = True dg.Refresh() 'ajustar da tabela dg.Dock = DockStyle.None dg.BorderStyle = BorderStyle.FixedSingle dg.Size = New System.Drawing.Size(400, 300) dg.CaptionVisible = False Me.Controls.Add(dg) dg.SetDataBinding(ds, "Consulta") 'dg.Enabled = False dg.Update() Edited June 14, 2012 at 09:06 AM by Caça GeSHi Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 at 09:06 AM Report Share #462624 Posted June 14, 2012 at 09:06 AM Para atribuir o evento utiliza o AddHandler http://msdn.microsoft.com/en-us/library/7taxzxka%28v=vs.80%29.aspx Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
vascoc Posted June 14, 2012 at 09:41 AM Author Report Share #462638 Posted June 14, 2012 at 09:41 AM (edited) Eu tive a ver.... isto assim funciona mas só quando clico no titulo das colunas, não dá para fazer quando clico nas colunas ? AddHandler dg.Click, AddressOf dg_click Public Sub dg_click() MsgBox(" asdasd") End Sub Também vi esse link que me deste, mas não entendi muito bem...e esta maneira parece ser mais fácil. Edited June 14, 2012 at 09:45 AM by vascoc Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 at 09:45 AM Report Share #462641 Posted June 14, 2012 at 09:45 AM Para isso utiliza o evento CellClick Para verificar se o utilizador clicou na coluna, tens de verificar se o valor da linha é igual a -1 Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
vascoc Posted June 14, 2012 at 10:02 AM Author Report Share #462658 Posted June 14, 2012 at 10:02 AM Como é que faço o Cell click ? estava a seguir-me por aqui mas agora "Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types." http://www.gcpowertools.com/help/spreadnet6/WF/FarPoint.Win.Spread~FarPoint.Win.Spread.FpSpread~CellClick_EV.html Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 at 10:09 AM Report Share #462659 Posted June 14, 2012 at 10:09 AM O Sub que vais chamar tem de ter isto como parâmetros sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
vascoc Posted June 14, 2012 at 10:21 AM Author Report Share #462663 Posted June 14, 2012 at 10:21 AM (edited) Algo tipo isto ? dg = New DataGrid() dg.Location = New Point(300, 300) dg.ReadOnly = True dg.Refresh() 'ajustar da tabela dg.Dock = DockStyle.None dg.BorderStyle = BorderStyle.FixedSingle dg.Size = New System.Drawing.Size(400, 300) dg.CaptionVisible = False Me.Controls.Add(dg) dg.SetDataBinding(ds, "Consulta") AddHandler dg.CellClick, AddressOf dg_cellclick 'dg.Enabled = False dg.Update() Private Sub dg_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) MsgBox("asdasd") End Sub Edited June 14, 2012 at 10:35 AM by Caça Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 at 10:34 AM Report Share #462668 Posted June 14, 2012 at 10:34 AM Sim. Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
vascoc Posted June 14, 2012 at 10:49 AM Author Report Share #462673 Posted June 14, 2012 at 10:49 AM (edited) Não está a dar, um colega meu disse-me para optar por arrastar uma datagriview e depois preenche-la, será mais fácil certo ? Já programei e tudo, mas agora não sei é preencher os campos de forma correcta :S Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click Try conn = New SqlCeConnection(My.Settings.HOTEL_DBConnectionString) cmd = New SqlCeCommand("SELECT Clientes.Cod_Clientes, Clientes.Nome_Clientes, Clientes.Morada_Cliente, Clientes.Localidade_Cliente, Clientes.Cod_Postal_Cliente, Clientes.Contato, Clientes.Num_BI, Clientes.Num_Contribuinte, Clientes.Email, Clientes.Nacionalidade, Reserva.Num_Quarto, Reserva.Data_Entrada, Reserva.Data_Saída, Reserva.Preco, Quartos.Tipo_Quarto, Tipo_Quarto.Tipo_Quarto AS Expr1 FROM Clientes INNER JOIN Reserva ON Clientes.Cod_Clientes = Reserva.Cod_Cliente AND Clientes.Cod_Clientes = Reserva.Cod_Cliente INNER JOIN Quartos ON Reserva.Num_Quarto = Quartos.Num_Quarto AND Reserva.Num_Quarto = Quartos.Num_Quarto INNER JOIN Tipo_Quarto ON Quartos.Tipo_Quarto = Tipo_Quarto.ID_Tipo WHERE (Clientes.Nome_Clientes LIKE '%" & txtnome.Text & "%')", conn) da = New SqlCeDataAdapter(cmd) ds = New DataSet() ' Dim dr As SqlCeDataReader = cmd.ExecuteReader() With conn If .State = ConnectionState.Open Then 'ta aberto .Close() ' fechamos End If .Open() ' abrimos a conexao End With da.Fill(ds, "consulta") cb = New SqlCeCommandBuilder(da) Catch ex As Exception MessageBox.Show(ex.Message, "informação do Sistema", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try Dim dr As SqlCeDataReader = cmd.ExecuteReader() datagrid_reservas.Columns.Clear() Dim n As Integer = dr.FieldCount Dim campo As String Dim k As Integer For k = 0 To n - 1 campo = dr.GetName(k) datagrid_reservas.Columns.Add(campo, campo) Next Dim i As Integer = 0 While dr.Read() Dim linha As New DataGridViewRow() datagrid_reservas.Rows.Add(linha) datagrid_reservas.Rows(i).Cells(0).Value = dr.Item(0) i = i + 1 End While datagrid_reservas.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.AllCells conn.Close() End Sub End Class Edited June 14, 2012 at 10:54 AM by Caça Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 at 10:55 AM Report Share #462675 Posted June 14, 2012 at 10:55 AM Mais fácil ou não depende do objectivo. Qual é o problema que estás a ter? Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
vascoc Posted June 14, 2012 at 10:59 AM Author Report Share #462678 Posted June 14, 2012 at 10:59 AM (edited) Mais fácil ou não depende do objectivo. Qual é o problema que estás a ter? Era aquilo do Cell Click, mas assim fica mais apresentável... A questão é que agora preenche mal... http://img812.imageshack.us/img812/2715/ddddnn.png Edited June 14, 2012 at 11:06 AM by vascoc Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 at 11:08 AM Report Share #462687 Posted June 14, 2012 at 11:08 AM Troca isto Dim linha As New DataGridViewRow() datagrid_reservas.Rows.Add(linha) datagrid_reservas.Rows(i).Cells(0).Value = dr.Item(0) por isto datagrid_reservas.Rows.Add(dr.Item(0), dr.Item(1), dr.Item(2), etc.) Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
vascoc Posted June 14, 2012 at 11:21 AM Author Report Share #462701 Posted June 14, 2012 at 11:21 AM Obrigado, já está, existe alguma maneira de ordenar as colunas ? Tipo em primeiro o numero do quarto, etc ? Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 at 11:22 AM Report Share #462703 Posted June 14, 2012 at 11:22 AM Da forma que estás a fazer, basta ordenares no te SELECT Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
vascoc Posted June 14, 2012 at 11:39 AM Author Report Share #462713 Posted June 14, 2012 at 11:39 AM (edited) E não dá para esconder alguns campos ? Tipo, não quero que mostre o código de cliente.. e eu preciso de ir buscar o código de cliente na conexão http://img195.imageshack.us/img195/9567/ddddu.png Edited June 14, 2012 at 11:49 AM by vascoc Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 at 12:18 PM Report Share #462727 Posted June 14, 2012 at 12:18 PM Sim, basta podes fazer isso nas propriedades da coluna. Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
vascoc Posted June 14, 2012 at 05:27 PM Author Report Share #462892 Posted June 14, 2012 at 05:27 PM como ao certo ? Link to comment Share on other sites More sharing options...
Caça Posted June 14, 2012 at 10:12 PM Report Share #462949 Posted June 14, 2012 at 10:12 PM DataGridView1.Columns(0).Visible = False Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
vascoc Posted June 15, 2012 at 11:57 AM Author Report Share #463051 Posted June 15, 2012 at 11:57 AM Está bom, obrigado. Resolvido 😉 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