none
如何操作新加行? RRS feed

答案

  • 你好,

    欢迎来到MSDN中文论坛!

    这里有一个有关这个问题的完整的例子,希望对你有所帮助。 如果有什么问题,请及时回复。

    Imports System.Data

    Imports System.Windows.Forms

    Public Class Form1

        Public Sub New()

            InitializeComponent()

            Me.InitData()

            Me.InitUI()

        End Sub

     

        Private Sub BindingSourceAddNew_Load(ByVal sender As Object, ByVal e As EventArgs)

            Me.AddDemo()

        End Sub

     

        Private dataGridView1 As System.Windows.Forms.DataGridView

        Private bs As BindingSource

        Private dt As DataTable

     

        Private Sub InitData()

            Me.dt = New DataTable()

     

            Me.dt.Columns.Add("Id", GetType(Guid))

            Me.dt.Columns.Add("Name", GetType(String))

     

            Me.dt.Rows.Add(New Object() {Guid.NewGuid(), "Name1"})

            Me.dt.Rows.Add(New Object() {Guid.NewGuid(), "Name2"})

     

            Me.bs = New BindingSource(Me.dt, Nothing)

            AddHandler Me.bs.AddingNew, New System.ComponentModel.AddingNewEventHandler(AddressOf AddingNew)

        End Sub

     

        Private Sub AddingNew(ByVal sender As Object, ByVal e As System.ComponentModel.AddingNewEventArgs)

            ' 添加默认行的方式

            Dim view As DataView = DirectCast(Me.bs.List, DataView)

            Dim newobj As DataRowView = view.AddNew()

            newobj("Id") = Guid.NewGuid()

            newobj("Name") = "default"

            e.NewObject = newobj

        End Sub

     

        Private Sub InitUI()

            Me.dataGridView1 = New System.Windows.Forms.DataGridView()

            Me.dataGridView1.Dock = DockStyle.Fill

            Me.dataGridView1.DataSource = Me.bs

            Me.Controls.Add(Me.dataGridView1)

        End Sub

     

            ''' <summary>

            ''' AddNew 测试

            ''' </summary>

        Private Sub AddDemo()

            For i As Integer = 0 To 4

                Me.bs.AddNew()

            Next

        End Sub

        End Class

     

    Best regards

     

     


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年1月10日 5:58

全部回复

  • Hello 大海怪,

     欢迎来到MSDN 中文论坛!

     您能把你的问题说的更详细点吗?

     

    Best Regards

    Rcoky

    2011年1月6日 4:50
  • 你好,

    欢迎来到MSDN中文论坛!

    这里有一个有关这个问题的完整的例子,希望对你有所帮助。 如果有什么问题,请及时回复。

    Imports System.Data

    Imports System.Windows.Forms

    Public Class Form1

        Public Sub New()

            InitializeComponent()

            Me.InitData()

            Me.InitUI()

        End Sub

     

        Private Sub BindingSourceAddNew_Load(ByVal sender As Object, ByVal e As EventArgs)

            Me.AddDemo()

        End Sub

     

        Private dataGridView1 As System.Windows.Forms.DataGridView

        Private bs As BindingSource

        Private dt As DataTable

     

        Private Sub InitData()

            Me.dt = New DataTable()

     

            Me.dt.Columns.Add("Id", GetType(Guid))

            Me.dt.Columns.Add("Name", GetType(String))

     

            Me.dt.Rows.Add(New Object() {Guid.NewGuid(), "Name1"})

            Me.dt.Rows.Add(New Object() {Guid.NewGuid(), "Name2"})

     

            Me.bs = New BindingSource(Me.dt, Nothing)

            AddHandler Me.bs.AddingNew, New System.ComponentModel.AddingNewEventHandler(AddressOf AddingNew)

        End Sub

     

        Private Sub AddingNew(ByVal sender As Object, ByVal e As System.ComponentModel.AddingNewEventArgs)

            ' 添加默认行的方式

            Dim view As DataView = DirectCast(Me.bs.List, DataView)

            Dim newobj As DataRowView = view.AddNew()

            newobj("Id") = Guid.NewGuid()

            newobj("Name") = "default"

            e.NewObject = newobj

        End Sub

     

        Private Sub InitUI()

            Me.dataGridView1 = New System.Windows.Forms.DataGridView()

            Me.dataGridView1.Dock = DockStyle.Fill

            Me.dataGridView1.DataSource = Me.bs

            Me.Controls.Add(Me.dataGridView1)

        End Sub

     

            ''' <summary>

            ''' AddNew 测试

            ''' </summary>

        Private Sub AddDemo()

            For i As Integer = 0 To 4

                Me.bs.AddNew()

            Next

        End Sub

        End Class

     

    Best regards

     

     


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年1月10日 5:58