Solved

VB.Net2 Change text on individual button in Datagridview

Posted on 2013-05-16
3
1,592 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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 class 3 28
How to limit User Input 2 33
Access/Visual Basic Question 3 42
VB.NET Inline If statement 4 40
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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