6 Replies - 835 Views - Last Post: 22 October 2013 - 04:31 PM Rate Topic: -----

#1 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

How to clear a column in datagridview when radiobutton is unchecked?

Posted 20 October 2013 - 06:05 PM

I have 2 radio buttons used for payment methods, one is for quarterly(4) and another for monthly(10).
When I checked the quarterly radiobutton, the first column would populate 4 rows with date of payment otherwise,
when I checked the monthly radiobutton, the first column would populate 10 rows with date of payments.

However, when I checked the quarterly radiobutton again, only the 4 rows are changed. The last 6 rows aren't cleared.

Private Sub RadioButton5_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton5.CheckedChanged
        Dim quar As Integer
        Dim n As Integer = 0
        Dim col As Integer = 0
        Dim newDate As Date = Date.Now
        If RadioButton5.Checked = True Then
            If ComboBox1.SelectedItem = "Pre-school" Then
                clearRs(rs)
                setCmd("Select * From fees_ps_tbl")
                setRs()
                quar = rs.Fields("total").Value
                quar = quar / 4
                For value As Integer = 0 To 3
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(3).Value = quar
                    col = col + 1
                    newDate = newDate.AddMonths(2.5)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 1" Then
                clearRs(rs)
                setCmd("Select * From fees_grade1-2_tbl")
                setRs()
                quar = rs.Fields("total").Value
                quar = quar / 4
                For value As Integer = 0 To 3
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(3).Value = quar
                    col = col + 1
                    newDate = newDate.AddMonths(2.5)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 2" Then
                clearRs(rs)
                setCmd("Select * From fees_1-2_tbl")
                setRs()
                quar = rs.Fields("total").Value
                quar = quar / 4
                For value As Integer = 0 To 3
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(3).Value = quar
                    col = col + 1
                    newDate = newDate.AddMonths(2.5)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 3" Then
                clearRs(rs)
                setCmd("Select * From fees_grade3-4_tbl")
                setRs()
                quar = rs.Fields("total").Value
                quar = quar / 4
                For value As Integer = 0 To 3
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(3).Value = quar
                    col = col + 1
                    newDate = newDate.AddMonths(2.5)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 4" Then
                clearRs(rs)
                setCmd("Select * From fees_grade3-4_tbl")
                setRs()
                quar = rs.Fields("total").Value
                quar = quar / 4
                For value As Integer = 0 To 3
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(3).Value = quar
                    col = col + 1
                    newDate = newDate.AddMonths(2.5)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 5" Then
                clearRs(rs)
                setCmd("Select * From fees_grade5-6_tbl")
                setRs()
                quar = rs.Fields("total").Value
                quar = quar / 4
                For value As Integer = 0 To 3
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(3).Value = quar
                    col = col + 1
                    newDate = newDate.AddMonths(2.5)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 6" Then
                clearRs(rs)
                setCmd("Select * From fees_grade5-6_tbl")
                setRs()
                quar = rs.Fields("total").Value
                quar = quar / 4
                For value As Integer = 0 To 3
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(3).Value = quar
                    col = col + 1
                    newDate = newDate.AddMonths(2.5)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 7" Then
                clearRs(rs)
                setCmd("Select * From fees_grade7_tbl")
                setRs()
                quar = rs.Fields("total").Value
                quar = quar / 4
                For value As Integer = 0 To 3
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(3).Value = quar
                    col = col + 1
                    newDate = newDate.AddMonths(2.5)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 8" Then
                clearRs(rs)
                setCmd("Select * From fees_grade8_tbl")
                setRs()
                quar = rs.Fields("total").Value
                quar = quar / 4
                For value As Integer = 0 To 3
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(3).Value = quar
                    col = col + 1
                    newDate = newDate.AddMonths(2.5)
                Next
            ElseIf ComboBox1.SelectedItem = "3rd Year" Then
                clearRs(rs)
                setCmd("Select * From fees_3rdyr_tbl")
                setRs()
                quar = rs.Fields("total").Value
                quar = quar / 4
                For value As Integer = 0 To 3
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(3).Value = quar
                    col = col + 1
                    newDate = newDate.AddMonths(2.5)
                Next
            ElseIf ComboBox1.SelectedItem = "4th Year" Then
                clearRs(rs)
                setCmd("Select * From fees_4thyr_tbl")
                setRs()
                quar = rs.Fields("total").Value
                quar = quar / 4
                For value As Integer = 0 To 3
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(3).Value = quar
                    col = col + 1
                    newDate = newDate.AddMonths(2.5)
                Next
            End If
        Else
            For value As Integer = 0 To 3
                DataGridView2.Rows.Item(col).Cells(3).Value = ""
                DataGridView2.Rows.Item(col).Cells(0).Value = ""
                col = col + 1
            Next
        End If
    End Sub
    Private Sub RadioButton6_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton6.CheckedChanged
        Dim monthly As Integer
        Dim n As Integer = 0
        Dim col As Integer = 0
        Dim newDate As Date = Date.Now
        If RadioButton6.Checked = True Then
            If ComboBox1.SelectedItem = "Pre-school" Then
                clearRs(rs)
                setCmd("Select * From fees_ps_tbl")
                setRs()
                monthly = rs.Fields("total").Value
                monthly = monthly / 10
                For value As Integer = 0 To 9
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(2).Value = monthly
                    col = col + 1
                    newDate = newDate.AddMonths(1)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 1" Then
                clearRs(rs)
                setCmd("Select * From fees_grade1-2_tbl")
                setRs()
                monthly = rs.Fields("total").Value
                monthly = monthly / 10
                For value As Integer = 0 To 9
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(2).Value = monthly
                    col = col + 1
                    newDate = newDate.AddMonths(1)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 2" Then
                clearRs(rs)
                setCmd("Select * From fees_1-2_tbl")
                setRs()
                monthly = rs.Fields("total").Value
                monthly = monthly / 10
                For value As Integer = 0 To 9
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(2).Value = monthly
                    col = col + 1
                    newDate = newDate.AddMonths(1)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 3" Then
                clearRs(rs)
                setCmd("Select * From fees_grade3-4_tbl")
                setRs()
                monthly = rs.Fields("total").Value
                monthly = monthly / 10
                For value As Integer = 0 To 9
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(2).Value = monthly
                    col = col + 1
                    newDate = newDate.AddMonths(1)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 4" Then
                clearRs(rs)
                setCmd("Select * From fees_grade3-4_tbl")
                setRs()
                monthly = rs.Fields("total").Value
                monthly = monthly / 10
                For value As Integer = 0 To 9
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(2).Value = monthly
                    col = col + 1
                    newDate = newDate.AddMonths(1)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 5" Then
                clearRs(rs)
                setCmd("Select * From fees_grade5-6_tbl")
                setRs()
                monthly = rs.Fields("total").Value
                monthly = monthly / 10
                For value As Integer = 0 To 9
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(2).Value = monthly
                    col = col + 1
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 6" Then
                clearRs(rs)
                setCmd("Select * From fees_grade5-6_tbl")
                setRs()
                monthly = rs.Fields("total").Value
                monthly = monthly / 10
                For value As Integer = 0 To 9
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(2).Value = monthly
                    col = col + 1
                    newDate = newDate.AddMonths(1)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 7" Then
                clearRs(rs)
                setCmd("Select * From fees_grade7_tbl")
                setRs()
                monthly = rs.Fields("total").Value
                monthly = monthly / 10
                For value As Integer = 0 To 9
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(2).Value = monthly
                    col = col + 1
                    newDate = newDate.AddMonths(1)
                Next
            ElseIf ComboBox1.SelectedItem = "Grade 8" Then
                clearRs(rs)
                setCmd("Select * From fees_grade8_tbl")
                setRs()
                monthly = rs.Fields("total").Value
                monthly = monthly / 10
                For value As Integer = 0 To 9
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(2).Value = monthly
                    col = col + 1
                Next
            ElseIf ComboBox1.SelectedItem = "3rd Year" Then
                clearRs(rs)
                setCmd("Select * From fees_3rdyr_tbl")
                setRs()
                monthly = rs.Fields("total").Value
                monthly = monthly / 10
                For value As Integer = 0 To 9
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(2).Value = monthly
                    col = col + 1
                    newDate = newDate.AddMonths(1)
                Next
            ElseIf ComboBox1.SelectedItem = "4th Year" Then
                clearRs(rs)
                setCmd("Select * From fees_4thyr_tbl")
                setRs()
                monthly = rs.Fields("total").Value
                monthly = monthly / 10
                For value As Integer = 0 To 9
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
                    DataGridView2.Rows.Item(col).Cells(2).Value = monthly
                    col = col + 1
                    newDate = newDate.AddMonths(1)
                Next
            End If
        Else
            For value As Integer = 0 To 9
                DataGridView2.Rows.Item(col).Cells(2).Value = ""
                DataGridView2.Rows.Item(col).Cells(0).Value = ""
                col = col + 1
            Next
        End If
    End Sub



