Solved

VB.Net2 Change text on individual button in Datagridview

Posted on 2013-05-16
3
1,616 Views
Last Modified: 2013-05-17
Hi Experts.  I have a datagridview with a column of buttons that all start off with the text "Off".  

I want to be able to change the text to "On" when the button is pressed and back to "Off when pressed again.    So some will say "Off"  and Some "On".   Please could someone help with an example of how this could be done?  

I will also need to get the condition of the button later, whether its text is "Off" or "On".   Help there would also be appreciated.    Many Thanks for the help
0
Comment
Question by:PNRT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39171550
Every control has a Tag property. You can store True or False in this property to indicate current state

Button1_Click:
Button1.Tag = Not Button1.Tag

If Button1.Tag Then
    Button1.Text = "On"
Else
    Button1.Text = "Off"
End If
0
 
LVL 2

Author Comment

by:PNRT
ID: 39172538
Thanks for the reply Codecruiser, but how do I get the button click of the button in the particular row of the datagridview.     I can see how to do it with a normal button but I need to do it with the individual buttons in the datagridview column.  Thanks again.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39174304
Ah sorry I missed the datagridview part. You will need to handle the CellClick event of the grid and if clicked cell is button cell then set cell's value to On and Off based on existing value

Something like this

Private Sub dataGridView1_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dataGridView1.CellClick

        ' Ignore clicks that are not on button cells.  
        If e.ColumnIndex = YourButtonColumnIndex Then
            If datagridView1.Item(e.ColumnIndex, e.RowIndex).Value.Tostring = "On" Then
               datagridView1.Item(e.ColumnIndex, e.RowIndex).Value = "Off"
            Else
               datagridView1.Item(e.ColumnIndex, e.RowIndex).Value = "On"
            End If
        End If

        ' Retrieve the task ID.
        Dim taskID As Int32 = CInt(dataGridView1(0, e.RowIndex).Value)
    End Sub
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net  Exporting Excel Woorkbook to PDF 7 72
How to set focus on a dynamic control 18 52
Adding Rows to Grid 4 32
VB.NET Parsing UDP Bytes 15 36
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question