更新DataGridView [英] Updating DataGridView

查看:121
本文介绍了更新DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

在我的项目"中,有两个子窗体(子窗体1:-Add_InStock,子窗体2:-View_InStock)和一个MDI父窗体(MAIN).在子窗体1中,我正在将数据插入到
使用Button控件(添加按钮)并在datagridview中显示前25个数据的数据库.在子窗体2中,我将在datagridviw中显示直到日期的完整数据.

我想使用按钮控件(更新按钮)来编辑datagridview并更新子窗体1中存在的数据库.但是问题是当我在编辑datagridview时,
单击更新按钮,它显示确认datagriview已更新的消息,然后当我导航到子表格2以看到更改时;在那里
我看不到任何更改,我看不到更新的datagridview.
严重的行为是,当我再次导航和编辑以子窗体1形式显示的datagridview时,它成功更新并在datagridview中显示编辑后的数据
以儿童形式1出现.

结论是, 在第二次导航到另一个子窗体后更新,而不是在第一次更新.

Hello,

In My Project there are two child forms (child form 1:- Add_InStock,Child form 2:- View_InStock) and one MDI Parent form (MAIN).In Child Form 1 i am inserting data into the 
Database using the Button Control (Add Button) and displaying TOP 25 data into the datagridview.In Child form 2 i amd displaying the complete data till date in the datagridviw.

I want to Edit the datagridview and update the database present in child form 1 using the button control(Update Button). But the problem is when i am editing the datagridview and 
click on the update button it shows the message of confirmation that the datagriview is updated and then when i navigate to child form 2 in order to see the change ; there
i can't see any change i am not able to see the updated datagridview.
The starnge behaviour is that when i again navigate and edit datagridview present in child form 1 it gets updated successfully and shows the edited data in datagridview
present in child form 1.

Conclusion is that at  the second time it is updating after navigating to the other child form rather updating at the first time.

Imports System.Windows.Forms
Public Class MAIN
    Private Sub MAIN_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        System.Windows.Forms.Application.Exit() 'Exit the Application
    End Sub
    Private Sub MAIN_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'Code to Align the Form Title Text at the Centre
        Dim g As Graphics = Me.CreateGraphics()
        Dim startingPoint As Double = (Me.Width / 2) - (g.MeasureString(Me.Text.Trim, Me.Font).Width / 2)
        Dim widthOfASpace As Double = g.MeasureString(" ", Me.Font).Width
        Dim tmp As String = " "
        Dim tmpWidth As Double = 0
        Do
            tmp += " "
            tmpWidth += widthOfASpace
        Loop While (tmpWidth + widthOfASpace) < startingPoint
        Me.Text = tmp & tmp & Me.Text.Trim & tmp & tmp

        Me.WindowState = FormWindowState.Maximized 'MaximizeBox The MAIN Form
        'Code to Change the background color of the MDI Parent (MAIN) Form 
        Dim child As Control
        For Each child In Me.Controls
            If TypeOf child Is MdiClient Then
                child.BackColor = Color.White
                Exit For
            End If
        Next
        child = Nothing
        Dim VB_TITLE As New TITLE()
        'Set the Parent Form of the Child window.
        VB_TITLE.MdiParent = Me
        'Display the new form
        VB_TITLE.Show()
        'ToolStripStatusLabel1.Text = Format(Now, "dd/MM/yyyy")
    End Sub
    Private Sub INSTOCKToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs) Handles INSTOCKToolStripMenuItem1.Click
        Dim VB_Add_InStock As New Add_InStock()
        'Set the Parent Form of the Child window.
        VB_Add_InStock.MdiParent = Me
        'Display the new form
        VB_Add_InStock.Show()
        'Add_InStock.Show()
        'Me.Hide()
    End Sub
    Private Sub EXITToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EXITToolStripMenuItem.Click
        System.Windows.Forms.Application.Exit() 'Exit the Application
    End Sub
    Private Sub INSTOCKToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles INSTOCKToolStripMenuItem.Click
        Dim VB_VIEW_InStock As New VIEW_InStock()
        'Set the Parent Form of the Child window.
        VB_VIEW_InStock.MdiParent = Me
        'Display the new form
        VB_VIEW_InStock.Show()
    End Sub


Imports Npgsql
Imports System.Data
Imports System.Data.SqlClient
Public Class Add_InStock
    Dim adap As NpgsqlDataAdapter
    Dim ds As DataSet
    Dim cmdbl As NpgsqlCommandBuilder
    Dim Command As NpgsqlCommand
    Dim data_Table1 As New DataTable
    Dim ID As Integer
    Private Sub Add_InStock_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        VIEW_InStock.Close()
        'TODO: This line of code loads data into the 'DataSet2.add_instock' table. You can move, or remove it, as needed.
        Add_instockTableAdapter.Fill(Me.DataSet2.add_instock)
        Me.WindowState = FormWindowState.Maximized 'Maximise the add in stock form
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
        DataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
        ' code to change the datagridview header text
        DataGridView1.Columns(0).HeaderText = "Date"
        DataGridView1.Columns(1).HeaderText = "Name of Material"
        DataGridView1.Columns(2).HeaderText = "Company Manufacturer"
        DataGridView1.Columns(3).HeaderText = "Product Number"
        DataGridView1.Columns(4).HeaderText = "Serial Number"
    End Sub