Is This A Good Question/Topic? 1
  • +

Replies To: How to clear a column in datagridview when radiobutton is unchecked?

#2 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,536
  • Joined: 03-December 12

Re: How to clear a column in datagridview when radiobutton is unchecked?

Posted 20 October 2013 - 07:13 PM

The only way that I think you could do it is to remove the rows you do not want with a loop or unbind clear everything and refresh.

have you tried datagridview.refresh?

This post has been edited by astonecipher: 20 October 2013 - 07:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Re: How to clear a column in datagridview when radiobutton is unchecked?

Posted 20 October 2013 - 08:19 PM

View Postastonecipher, on 20 October 2013 - 07:13 PM, said:

The only way that I think you could do it is to remove the rows you do not want with a loop or unbind clear everything and refresh.

have you tried datagridview.refresh?


Datagridview.refresh doesn't work >.<

Only the payment values are being cleared out.. not the dates.. >.<
Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,536
  • Joined: 03-December 12

Re: How to clear a column in datagridview when radiobutton is unchecked?

Posted 20 October 2013 - 08:29 PM

It may not be the best solution but why not just do a loop before you input anything to clear what is there?
Was This Post Helpful? 0
  • +
  • -

#5 ebolisa  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 145
  • Joined: 22-September 09

Re: How to clear a column in datagridview when radiobutton is unchecked?

