hypz Posted June 24, 2012 at 02:56 PM Report Share #465216 Posted June 24, 2012 at 02:56 PM Como faço para ver se uma variavel é maior que o ultimo registo duma DataGriendView ? Exemplo: If x < (Ultimo registo datagridview) then msgbox("Novo") end if É que estou a ter bastantes problemas a encontrar um artigo onde explique bem e não estou a conseguir chamar o ultimo registo. Link to comment Share on other sites More sharing options...
jlpcalado Posted June 24, 2012 at 06:30 PM Report Share #465246 Posted June 24, 2012 at 06:30 PM If DataGridView1.AllowUserToAddRows Then k = 2 Else k = 1 End If MsgBox(DataGridView1.Rows(DataGridView1.Rows.Count - k).Cells(i).Value) ' ( i=indice da coluna ou "nomecoluna") Link to comment Share on other sites More sharing options...
hypz Posted June 25, 2012 at 02:09 PM Author Report Share #465360 Posted June 25, 2012 at 02:09 PM Boas, eu quero fazer tipo isto. Dim cod_enc As String Dim sql2 As String = "Select * from Encomendas where Cod_Encomenda = " & cod_enc 'cod_enc = DataGridView1.Rows(i).Cells(0).Value If cod_enc > (DataGridView1.Rows(i).Cells(i).Value) Then Label1.Visible = True Label1.Text = "Nova Encomenda" End If Só ñ sei é chamar o ultimo campo da datagridview para que possa fazer o If e ver se é nova encomenda ou não. Link to comment Share on other sites More sharing options...
Caça Posted June 25, 2012 at 02:14 PM Report Share #465363 Posted June 25, 2012 at 02:14 PM If DataGridView1.AllowUserToAddRows Then k = 2 Else k = 1 End If MsgBox(DataGridView1.Rows(DataGridView1.Rows.Count - k).Cells(i).Value) ' ( i=indice da coluna ou "nomecoluna") Serve para o que precisas. Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
hypz Posted June 25, 2012 at 04:06 PM Author Report Share #465405 Posted June 25, 2012 at 04:06 PM (edited) Sim, isso realmente dá certo, dá-me sempre a label a dizer que tenho uma nova encomenda, mas se deixar essa "MsgBox(DataGridView1.Rows(DataGridView1.Rows.Count - k).Cells(i).Value)" dá-me erro "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" E eu não percebo muito de vb e o DataGridView para mim ainda é mais dificil.. Edited June 25, 2012 at 04:14 PM by hypz Link to comment Share on other sites More sharing options...
jlpcalado Posted June 25, 2012 at 05:35 PM Report Share #465429 Posted June 25, 2012 at 05:35 PM Explica melhor o que pretendes. Link to comment Share on other sites More sharing options...
acao Posted June 25, 2012 at 10:03 PM Report Share #465507 Posted June 25, 2012 at 10:03 PM (edited) Boas, eu quero fazer tipo isto. Dim cod_enc As String Dim sql2 As String = "Select * from Encomendas where Cod_Encomenda = " & cod_enc 'cod_enc = DataGridView1.Rows(i).Cells(0).Value If cod_enc > (DataGridView1.Rows(i).Cells(i).Value) Then Label1.Visible = True Label1.Text = "Nova Encomenda" End If Só ñ sei é chamar o ultimo campo da datagridview para que possa fazer o If e ver se é nova encomenda ou não. boas tente assim: Dim cod_enc As String vultimaLinhaDgv = DataGridView1.RowCount -1 If cod_enc > (DataGridView1.Rows(vultimaLinhaDgv).Cells(i).Value) Then Label1.Visible = True Label1.Text = "Nova Encomenda" end if não testei mas penso que será assim, suponho que ao adicionar um registo no dgv o seu indice será o maior, caso contrario será o menor então é só alterar o indice de rows para 0. cumps acao Edited June 25, 2012 at 10:08 PM by acao Link to comment Share on other sites More sharing options...
hypz Posted June 26, 2012 at 10:21 AM Author Report Share #465573 Posted June 26, 2012 at 10:21 AM (edited) Explica melhor o que pretendes. Eu queria fazer do tipo, eu tenho uma lista de encomendas. E queria fazer uma query do tipo, eu tenho 5 encomendas, e uma encomenda é feita, eu quando for lá vou ter 6, e quero que essa encomenda nº 6 apareça uma label a dizer que é nova. Não sei se me fiz entender.. Eu mal percebo de vb, estou agora a entrar mais na programação séria, e datagridview a unica coisa que aprendi foi a meter lá registos, eliminar, procurar e etc. Acao podias explicar-me melhor esse código sf para entender melhor o que estou a fazer ? É que continua a dar-me o mesmo erro 🙂 "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" Se eu usar este código 'Dim cod_enc As Boolean 'Dim sql2 As String = "Select * from Encomendas where Cod_Encomenda = " & cod_enc 'If cod_enc > DataGridView1.AllowUserToAddRows Then ' k = 2 ' Label1.Visible = True 'ElseIf DataGridView1.AllowUserToAddRows = False Then ' k = 1 ' Label1.Visible = False 'End If 'MsgBox(DataGridView1.Rows(DataGridView1.Rows.Count - k).Cells(i).Value) Ele dá-me sempre Encomenda nova mesmo que eu não tenha inserido nenhuma.. Edited June 26, 2012 at 10:26 AM by hypz Link to comment Share on other sites More sharing options...
jlpcalado Posted June 26, 2012 at 12:03 PM Report Share #465583 Posted June 26, 2012 at 12:03 PM Vamos por partes. 1º - se podes adicionar direta/ registos na dadatagrid (AllowUserToAddRows) o último registo da grid aparece com todos os campos a 'NULL' nesta situação para obteres o Cod_encomenda do último registo da dgview podes fazer: DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells("Cod_encomenda")).Value senão - DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells("Cod_encomenda")).Value 2º - Se a dgview permitir ser reordenada por colunas poderás não ter na última linha a última encomenda (?!) e aquela comparação não te vai servir de nada. 3º - Uma abordagem preferível é criares um campo na tabela, por ex, [Data Tratamento], em que quando a encomenda é vista ou tratada inseres no campo a data atual A encomenda será nova se esse campo for Null. Uma hipotese é; Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter if DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells("Data Tratamento").Value Is System.DBNull.Value then Label1.Visible = True Label1.Text = "Nova Encomenda" else Label1.Visible = False end if End Sub Link to comment Share on other sites More sharing options...
hypz Posted June 26, 2012 at 04:42 PM Author Report Share #465722 Posted June 26, 2012 at 04:42 PM então para funcionar melhor tenho que criar um campo Data para ver quando a encomenda é vista ? Link to comment Share on other sites More sharing options...
jlpcalado Posted June 27, 2012 at 12:08 AM Report Share #465808 Posted June 27, 2012 at 12:08 AM Exato. Neste caso quando clicares em qualquer célula da datagrid a Label1 será visível se a encomenda ainda não tiver sido vista. Link to comment Share on other sites More sharing options...
hypz Posted June 27, 2012 at 09:16 AM Author Report Share #465844 Posted June 27, 2012 at 09:16 AM Obrigado pela ajuda jpcalado 😉 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