m1aw Posted June 27, 2012 Report Share Posted June 27, 2012 Boas Em seguimento a este topico que postei na zona de SQL Server. Como preciso de poder modificar um campo da minha grid, atraves de uma combobox e depois fazer update para o servidor SQL. O meu problema e que eu queria passar a informação toda primeiro para a datatable e so depois adicionar a combobox na coluna já existetente... Ou ainda melhor a combobox apenas aparcer quando eu clico sobre o campo. Alguma ajuda? Link to comment Share on other sites More sharing options...
jlpcalado Posted June 28, 2012 Report Share Posted June 28, 2012 (edited) Adapta o exemplo. Imports System.Data.SqlClient Public Class Form1 Dim dd As DataTable Dim dt As DataTable Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load dt = GetTable("SELECT Candidatura.candidatura_id, Candidatura.processo, Candidatura.programa_id, " & _ "PROGRAMA.programa_desc FROM Candidatura INNER JOIN PROGRAMA ON Candidatura.programa_id = PROGRAMA.programa_id", My.Settings.CnStr) DataGridView1.DataSource = dt 'Cria tabela com a lista dos programas para source da combobox dd = GetTable("select programa_id, programa_desc from PROGRAMA", My.Settings.CnStr) 'criação da ComboBoxColumn Dim col As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn() col.DataSource = dd col.DisplayMember = "programa_desc" col.ValueMember = "programa_id" col.Name = "Programa" col.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing 'pode mudar-se o style col.DisplayIndex = DataGridView1.Columns(2).DisplayIndex col.DataPropertyName = DataGridView1.Columns(2).DataPropertyName ' relaciona com o campo 'programa_id' da dt DataGridView1.Columns.RemoveAt(2) DataGridView1.Columns.Insert(2, col) End Sub Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged ' Tratar aqui (eventual/) a atualização da informação na bd If e.ColumnIndex = 2 Then MsgBox(sender.Item(e.ColumnIndex, e.RowIndex).Value) End Sub Private Function GetTable(ByVal sql As String, ByVal scn As String) As DataTable Dim da As SqlClient.SqlDataAdapter Dim dt As New DataTable Try Using connection As New SqlConnection(scn) connection.Open() da = New SqlClient.SqlDataAdapter(sql, connection) da.Fill(dt) End Using Catch ex As Exception 'MsgBox(ex.Message) End Try Return dt End Function End Class Edited June 28, 2012 by Caça GeSHi Link to comment Share on other sites More sharing options...
m1aw Posted June 28, 2012 Author Report Share Posted June 28, 2012 (edited) Thank you good Sir. Tive a ver, e por o que me parece e mesmo isso que queria. E ainda aprendi uma serie de coisas que não fazia a mínima que existiam (tipo o INNER JOIN). Agora tou a acabar outra parte do programa, assim que implemente isso venho aqui dar o meu Feedback. Edited June 28, 2012 by m1aw 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