5 Replies - 8198 Views - Last Post: 11 March 2012 - 01:51 AM Rate Topic: -----

#1 remya1000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-December 10

VB.Net DataGridView Cell Editable

Posted 09 March 2012 - 01:04 PM

Im using VB.Net.

Im using DataGridView with ColumnType = DataGridViewTextBoxColumn.

I have 3 columns. Column1, Column2, Column3.

For example:
Column1          Column2          Column3
1                        AA                AAA
2                        BB                EDIT
3                        CC                CCC



If column3s value = EDIT, then I need to edit only that cell.

Here for example: i need to make the Column3s second cell to be readonly=false.

So i cannot make any changes to (1, AA, AAA), (3, CC, CCC) and (2, BB). Need to make changes only EDIT cell.

I tried this code. CountR is the row in which EDIT values is there.

DataGridView1.Item(2, CountR).ReadOnly = False 


DataGridView1.Rows(CountR).Cells(2).ReadOnly = False



But this makes every cell in column editable.

Is there a way i can make some cells editable in column?

If you know how to do it, please help me. if you can provide an example, then that will be so helpful.

Thanks in Advance.

Is This A Good Question/Topic? 0
  • +

Replies To: VB.Net DataGridView Cell Editable

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: VB.Net DataGridView Cell Editable

Posted 09 March 2012 - 01:50 PM

I'm thinking a for...each to check each cell in the datagrid and if the value is "EDIT" then readonly = false, else readonly = true. And then on Cell Leave run the routine again to make sure what you just edited is no longer editable.

Problems I see is if some start to type something and moves out of the field then it is no longer editable. This would be a problem is a typo was made as it would no longer have the value "EDIT"
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: VB.Net DataGridView Cell Editable

Posted 09 March 2012 - 02:05 PM

Play around with this, It might work for you
    Private Sub DisableNonEdits()
        DataGridView1.AllowUserToAddRows = False
        For Each r As DataGridViewRow In DataGridView1.Rows
            For x As Integer = 0 To DataGridView1.Columns.Count - 1
                If r.Cells(x).Value.ToString = "EDIT" Then
                    r.Cells(x).ReadOnly = False
                Else
                    r.Cells(x).ReadOnly = True
                End If
            Next
        Next
    end sub

You should be able to call it after you populate your dgv and then in your cell_leave event of the dgv this way, once you change it from edit it is no longer editable.

This post has been edited by CharlieMay: 09 March 2012 - 02:06 PM

Was This Post Helpful? 0
  • +
  • -

#4 remya1000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-December 10

Re: VB.Net DataGridView Cell Editable

Posted 09 March 2012 - 03:02 PM

Thanks CharlieMay for your reply.

is there a way i can make the cell editable full time instead of editable for just once.

Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: VB.Net DataGridView Cell Editable

Posted 09 March 2012 - 03:50 PM

So in the first pass of the filled grid, if it is edit you want that to remain editable?

If so, then I guess just don't recall the sub in the Leave event. That is what is rechecking. But I would maybe change the background or the foreground of the cells color so that it is visibly known that it is editable.
Was This Post Helpful? 0
  • +
  • -

#6 remya1000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-December 10

Re: VB.Net DataGridView Cell Editable

Posted 11 March 2012 - 01:51 AM

CharlieMay, thank you so much for your help.

As you said i try this code, and it's working...


                For Each r As DataGridViewRow In DataGridView1.Rows
                    If r.Cells(3).Value.ToString = "EDIT" Then
                        r.Cells(3).ReadOnly = False
                    Else
                        r.Cells(3).ReadOnly = True
                    End If
                Next



Also i change the background.

Thank you so much for your help.....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1