编辑更新 DatagridView VB.Net(无数据库)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15629088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:46:09  来源:igfitidea点击:

Edit Update DatagridView VB.Net (No Database)

vb.netdatagridview

提问by RedJohn

Good day everyone. I need your help in this project I am into (a Visual Basic program with no database.) It just contains a Datagridview, a Textbox, and three buttons (an "Add" Button, a "Edit" and an "Update" Button).

今天是个好日子。我在这个项目中需要你的帮助(一个没有数据库的 Visual Basic 程序。)它只包含一个 Datagridview、一个文本框和三个按钮(一个“添加”按钮、一个“编辑”和一个“更新”按钮) .

1 . Is there any way (like using "for loop") to automatically assign DataGridView1.Item("item location")to the one edited and be updated?

1 . 有什么方法(比如使用“for 循环”)可以自动分配DataGridView1.Item("item location")给编辑过的并进行更新?

2 . Or is it possible to just click an item in the Datagridview then it will be edited at that without passing it to a Textbox, and to be updated at that.

2 . 或者是否可以只单击 Datagridview 中的一个项目,然后将在该项目进行编辑而不将其传递给文本框,并在该项目进行更新。

回答by yu_ominae

The DataGridViewCellEventArgsvariable (ein the method stub the designer will generate for you) of the double click event of the cell has RowIndexand ColumnIndexproperties which refer to the position of the cell you clicked.

DataGridViewCellEventArgs变量(e在方法存根设计师将为您生成)细胞的双击事件都有RowIndexColumnIndex这指的是你单击单元格的位置属性。

Save those (in a class variable possibly or a local one if that's all you need) and then refer to them when you update the cell in your DataGridView, possibly like this MyDataGridView.Item(e.ColumnIndex, e.RowIndex)or MyDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)where eis the variable from the double click event handler.

保存这些(可能在一个类变量中,如果这就是你需要的本地变量),然后当你更新你的单元格时引用它们DataGridView,可能像这样MyDataGridView.Item(e.ColumnIndex, e.RowIndex)或者来自双击事件处理程序的变量MyDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)在哪里e

For you cell double click event you could have something like this:

对于你的单元格双击事件,你可以有这样的事情:

Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick

    Using myEditor As New frmCellEditor(Me.DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value)
        If myEditor.ShowDialog() = DialogResult.OK Then
            Me.DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value = myEditor.NewCellValue
        End If
    End Using

End Sub

This will call a new instance of your editor and get a value from you. For the purpose of this demo I have made a form like this:

这将调用您的编辑器的一个新实例并从您那里获得一个值。为了这个演示的目的,我制作了一个这样的表格:

Public Class frmCellEditor

公共类 frmCellEditor

    Public NewCellValue As Integer

    Public Sub New(ByVal CurrentCellValue As Object)

        InitializeComponent()

        Me.TextBox1.Text = CStr(CurrentCellValue)

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Me.NewCellValue = CInt(Me.TextBox1.Text)

        Me.DialogResult = DialogResult.OK

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Call Me.Close()

    End Sub
End Class

Which just has two buttons (Button1= OK, Button2= Cancel). When you click OK, it just returns the value 1which then gets set as the value of the cell.

它只有两个按钮(Button1= OK,Button2= Cancel)。当您单击“确定”时,它只返回值1,然后将其设置为单元格的值。

This is a VERY simplistic example, but it should provide you the basics of what you are trying to do.

这是一个非常简单的例子,但它应该为您提供您正在尝试做的事情的基础知识。

UPDATE:

更新:

I updated the code for the editor interface so it will include handling for passing the value back and forth from the form with your datagridview.

我更新了编辑器界面的代码,因此它将包括处理从带有 datagridview 的表单来回传递值的处理。

In your project, make a new form called frmCellEditor. This forms has to have two buttons and a textbox (Make sure that the programmatic names match!). Replace the code with the code listed above. You will have to add Imports System.Windows.Formsabove the class as well.

在您的项目中,创建一个名为frmCellEditor. 这个表单必须有两个按钮和一个文本框(确保程序名称匹配!)。将代码替换为上面列出的代码。您还必须Imports System.Windows.Forms在类上方添加。

Amend the event handler for the cell double click event of your datagrid to pass the cell value when frmCellEditoris constructed (the line going ... New frmCellEditor(...).

修改数据网格的单元格双击事件的事件处理程序,以在frmCellEditor构造时传递单元格值 (行... New frmCellEditor(...).

回答by Ruben_PH

How many columns does your DataGridView has?
Based on how you populate your DataGridView, I'll assume only 1.
Declare this on top of your form

您的 DataGridView 有多少列?
根据您填充 DataGridView 的方式,我假设只有 1 个
。在表单顶部声明

Dim i as Integer  

On your btnUpdate_Click Event (Just combine your Edit and Update button into One)

在您的 btnUpdate_Click 事件上(只需将您的“编辑”和“更新”按钮合二为一)

SELECT CASE btnUpdate.Text
    Case "Update"
         With DataGridView1
              'Check if there is a selected row
              If .SelectedRows.Count = 0 Then
                   Msgbox "No Row Selected for Update"
                   Exit Sub
              End If
              i = .CurrentRow.Index 'Remember the Row Position
              Textbox1.Text = .item(0 ,i).value 'Pass the Value to the textbox
              .Enabled = False 'Disable DataGridView to prevent users from clicking other row while updating. 
              btnUpdate.Text = "Save"
         End With
    Case Else 'Save
         DatagridView1.Item(0,i).Value = Textbox1.Text
         btnUpdate.Text = "Update"
END SELECT  

回答by RedJohn

Thanks for those who contributed to finding answers for this thread. I have not used your solutions for now (maybe some other time). After some research, I've found an answer for problem 2 (more user friendly at that):

感谢那些为寻找这个线程的答案做出贡献的人。我暂时还没有使用您的解决方案(也许其他时间)。经过一番研究,我找到了问题 2 的答案(对用户更友好):

2 . Or is it possible to just click an item in the Datagridview then it will be edited at that without passing it to a Textbox, and to be updated at that.

2 . 或者是否可以只单击 Datagridview 中的一个项目,然后将在该项目进行编辑而不将其传递给文本框,并在该项目进行更新。

Here's what i did: in Private Sub Form1_Load, just add:

这是我所做的:在 Private Sub Form1_Load 中,只需添加:

yourDataGridView.EditMode = DataGridViewEditMode.EditOnEnter

in Private Sub yourDataGridView_(whatever event here: DoubleCellClick, CellContentClick, etc.) add:

在 Private Sub yourDataGridView_(这里的任何事件:DoubleCellClick、CellContentClick 等)中添加:

DataGridView1(e.ColumnIndex, e.RowIndex).[ReadOnly] = False
DataGridView1.BeginEdit(False)