Jump to content

[Resolvido] Ajuda em DataGriedView


hypz

Recommended Posts

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

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 by hypz
Link to comment
Share on other sites

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 by acao
Link to comment
Share on other sites

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 by hypz
Link to comment
Share on other sites

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

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
×
×
  • 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.