Solved

VB.Net2 Change text on individual button in Datagridview

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

932 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now