具有BindingSource的DataGridView [英] DataGridView With BindingSource

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

问题描述

你好,
我正在尝试捕获DataGridView控件中现有行或新行的用户编辑,如果用户编辑现有行,我想保存更改,如果用户添加了一个新对象,我想在集合中插入一个新对象新行.
我为BindingSource和DataGridView尝试了许多事件,但是我无法区分用户是添加新行还是编辑现有行.

Hello,
I am trying to catch the User edits for an existing rows or new row in a DataGridView control, I want to save the changes if the user edits an existing row, and I want to insert a new object in the collection if the user added a new row.
I tried many events for the BindingSource and DataGridView but I am not able to differentiate if the user added new row or if he edit an existing row.

''''''''''''''''''''''''''''''''''''''''
' Data Access
''''''''''''''''''''''''''''''''''''''''
Imports System.Data.SqlClient
Namespace Data
    Public Class BankDAL
        Private _Connection As SqlConnection = UtilitiesDAL.Connection

        'Insert
        Public Function Insert(ByVal Name_ar As String, ByVal Name_en As String) As Integer
            Dim BankId As Integer
            Dim cmdInsert As New SqlCommand("gen_Banks_Insert", _Connection)
            Dim prmName_ar As New SqlParameter("@Name_ar", Name_ar)
            Dim prmName_en As New SqlParameter("@Name_en", Name_en)
            With cmdInsert
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add(prmName_ar)
                .Parameters.Add(prmName_en)
            End With 'cmdInsert
            Try
                _Connection.Open()
                BankId = CType(cmdInsert.ExecuteScalar, Integer)
                _Connection.Close()
            Catch ex As Exception
                Throw ex
            Finally
                _Connection.Close()
            End Try
            Return BankId
        End Function

        'Update
        Public Sub Update(ByVal BankId As Integer, ByVal Name_ar As String, ByVal Name_en As String)
            Dim cmdUpdate As New SqlCommand("gen_Banks_Update", _Connection)
            Dim prmBankId As New SqlParameter("@BankId", BankId)
            Dim prmName_ar As New SqlParameter("@Name_ar", Name_ar)
            Dim prmName_en As New SqlParameter("@Name_en", Name_en)
            With cmdUpdate
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add(prmBankId)
                .Parameters.Add(prmName_ar)
                .Parameters.Add(prmName_en)
            End With 'cmdUpdate
            Try
                _Connection.Open()
                cmdUpdate.ExecuteNonQuery()
                _Connection.Close()
            Catch ex As Exception
                Throw ex
            Finally
                _Connection.Close()
            End Try
        End Sub

        'Delete
        Public Sub Delete(ByVal BankId As Integer)
            Dim cmdDelete As New SqlCommand("gen_Banks_Delete", _Connection)
            Dim prmBankId As New SqlParameter("@BankId", BankId)
            With cmdDelete
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add(prmBankId)
            End With 'cmdDelete
            Try
                _Connection.Open()
                cmdDelete.ExecuteNonQuery()
                _Connection.Close()
            Catch ex As Exception
                Throw ex
            Finally
                _Connection.Close()
            End Try
        End Sub

        'This will Get the Bank By ID
        Public Function GetByID(ByVal BankId As Integer) As SqlDataReader
            Dim cmdGetByKey As New SqlCommand("gen_Banks_GetById", _Connection)
            Dim prmBankId As New SqlParameter("@BankId", BankId)
            With cmdGetByKey
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add(prmBankId)
            End With 'cmdGetByKey
            _Connection.Open()
            Return cmdGetByKey.ExecuteReader(CommandBehavior.CloseConnection)
        End Function

        'This will Get all Banks
        Public Function GetAll() As SqlDataReader
            Dim cmdGetAll As New SqlCommand("gen_Banks_GetAll", _Connection)
            With cmdGetAll
                .CommandType = CommandType.StoredProcedure
            End With 'cmdGetAll
            _Connection.Open()
            Return cmdGetAll.ExecuteReader(CommandBehavior.CloseConnection)
        End Function
    End Class
End Namespace

''''''''''''''''''''''''''''''''''''''''
' Entity 
''''''''''''''''''''''''''''''''''''''''
Imports BingoPOS.DataAccess.Data
Namespace Business
    Public Class Bank

        Private _BankId As Integer
        Private _Name_ar As String
        Private _Name_en As String

        Public ReadOnly Property BankID() As Integer
            Get
                Return _BankId
            End Get
        End Property
        Public Property Name_ar() As String
            Get
                Return _Name_ar
            End Get
            Set(ByVal Value As String)
                _Name_ar = Value
            End Set
        End Property
        Public Property Name_en() As String
            Get
                Return _Name_en
            End Get
            Set(ByVal Value As String)
                _Name_en = Value
            End Set
        End Property

        'Default Constructor
        Public Sub New()

        End Sub

        'This constructor will load the  Bank information according to the BankId
        Public Sub New(ByVal BankID As Integer)
            _BankId = BankID
            Dim oDataReader As SqlClient.SqlDataReader
            Dim oBankDAL As New BankDAL
            oDataReader = oBankDAL.GetByID(_BankId)
            If oDataReader.HasRows Then
                While oDataReader.Read
                    _Name_ar = oDataReader("Name_ar")
                    _Name_en = oDataReader("Name_en")
                End While
            End If
            oDataReader.Close()
        End Sub

        'The Construcor will load all of the Bank  member variables.
        Public Sub New(ByVal BankId As Integer, ByVal Name_ar As String, ByVal Name_en As String)
            Me._BankId = BankId
            Me._Name_ar = Name_ar
            Me._Name_en = Name_en
        End Sub

        'Insert
        Public Sub Insert()
            Dim oBankDAL As New BankDAL
            _BankId = oBankDAL.Insert(_Name_ar, _Name_en)
        End Sub

        'Update
        Public Sub Update()
            Dim oBankDAL As New BankDAL
            oBankDAL.Update(_BankId, _Name_ar, _Name_en)
        End Sub

        'Delete
        Public Sub Delete()
            Dim oBankDAL As New BankDAL
            oBankDAL.Delete(_BankId)
        End Sub
    End Class
