Prasad Godbole

Prasad Godbole

  • NA
  • 153
  • 170.8k

Change button txt according to the gridview checkbox state ?

Jan 9 2013 6:04 AM
Hi All,

We are working on one windows application devloped using vb.net 2.0.
In this application in one form we are displaying datagridview and a button.

The gridview contains 1 checkbox column and other boolean column having name "Accepted".

We want that when form will be displayed the button text should show "Ok" and now if check box is checked from any rows of grid then button text should change according to the value of column "Accepted"

For e.g. if grid is displaying five rows then

if 1st row check box is checked and Accepted value is 1 then button text should display "Create Retrial"
if 1st row check box is checked and Accepted value is 0 then button text should display "Amend Retrial"

if 3rd row check box is checked and Accepted value is 1 then button text should display "Create Retrial"
if 3rd row check box is checked and Accepted value is 0 then button text should display "Amend Retrial"

and so on

Upto this we have added the functionality.

But while checking and unchecking checkbox of any row if checkbox of all rows become unchecked then button should show text as "Ok".

Please advise us how to do this.

Below is our code

Private Sub dgvRetrialPayments_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvRetrialPayments.CellValueChanged
        Dim counter As Integer = 0
        Dim totalUnChecked As Integer = 0

        If TypeOf dgvRetrialPayments.CurrentRow.Cells(e.ColumnIndex) Is DataGridViewCheckBoxCell Then
            Dim isChecked As Boolean = dgvRetrialPayments.CurrentRow.Cells(0).FormattedValue
            If isChecked = True Then
                'MessageBox.Show("Checked")
                If Convert.ToBoolean(dgvRetrialPayments.CurrentRow.Cells("Accepted").Value) = False Then
                    btnOk.Text = "Amend Retrial"
                End If
                If Convert.ToBoolean(dgvRetrialPayments.CurrentRow.Cells("Accepted").Value) = True Then
                    btnOk.Text = "Create Retrial"
                End If
                For counter = 0 To dgvRetrialPayments.Rows.Count - 1
                    If counter <> e.RowIndex Then
                        dgvRetrialPayments.CurrentRow.Cells(0).Value = False
                        'dgvRetrialPayments.Rows(e.RowIndex).Cells(0).Value = False
                    End If
                Next
            Else
                'MessageBox.Show("Un Checked")
                'btnOk.Text = "Ok"
            End If
        End If
    End Sub

    Private Sub dgvRetrialPayments_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvRetrialPayments.CurrentCellDirtyStateChanged
        If dgvRetrialPayments.IsCurrentCellDirty Then
            dgvRetrialPayments.CommitEdit(DataGridViewDataErrorContexts.Commit)
        End If
    End Sub



Thanks
Prasad