在vb.net中编辑datagridview行 [英] editing datagridview row in vb.net

查看:76
本文介绍了在vb.net中编辑datagridview行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的datagridview1的数据源是datatble.我添加了另外2个datagridviewButton列(edit,delete).当我单击编辑"时,整个行应处于编辑模式,并且按钮标签将更改为保存和取消.尽管有当前行,但对于datagridview中显示的所有行,这些按钮组都在变化.如何避免这种情况?还有其他任何方法,例如默认模板或其他...对datagridview行进行编辑和删除操作.

这是我的代码:

Datasource for my datagridview1 is datatble. I have added 2 more datagridviewButton columns(edit,delete). when I click on edit the entire row should be in edit mode and button labels will be changed to save and cancel. despite of current row,for all rows presented in datagridview, these button laels are changing. How to avoid this? Is there any other way like default template or etc... for edit and delete operations on datagridview rows.

Here is my code:

Private dataTable As New DataTable
    Private _editButton As New DataGridViewButtonColumn
    Private _deleteButton As New DataGridViewButtonColumn

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'edit button
        _editButton.HeaderText = ""
        _editButton.Name = "Edit"
        _editButton.Text = "Edit"
        _editButton.UseColumnTextForButtonValue = True
        _editButton.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        DataGridView1.Columns.Add(_editButton)

        'delete button
        _deleteButton.HeaderText = ""
        _deleteButton.Text = "Delete"
        _deleteButton.Name = "Delete"
        _deleteButton.UseColumnTextForButtonValue = True
        _deleteButton.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        DataGridView1.Columns.Add(_deleteButton)

End Sub


Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.CellClick

        'Ignore clicks on other than button cells
        If Not DataGridView1.Columns("Edit") Is Nothing Then
            If DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns("Edit").Index And _editButton.Name = "Edit" Then

                _editButton.Text = "Save"
                _editButton.Name = "Save"
                _deleteButton.Text = "Cancel"
                _deleteButton.Name = "Cancel"
                For Each cell As DataGridViewCell In DataGridView1.CurrentRow.Cells
                    DataGridView1.CurrentRow.ReadOnly = False
                    cell.Style.BackColor = Color.Yellow
                    cell.ReadOnly = False
                Next
                DataGridView1.CurrentCell = DataGridView1.CurrentRow.Cells(0)
            ElseIf DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns("Delete").Index And _deleteButton.Name = "Delete" Then
                DataGridView1.Rows.RemoveAt(DataGridView1.CurrentCell.RowIndex)
            End If

        ElseIf DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns("Save").Index And _editButton.Name = "Save" Then
            _editButton.Text = "Edit"
            _editButton.Name = "Edit"
            _deleteButton.Text = "Delete"
            _deleteButton.Name = "Delete"
            DataGridView1.CurrentRow.ReadOnly = True
        ElseIf DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns("Cancel").Index And _deleteButton.Name = "Cancel" Then
            _editButton.Text = "Edit"
            _editButton.Name = "Edit"
            _deleteButton.Text = "Delete"
            _deleteButton.Name = "Delete"
            DataGridView1.CurrentRow.ReadOnly = True
        End If
    End Sub

推荐答案

不要更改列的名称.试试这个
Don''t change the name of column. Try this
DataGridView1(DataGridView1.Columns("Edit").Index,DataGridView1.CurrentCell.RowIndex).Value = "Save" 'or whatever



我的意思是,您需要根据Value更改条件.


它应该像这样.



You need to change your conditions accordingly, I mean, based on Value.


It should go like this.

If dgv(dgv.Columns("Edit").Index, dgv.CurrentCell.RowIndex).Value = "Edit" Then
  dgv(dgv.Columns("Edit").Index, dgv.CurrentCell.RowIndex).Value = "Save"
Else
  dgv(dgv.Columns("Edit").Index, dgv.CurrentCell.RowIndex).Value = "Edit"
End If


这篇关于在vb.net中编辑datagridview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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