Chamuanza Posted March 7, 2009 at 09:06 PM Report Share #249016 Posted March 7, 2009 at 09:06 PM Um bem aja a todos Veremos se consigo expor a minha dificuldade Estou constriundo um pequeno programa de despesas/receitas/balanço caseiro. Faço a inserção de alguns registos (despesa/Receita) na tabela, que vão actualizar a coluna balanço. Como pode vir a ser necessário eliminar um dos registos intermédios, introduzizos erradamente, passo a reportar o problema que me está a suceder. Depois de apagar um dos registos intermédios na datagridview, quando me desloco na gird o ponteiro faz o salto de linha quando não encontra o registo apagado. Penso que tem a ver com a indexação do campo Id com chave primaria, mas não sei como resolver a situação. Junto o codigo Imports System.Data Imports System.Data.OleDb Public Class frmMain Dim con As OleDbConnection Dim dAdapt As OleDbDataAdapter Dim dSet As DataSet Dim a As Integer = 0 Dim dBind As New BindingSource Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click GroupBox1.Enabled = True 'MsgBox(con.ConnectionString) Call Md_Settings() btnConnect.Enabled = False Call Md_FillEvents() Call Md_FillCombo() ' Selecciona o ultimo item da combobox comboBoxNo.SelectedIndex = comboBoxNo.Items.Count - 1 End Sub Private Sub Md_Settings() Try con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _ System.Environment.CurrentDirectory.ToString() & "\DataBase.mdb") con.Open() dAdapt = New Data.OleDb.OleDbDataAdapter("select * from Details order by Id asc ", con) dSet = New DataSet dAdapt.Fill(dSet) dBind.DataSource = dSet dBind.DataMember = dSet.Tables(0).ToString() dataGridView.DataSource = dBind ''Move o cursor para a ultima linha da grid 'Me.dBind.MoveLast() Catch ex As Exception MsgBox(ex.ToString()) End Try End Sub Private Sub Md_FillEvents() Try txtBoxNo.Text = dSet.Tables(0).Rows(a).Item(0) txtBoxData.Text = dSet.Tables(0).Rows(a).Item(1) txtBoxDesignacao.Text = dSet.Tables(0).Rows(a).Item(2) 'txtBoxDespesa.Text = dSet.Tables(0).Rows(a).Item(3) txtBoxDespesa.Text = Format(Convert.ToDouble(dSet.Tables(0).Rows(a).Item(3)), "##,##0.00") 'txtBoxReceita.Text = dSet.Tables(0).Rows(a).Item(4) txtBoxReceita.Text = Format(Convert.ToDouble(dSet.Tables(0).Rows(a).Item(4)), "##,##0.00") 'txtBoxBalanco.Text = dSet.Tables(0).Rows(a).Item(5) txtBoxBalanco.Text = Format(Convert.ToDouble(dSet.Tables(0).Rows(a).Item(5)), "##,##0.00") 'Call SomarColuna() Call DarCorUltimaCelula() Catch ex As Exception MsgBox(ex.ToString()) End Try End Sub Private Sub Md_FillCombo() Try Dim r As DataRow comboBoxNo.Items.Clear() For Each r In dSet.Tables(0).Rows comboBoxNo.Items.Add(r.Item(0)) Next r btnUpdate.Enabled = False btnDelete.Enabled = False With dataGridView .Columns(0).Width = 50 .Columns(1).Width = 65 .Columns(2).Width = 160 .Columns(2).HeaderText = "Designação" .Columns(3).Width = 75 .Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight .Columns(3).DefaultCellStyle.Format = ("##,##0.00") .Columns(4).Width = 75 .Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight .Columns(4).DefaultCellStyle.Format = ("##,##0.00") .Columns(5).Width = 75 .Columns(5).HeaderText = "Balanço" .Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight .Columns(5).DefaultCellStyle.Format = ("##,##0.00") End With ' Coloca o focus no combobox If comboBoxNo.CanFocus Then comboBoxNo.Focus() Catch ex As Exception MsgBox(ex.ToString()) End Try End Sub Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click Try a = 0 Call Md_FillEvents() 'Deslocar o ponteiro na Grid Me.dataGridView.CurrentCell = Me.dataGridView.Rows(txtBoxNo.Text - 1).Cells(0) Catch ex As Exception MsgBox(ex.ToString()) End Try End Sub Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click Try If (Not a = 0) Then a -= 1 Call Md_FillEvents() End If 'Deslocar o ponteiro na Grid Me.dataGridView.CurrentCell = Me.dataGridView.Rows(txtBoxNo.Text - 1).Cells(0) Catch ex As Exception MsgBox(ex.ToString()) End Try End Sub Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click Try If (Not a = dSet.Tables(0).Rows.Count - 1) Then a += 1 Call Md_FillEvents() End If 'Deslocar o ponteiro na Grid Me.dataGridView.CurrentCell = Me.dataGridView.Rows(txtBoxNo.Text - 1).Cells(0) Catch ex As Exception MsgBox(ex.ToString()) End Try End Sub Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click Try If (Not a = dSet.Tables(0).Rows.Count - 1) Then a = dSet.Tables(0).Rows.Count - 1 Call Md_FillEvents() End If 'Deslocar o ponteiro na Grid Me.dataGridView.CurrentCell = Me.dataGridView.Rows(txtBoxNo.Text - 1).Cells(0) Catch ex As Exception MsgBox(ex.ToString()) End Try End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click 'Move o cursor para a ultima linha da grid Me.dBind.MoveLast() Call Md_ClearAll() btnAnular.Enabled = True 'Conta o nº de linhas da grid para aplicar no numerador Dim NumeroLinha As Integer = Me.dataGridView.Rows.Count - 1 txtBoxNo.Text = (dataGridView.Item(0, NumeroLinha - 1).Value) + 1 DateTimePicker1.Focus() End Sub Private Sub Md_ClearAll() txtBoxNo.Text = "" txtBoxData.Text = "" txtBoxDesignacao.Text = "" txtBoxDespesa.Text = "" txtBoxReceita.Text = "" txtBoxBalanco.Text = "" comboBoxNo.Text = "" btnAdd.Enabled = True txtBoxNo.ReadOnly = False Call Md_FillCombo() txtBoxNo.Focus() End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click 'Conta o nº de linhas da grid para ir buscar o valor da ultima celula Dim NumeroLinha1 As Integer = Me.dataGridView.Rows.Count - 1 Dim Ultimobalanco As Double ' Coloca a cx. texto p/receber valor txtBoxBalanco.ReadOnly = False 'Proceder a operação de somar ou subtrair valores ao ultimo balanco 'Obter o valor de balanço face a despesa If txtBoxDespesa.Text = "" And txtBoxReceita.Text = "" Then MsgBox("Não pode haver valores em branco", MsgBoxStyle.Information, "Erro") txtBoxDespesa.Focus() Exit Sub ElseIf txtBoxDespesa.Text > 0 Then MsgBox("Vai Ocorrer uma despesa", MsgBoxStyle.Information, "Inserir Despesa") Ultimobalanco = dataGridView(5, NumeroLinha1 - 1).Value - txtBoxDespesa.Text txtBoxBalanco.Text = Format(Convert.ToDouble(Ultimobalanco), "##,##0.00") ElseIf txtBoxReceita.Text > 0 Then MsgBox("Vai Ocorrer uma receita", MsgBoxStyle.Information, "Inserir Receita") Ultimobalanco = dataGridView(5, NumeroLinha1 - 1).Value + txtBoxReceita.Text txtBoxBalanco.Text = Format(Convert.ToDouble(Ultimobalanco), "##,##0.00") End If Try Dim com As New OleDbCommand com.Connection = con com.CommandText = "insert into Details values(" & txtBoxNo.Text & ",'" & txtBoxData.Text & "','" & _ txtBoxDesignacao.Text & "','" & txtBoxDespesa.Text & "','" & txtBoxReceita.Text & "','" & txtBoxBalanco.Text & "')" com.ExecuteNonQuery() MsgBox("Registo Inserido") Call Md_FillCombo() Call Md_Settings() Call Md_ClearAll() Call SomarColuna() Call DarCorUltimaCelula() 'Coloca a cx. texto p/receber valr txtBoxBalanco.ReadOnly = True Catch exp As Exception MsgBox(exp.ToString()) End Try End Sub Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click Try Dim com As New OleDbCommand com.Connection = con com.CommandText = "update Details set Data='" & txtBoxData.Text & "',Designacao='" & _ txtBoxDesignacao.Text & "',Despesa='" & _ txtBoxDespesa.Text & "',Receita='" & _ txtBoxReceita.Text & "',Balanco='" & _ txtBoxBalanco.Text & "' where Id =" & comboBoxNo.Text com.ExecuteNonQuery() MsgBox("Record Updated") Call Md_Settings() Catch MsgBox("Error") End Try End Sub Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click Try Dim com As New OleDbCommand com.Connection = con com.CommandText = "delete from Details where Id=" & comboBoxNo.Text com.ExecuteNonQuery() MsgBox("Registo Apagado", MsgBoxStyle.Information, "Apagar Registo") 'Move o cursor para a ultima linha da grid Me.dBind.MoveLast() Call Md_Settings() Call Md_ClearAll() Call Md_FillCombo() Call SomarColuna() Call DarCorUltimaCelula() 'Coloca o Foco na Grid Me.dataGridView.Focus() Call AcertarBalanco() Catch exp As Exception MsgBox(exp.ToString()) End Try End Sub Private Sub comboBoxNo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboBoxNo.SelectedIndexChanged Try dSet.Tables(0).PrimaryKey = New DataColumn() {dSet.Tables(0).Columns("Id")} Dim row As DataRow row = dSet.Tables(0).Rows.Find(comboBoxNo.Text) txtBoxNo.Text = row("Id") txtBoxData.Text = row("Data") ' DateTimePicker1.Value = row("Data") txtBoxDesignacao.Text = row("Designacao") txtBoxDespesa.Text = Format(Convert.ToDouble(row("Despesa")), "##,##0.00") txtBoxReceita.Text = Format(Convert.ToDouble(row("Receita")), "##,##0.00") txtBoxBalanco.Text = Format(Convert.ToDouble(row("Balanco")), "##,##0.00") btnUpdate.Enabled = True btnDelete.Enabled = True txtBoxNo.ReadOnly = True btnAdd.Enabled = False btnAnular.Enabled = False Dim NumeroLinha As Integer = comboBoxNo.Text 'Acerta o valor da variavel a = comboBoxNo.Text - 1 'Deslocar o ponteiro na Grid Me.dataGridView.CurrentCell = Me.dataGridView.Rows(comboBoxNo.Text - 1).Cells(0) dataGridView.RefreshEdit() 'Move o cursor para a ultima linha da grid 'Me.dBind.MoveLast() Catch ex As Exception MsgBox(ex.ToString()) End Try End Sub Private Sub btnAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbout.Click Dim f As New frmAbout f.ShowDialog(Me) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Close() End Sub Private Sub txtBoxReceita_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBoxReceita.KeyPress 'so numeros e backspace If Char.IsLetter(e.KeyChar) Then e.Handled = True End If ' ao pressionar a tecla ENTER ( Return) o foco vai para o proximo controle If e.KeyChar = Convert.ToChar(Keys.Return) Then e.Handled = True btnAdd.Focus() ElseIf e.KeyChar = "."c Then ' converte o ponto para virgula e.Handled = True SendKeys.Send(",") End If End Sub Private Sub txtBoxDespesa_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBoxDespesa.KeyPress 'so numeros e backspace If Char.IsLetter(e.KeyChar) Then e.Handled = True End If ' ao pressionar a tecla ENTER ( Return) o foco vai para o proximo controle If e.KeyChar = Convert.ToChar(Keys.Return) Then e.Handled = True txtBoxReceita.Focus() ElseIf e.KeyChar = "."c Then ' converte o ponto para virgula e.Handled = True SendKeys.Send(",") End If End Sub Private Sub txtBoxDespesa_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBoxDespesa.LostFocus If txtBoxDespesa.Text = "" Then txtBoxDespesa.Text = "0,00" ElseIf txtBoxDespesa.Text > 0 Then txtBoxReceita.Text = "0,00" btnAdd.Focus() End If End Sub Private Sub txtBoxReceita_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBoxReceita.LostFocus If txtBoxReceita.Text = "" Then MsgBox("Não pode haver registos em branco", MsgBoxStyle.Information, "Registos em Branco") txtBoxReceita.Focus() Exit Sub End If End Sub Private Sub DateTimePicker1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles DateTimePicker1.KeyPress ' ao pressionar a tecla ENTER ( Return) o foco vai para o proximo controle If e.KeyChar = Convert.ToChar(Keys.Return) Then e.Handled = True txtBoxDesignacao.Focus() End If End Sub Private Sub DateTimePicker1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.LostFocus txtBoxData.Text = Format(DateTimePicker1.Value, "dd-MM-yyyy") End Sub Private Sub txtBoxDesignacao_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBoxDesignacao.KeyPress ' ao pressionar a tecla ENTER ( Return) o foco vai para o proximo controle If e.KeyChar = Convert.ToChar(Keys.Return) Then e.Handled = True txtBoxDespesa.Focus() End If End Sub Private Sub dataGridView_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dataGridView.CellContentClick ''Obter valor de celula 'MsgBox(dataGridView.Item(e.ColumnIndex, e.RowIndex).Value) End Sub Private Sub SomarColuna() 'Somar a coluna de Balanço Dim SOMA As Decimal Dim Soma1 As Decimal For Each coluna As DataGridViewRow In dataGridView.Rows SOMA = SOMA + coluna.Cells(4).Value Soma1 = Soma1 + coluna.Cells(3).Value Next TextBox1.Text = Format(Convert.ToDouble(SOMA - Soma1), "##,##0.00 €") txtBoxTotalReceita.Text = Format(Convert.ToDouble(SOMA), "##,##0.00 €") TexBoxTotalDespesa.Text = Format(Convert.ToDouble(Soma1), "##,##0.00 €") End Sub Private Sub DarCorUltimaCelula() 'Conta o nº de linhas da grid para ir buscar o valor da ultima celula Dim NumeroLinhas As Integer = Me.dataGridView.Rows.Count - 1 dataGridView.Item(5, NumeroLinhas - 1).Style.BackColor = Color.Yellow End Sub Private Sub AcertarBalanco() 'Conta o nº de linhas da grid para ir buscar o valor da ultima celula 'Dim Linhas As Integer = Me.dataGridView.Rows.Count - 1 'Dim Teste As Integer = comboBoxNo.Text - 1 'Dim X As Integer 'Dim ValorAcerto As Decimal = dataGridView.Item(5, Teste - 1).Value 'For X = Teste To Linhas ' dataGridView.Item(5, X).Value = ValorAcerto - dataGridView.Item(4, X).Value 'Next X End Sub Private Sub dataGridView_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dataGridView.SelectionChanged End Sub End Class Desde já Obrigado pela atenção Chamuanza EDIT: Adicionado Geshi Link to comment Share on other sites More sharing options...
Hellblazer Posted March 8, 2009 at 09:02 AM Report Share #249060 Posted March 8, 2009 at 09:02 AM Boas, Não estou a entender muito bem o teu problema... podias demonstrar com uma imagem pois parece ser um meio relativamente facil de entender o problema 😄 There are two ways to write error-free programs; only the third one works. Link to comment Share on other sites More sharing options...
Chamuanza Posted March 8, 2009 at 09:22 AM Author Report Share #249064 Posted March 8, 2009 at 09:22 AM Como e que eu mando uma imagem? Quando abro as opções adicionais, não me aparece a caixa para colocar o ficheiro Link to comment Share on other sites More sharing options...
jpaulino Posted March 8, 2009 at 09:44 AM Report Share #249066 Posted March 8, 2009 at 09:44 AM Como e que eu mando uma imagem? Quando abro as opções adicionais, não me aparece a caixa para colocar o ficheiro Utiliza um site externo do tipo http://www.imageshack.us/ Link to comment Share on other sites More sharing options...
Chamuanza Posted March 8, 2009 at 10:23 AM Author Report Share #249071 Posted March 8, 2009 at 10:23 AM Espero que seja isto Como se pode ver estão 5 registos inseridos (1;2;3;5;6), faltando o registo nº 4 que foi apagado de prposito.Quando me desloco na grid via teclado ou mesmo via combobox o ponteiro salta do registo nº 3 para o registo nº 6 http://img15.imageshack.us/img15/8909/despesaspessoais.gif 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