End Namespace

''''''''''''''''''''''''''''''''''''''''
' Collection
''''''''''''''''''''''''''''''''''''''''
Imports BingoPOS.DataAccess.Data
Imports System.Data.SqlClient

Namespace Business
    Public Class BankCollection
        Inherits CollectionBase

        'The Default Constructor for Bank
        Public Sub New()

        End Sub

        'This will add a new Bank to the Collection
        Public Shadows Sub Add(ByVal oBank As Bank)
            List.Add(oBank)
        End Sub
        'This will remove a Bank from the Collection
        Public Shadows Sub Remove(ByVal oBank As Bank)
            For intIndex As Integer = 0 To List.Count - 1
                If oBank.BankID = CType(List.Item(intIndex), Bank).BankID Then
                    List.Remove(List.Item(intIndex))
                    Exit For
                End If
            Next
        End Sub
 
        'This sub will load the list with the information in the data reader 
        Private Sub LoadListFromDataReader(ByVal oDataReader As SqlDataReader)
            If oDataReader.HasRows Then
                'Make sure to clear the inner list before filling it with the new information
                List.Clear()
                While oDataReader.Read
                    List.Add(New Bank(oDataReader("BankId"), oDataReader("Name_ar"), oDataReader("Name_en")))
                End While
            End If
        End Sub

        'This will load the collection with all records in the table
        Public Sub GetAll()
            Dim oDataReader As SqlClient.SqlDataReader
            Dim oBankDAL As New BankDAL
            oDataReader = oBankDAL.GetAll()
            If oDataReader.HasRows Then
                'Make sure to clear the inner list before filling it with the new information
                List.Clear()
                While oDataReader.Read
                    List.Add(New Bank(oDataReader("BankId"), oDataReader("Name_ar"), oDataReader("Name_en")))
                End While
            End If
            oDataReader.Close()
        End Sub
    End Class
End Namespace


''''''''''''''''''''''''''''''''''''''''
' Form
''''''''''''''''''''''''''''''''''''''''
Imports BingoPOS.Business.Business
Imports System.ComponentModel

Public Class BankSetup
    Private oBankCollection As New BankCollection
    Private oBank As Bank
    Private WithEvents BankBindingSource As BindingSource

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        Call FillBanks()
    End Sub

    'Fill DataGridView.
    Private Sub FillBanks()
        oBankCollection = New BankCollection
        oBankCollection.GetAll()
        BankBindingSource = New BindingSource
        BankBindingSource.DataSource = oBankCollection
        Me.grv_banks.DataSource = BankBindingSource
    End Sub



    'Delete Selected Banks.
    Private Sub grv_banks_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles grv_banks.UserDeletingRow
        For Each DataGridViewRow In Me.grv_banks.SelectedRows
            CType(BankBindingSource.Current, Bank).Delete()
        Next
    End Sub
End Class




请帮助,我想知道用户何时完成编辑现有行以及用户何时完成添加新行.

问候,
Sameer Alomari




Please help, I want to know when the user completes editing an existing row and when the user completes adding a new row.

Regards,
Sameer Alomari

推荐答案

首先,我是vb的初学者. 但我会尽力帮助您.
如果我不明白您的问题,您有一个数据网格,并且您想知道某人是否已更改某些内容或是新的

首先,我将使用数据表从数据库中检索我的数据
如果您通过事件与朋友一起执行此操作,则可以使用数据表具有的某些操作(RowChanged,ColumnChanged,RowDeleting ...)

如果那没有帮助,我会放一个新专栏
就像
first of all i am a beginer in vb
but i will try to help you.
if i understund your question you have a datagrid and you want to know if somebody ha changed something or is new

first of all i would use a datatable to retrieve my data from the database
if you do this with friend with events you can use some actions that the datatable has (RowChanged, ColumnChanged, RowDeleting ...)

if that does not help i would put a new column
like
datatable.column.add("newrow", gettype(system.boolean))
datatable.Columns("newrow").DefaultValue = true


在旧的里放假
这样我就可以从旧行中识别出新行

我希望我能为您提供帮助

祝你有美好的一天


put false in the old ones
so that i can identify the new rows from the old ones

i hope that i could help you

have a nice day


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

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