Posted 22 October 2013 - 04:03 AM

If the DataGridView is not bound to any data source, this code may do the trick:

me.DataGridView2.Rows.Clear()



otherwise

me.DataGridView2.DataSource = Nothing 


This post has been edited by ebolisa: 22 October 2013 - 04:04 AM

Was This Post Helpful? 1
  • +
  • -

#6 jimzcoder  Icon User is online

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 331
  • Joined: 14-November 12

Re: How to clear a column in datagridview when radiobutton is unchecked?

Posted 22 October 2013 - 05:20 AM

by analyzing your code.
the issue is not caused by its datasource.
If RadioButton5.Checked = True Then
007
            If ComboBox1.SelectedItem = "Pre-school" Then
008
                clearRs(rs)
009
                setCmd("Select * From fees_ps_tbl")
010
                setRs()
011
                quar = rs.Fields("total").Value
012
                quar = quar / 4
013             'this is where you add/edit the datagridview cell values but you are not clearing its previously created rows and values
                'you can resolve this issue by simply clearing the datagridview rows first before adding new values
                Datagridview2.Rows.Clear()
                'then we will have to create new rows after we cleared all its previous rows
                
                 For value As Integer = 0 To 3 'this code wont work since you are just editing the old rows
014                 Datagridview2.Rows.Add()   'this is where we add new rows for our next line of codes to store its value
                    DataGridView2.Rows.Item(col).Cells(0).Value = newDate.ToShortDateString
015
                    DataGridView2.Rows.Item(col).Cells(3).Value = quar
016
                    col = col + 1
017
                    newDate = newDate.AddMonths(2.5)
018
                Next



Note: i said by analyzing your code(Posted) i cant see any datasource assigned to your datagridview2 so the above code should work fine.
Hope that helped.
Happy Coding.
Was This Post Helpful? 1
  • +
  • -

#7 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Re: How to clear a column in datagridview when radiobutton is unchecked?

Posted 22 October 2013 - 04:31 PM

Thank you guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1