'Add Button
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        
        DateTimePicker1.Value = DateTimePicker1.Value.ToShortDateString
        Try
            Dim connstring As String = String.Format("Server=127.0.0.1;Port=5432;" +
              "Username=xxxx;Password=xxxx;Database=SRUTILITY;")
            Dim conn As NpgsqlConnection = New NpgsqlConnection(connstring)
            conn.Open()
            Dim Command As NpgsqlCommand = New NpgsqlCommand("INSERT INTO add_instock values('" + DateTimePicker1.Value + "','" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "')", conn)
            Command.ExecuteNonQuery()
            conn.Close()
            MessageBox.Show("Data Saved.Click OK to Continue")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        ' After inserting clears all the textboxes
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        Try
            Dim connstring As String = String.Format("Server=127.0.0.1;Port=5432;" +
              "Username=xxxx;Password=xxxx;Database=SRUTILITY;")
            Dim conn As NpgsqlConnection = New NpgsqlConnection(connstring)
            conn.Open()
            Dim Command As NpgsqlCommand = New NpgsqlCommand("SELECT date,nameofmaterial,companymanufacturer,productnumber,serialnumber FROM add_instock ORDER by srnum DESC  LIMIT 25", conn)
            Dim reader As NpgsqlDataReader = Command.ExecuteReader()
            Dim data_Table1 As New DataTable
            data_Table1.Load(reader)
            DataGridView1.DataSource = data_Table1
            reader.Close()
            conn.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Try
            Dim connstring As String = String.Format("Server=127.0.0.1;Port=5432;" +
              "Username=xxxx;Password=xxxx;Database=SRUTILITY;")
            Dim conn As NpgsqlConnection = New NpgsqlConnection(connstring)
            conn.Open()
            Dim Command As NpgsqlCommand = New NpgsqlCommand("SELECT date,nameofmaterial,companymanufacturer,productnumber,serialnumber FROM add_instock ORDER by srnum DESC  LIMIT 25", conn)
            Dim reader As NpgsqlDataReader = Command.ExecuteReader()
            Dim data_Table1 As New DataTable
            data_Table1.Load(reader)
            DataGridView1.DataSource = data_Table1
            reader.Close()
            conn.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    'Update Button
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Me.Validate()
        Me.Add_instockTableAdapter.Update(Me.DataSet2.add_instock)
        MessageBox.Show("Information Updated Successfully!")
    End Sub


mports Npgsql
Imports Microsoft.Office.Interop
Public Class VIEW_InStock
    Dim connstring As String = String.Format("Server=127.0.0.1;Port=5432;" +
      "Username=xxx;Password=xxxx;Database=SRUTILITY;")
    Dim conn As NpgsqlConnection = New NpgsqlConnection(connstring)
    Private Sub VIEW_InStock_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DataSet2.add_instock' table. You can move, or remove it, as needed.
        Me.Add_instockTableAdapter.Fill(Me.DataSet2.add_instock)
        'Add_InStock.Close()
        Me.WindowState = FormWindowState.Maximized
        ' code to align the datagridview header as well as the datagridview content
        DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
        DataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
        ' code to change the datagridview header text
        DataGridView1.Columns(0).HeaderText = "Date"
        DataGridView1.Columns(1).HeaderText = "Name of Material"
        DataGridView1.Columns(2).HeaderText = "Company Manufacturer"
        DataGridView1.Columns(3).HeaderText = "Product Number"
        DataGridView1.Columns(4).HeaderText = "Serial Number"
    End Sub



推荐答案

Hi Tech Aspirant,

Hi Tech Aspirant,

也许DataGridView的状态仍为正在编辑",所以DataGridView中的数据将不会在第一时间更新.因此,我建议您尝试使用 在更新数据之前,先 EndEdit ().

Maybe the state of the DataGridView still is Editing, the data in the DataGridView will not be updated at the first time. So, I suggest you trying to use the EndEdit() before you Update the data.

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click Me.Validate()
Me.DataGridView.EndEdit() Me.Add_instockTableAdapter.Update(Me.DataSet2.add_instock) MessageBox.Show("Information Updated Successfully!") End Sub

如果您对此问题还有其他疑问,请随时告诉我.

If you have any other concern regarding this issue, please feel free to let me know.

最诚挚的问候,
唐有俊

Best regards,
Youjun Tang


这篇关于更新DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