Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:

VB
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
Posted
Updated 5-Aug-11 1:37am
v2
Comments
Member 8001800 5-Aug-11 6:45am    
It is very very urgent...anyone please help me as soon as u can
Prerak Patel 5-Aug-11 6:48am    
Windows application or web?
Member 8001800 5-Aug-11 6:48am    
it is windows application
Prerak Patel 5-Aug-11 6:52am    
Where and what is the _editButton?
Member 8001800 5-Aug-11 6:55am    
both _editButton and _deleteButton are DatagridviewButtonColumns

Private _editButton As New DataGridViewButtonColumn
Private _deleteButton As New DataGridViewButtonColumn

1 solution

Don't change the name of column. Try this
VB
DataGridView1(DataGridView1.Columns("Edit").Index,DataGridView1.CurrentCell.RowIndex).Value = "Save" 'or whatever


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

[Edit]
It should go like this.
VB
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
 
Share this answer
 
v3
Comments
Member 8001800 5-Aug-11 7:08am    
the label is not changing to "Save"
Prerak Patel 5-Aug-11 7:15am    
It should be a Button! Anyways, how you did it?
Prerak Patel 5-Aug-11 7:16am    
Update in question.
Member 8001800 5-Aug-11 8:21am    
It is same again. Button label is not changing. Can we treat both cell value and button lable in same way?
Member 8001800 5-Aug-11 8:23am    
Is there any other way to do editing or deleting a row in datagridview? Because i think this is quite complex way and not getting result what i expect

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900