如何为datagridview visual basic 2012创建保存功能 [英] how to create save function for datagridview visual basic 2012

查看:81
本文介绍了如何为datagridview visual basic 2012创建保存功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用visual studio 2012和SQL server 2012来制作表格,将datagridview中的数据复制到另一个datagridview。



这是我的编码:

i'm using visual studio 2012 and SQL server 2012 to make form to copy data in datagridview to another datagridview.

this is my coding:

Public Class Form3

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.AllowUserToAddRows = False
        DataGridView2.AllowUserToAddRows = False
        ClassKoneksi.namadatabase = "KPIRWAN"
        Dim dssiswa As New DataSet
        Dim sql As String
        sql = "select*from Siswa order by NIS ASC"
        dssiswa = ClassSiswa.displayData(ClassSiswa.opencon, sql, "DataSiswa")
        DataGridView1.DataSource = dssiswa
        DataGridView1.DataMember = "DataSiswa"
        DataGridView1.ReadOnly = True
        DataGridView2.ColumnCount = DataGridView1.ColumnCount
        For a = 0 To DataGridView1.ColumnCount
            DataGridView2.Columns(a).Name = DataGridView1.Columns(a).Name
        Next a
        ClassSiswa.closecon()
    End Sub

    Private Sub ButtonCopy_Click(sender As Object, e As EventArgs) Handles ButtonCopy.Click
        Dim dr As New System.Windows.Forms.DataGridViewRow
        For Each dr In Me.DataGridView1.SelectedRows
            Me.DataGridView2.Rows.Add(dr.Cells(0).Value,
                                      dr.Cells(1).Value,
                                      dr.Cells(2).Value,
                                      dr.Cells(3).Value,
                                      dr.Cells(4).Value,
                                      dr.Cells(5).Value)

        Next
    End Sub

    Private Sub ButtonDel_Click(sender As Object, e As EventArgs) Handles ButtonDel.Click
        Dim LastRow As DataGridViewRow = (From this In DataGridView2.Rows.OfType(Of DataGridViewRow)() Where Not this.IsNewRow).LastOrDefault
        If LastRow IsNot Nothing Then
            DataGridView2.Rows.Remove(LastRow)
        End If
    End Sub
End Class





此表格的功能是:



- 按钮复制是用于将行从顶部datagridview复制到底部datagridview。



- 使用按钮删除底部datagridview中的行将被删除。



问题是:

我将顶部datagridview中的行复制到底部datagridview后我关闭了form.but,之后我再次运行程序底部的行datagridview消失。



我想要的是使用按钮保存创建一个保存功能,以便在我将顶部datagridview上的行复制到底部datagridview之后,当我再次运行程序时,底部的datagridview将保留在那里。



如何创建编码来执行此操作?



the function of this form is:

- button "copy" is used to copy the row from the top datagridview to the bottom datagridview.

- using button "delete" the row in the bottom datagridview will be removed.

the problem is:
after i copy the row in the top datagridview to the bottom datagridview i closed the form.but,after i run the program again the row in the bottom datagridview disappear.

what i want is to create a save function using button save so that after i copied the row on the top datagridview to the bottom datagridview, the row in the bottom datagridview will stay there when i running the program again.

how do i create a coding to do that?

推荐答案

你不需要这样做。只需绑定到相同的数据源。
You don't need to do that. Just bind to the same datasource.


这篇关于如何为datagridview visual basic 2012创建保存功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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