Click here to Skip to main content
12,299,332 members (54,695 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB .NET VB.NET
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
Posted 5-Aug-11 0:44am
Edited 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
Prerak Patel 5-Aug-11 6:56am
   
:doh: You are changing column text, then obviously it is going to be reflected in all rows.
Member 8001800 5-Aug-11 6:57am
   
@ Prerak Patel: Thank you very much for showing intrest in solving this problem
Member 8001800 5-Aug-11 7:00am
   
what can i do instead of changing column text? Did I choose complex way to do this sort of edit and delete operation? Is there any other simple way to do this?
Prerak Patel 5-Aug-11 7:02am
   
Check the answer. If it helps, accept it, or comment there.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Don't change the name of column. Try this
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.
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
  Permalink  
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
Member 8001800 5-Aug-11 7:17am
   
I am posting all my code. so that u can have better idea what i did.
Member 8001800 5-Aug-11 7:29am
   
If it is a datagrid cell, your suggested code is working fine but not for datagridviewbuttoncolumn
Prerak Patel 5-Aug-11 7:56am
   
You need to change the cell value only for current row. That was the problem, isn't it? And you didn't put modified code. No one here will do all modifications.
Member 8001800 5-Aug-11 8:25am
   
i updated the question with all my code as you suggested. If you need anything more i am ready to provide for you in solving this problem
Prerak Patel 5-Aug-11 8:43am
   
I asked you to do the modification and then put your modified code if you are stuck.
Member 8001800 5-Aug-11 8:45am
   
I found an alternative for this problem

If i use DatagridviewLinkColumn instead of DatagridviewButtonColumn, I can able to change the cell value.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160525.2 | Last Updated 5 Aug 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100