Solved

VB.Net2 Change text on individual button in Datagridview

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now