DataGridview单元格编辑模式 [英] DataGridview cell edit mode

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

问题描述

大家好,
我有以下问题.我有两种形式,父母和孩子.两种形式都包含一个DGV,并且第一个DGV被绑定.如果第一个DGV为空(源不包含任何记录),并且在新行的第一个单元格中键入内容,则在左侧显示铅笔 列,因此该单元格处于编辑模式,第二行新行显示在第一行下方.而且我可以保存第一行.
我想要的是,如果用户在第一个单元格上并按下鼠标右键,则第二个DGV将打开,用户在第二个DGV中选择一行,并将该行的某些值复制到父级的第一行DGV.我写了一些代码来做到这一点,那就是 没问题.问题是:将值复制后,该行或单元格就不在编辑模式下(没有铅笔),因此不会添加第二行,并且我无法保存第一行.

Hi all,
I have the following problem. I have two forms, parent and child. Both forms contains a DGV and the first DGV is bound. If the first DGV is empty (source contains no records), and I type something in the first cell on the new row, a pencil is shown in the left column so the cell is in edit mode and a second, new row is shown below the first row. And I'm able to save the first row.
What I want, is, if a user is on the first cell and presses the right mouse button, the second DGV opens and the user picks a row in the second DGV and some values of that row are copied to the first row in the parent DGV. I wrote some code to do that, that's not the problem. The problem is: after the values are copied, the row or cell aren't in edit mode (no pencil), so no second row will be added and I can't save my first row.

Private Sub DataGridView1_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
        If e.Button = MouseButtons.Right Then
            Dim column = DataGridView1.CurrentCell.ColumnIndex
            Select Case column
                Case 0
                    Dim rowIdx = e.RowIndex
                    Dim colIdx = e.ColumnIndex
                    Dim frm1 As Beg_Bestek = New Beg_Bestek(Beste_id)
                    frm1.Owner = Me
                    ' frm1.TopMost = True
                    frm1.ShowDialog()
                    If frm1.Tb_Omsch.TextLength <> 0 Then
                        DataGridView1.CurrentCell = DataGridView1(rowIdx, colIdx)
                        DataGridView1.CurrentRow.Cells("Mat").Value = frm1.Tb_Omsch.Text
                        DataGridView1.CurrentRow.Cells("Hoofdst").Value = frm1.Tb_Hoofdst.Text
                        DataGridView1.BeginEdit(True)
                    End If
                
            End Select
        End If
    End Sub

我希望,如果将单元格从第二个DGV复制回第一个DGV,我可以将BeginEdit设置为true,以获取铅笔并随后保存数据.但没有运气.
有任何想法吗?

问候,

Mark Hofland

I hoped that if I copied the cells from the second DGV back to the first DGV, that I could set BeginEdit to true, to get the pencil and afterward saving my data. But no luck.
Any Idea's?

regards,

Mark Hofland

推荐答案

这是一些非常基本的代码,可将数据从Form2非数据绑定的DGV复制到Form1数据表(绑定到Form1 DGV)

Here is some very basic code that copies data from Form2 non-databound DGV to Form1 DataTable (bound to Form1 DGV).

它通过绑定(myTable)将行添加到Form1上的DGV.

It adds rows to DGV on Form1 via its binding (myTable).

这可能不是您想要的那种东西,但这是我想出的.

It may not be the sort of thing you want, but it is what I came up with.

FORM1

' Form1 with databound DGV and Button1
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
    Public myTable As New DataTable("Freddy")
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With myTable
            .Columns.Add("One", GetType(String))
            .Columns.Add("Two", GetType(Integer))
            .Columns.Add("Three", GetType(String))
            .Columns.Add("Four", GetType(String))
            For i As Integer = 0 To 5
                .Rows.Add("First" & i.ToString, i.ToString, "Andrew" & i.ToString, "Smith" & i.ToString)
            Next
        End With
        DataGridView1.DataSource = myTable
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Form2.Show()
    End Sub
End Class

FORM2

' Form2 with default DGV and Button1 Option Strict On Option Explicit On Option Infer Off Public Class Form2 Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load With DataGridView1 .Columns.Add("Column1", "Column1") .Columns.Add("Column2", "Column2") .Columns.Add("Column3", "Column3") .Columns.Add("Column4", "Column4") For i As Integer = 0 To 5 .Rows.Add("Second" & i.ToString, i.ToString, "June" & i.ToString, "Arrkwright" & i.ToString) Next End With End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

       '从一个"行复制项目和四个"到mytable

        ' copy items from rows "One" and "Four" to mytable

Dim dr As DataRow = Form1.myTable.NewRow dr.Item(一个")= DataGridView1.CurrentRow.Cells("Column1").Value dr.Item(四个")= DataGridView1.CurrentRow.Cells("Column1").Value Form1.myTable.Rows.Add(dr) 结束子 结束班级

Dim dr As DataRow = Form1.myTable.NewRow dr.Item("One") = DataGridView1.CurrentRow.Cells("Column1").Value dr.Item("Four") = DataGridView1.CurrentRow.Cells("Column1").Value Form1.myTable.Rows.Add(dr) End Sub End Class


这篇关于DataGridview单元格编辑模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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