How to maintain datagridview checkbox state VB.NET

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 721 Views - Last Post: 26 October 2017 - 09:45 AM Rate Topic: -----

#1 arn3342  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 31-July 17

How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 07:59 AM

i have a dgvw in my winform.the dgvw has a checkbox column.There's a textbox in the form to search/filter the dgvw.But the problem is, suppose i checked a few checkboxes then type something(search) in the textbox , the previously checked checkboxes become unchekced . ANy way to maintain their check state?

The textbox code is :
 Private Sub searcgcon_TextChanged(sender As Object, e As EventArgs) Handles searcgcon.TextChanged
        If searcgcon.Text = "" Then
            filterdata("")
            entrylabel.Text = "There are/is " & userdatagrid.Rows.Count & " contact entries"
        Else
            filterdata(searcgcon.Text)
            entrylabel.Text = "There are/is " & userdatagrid.Rows.Count & " contact entries that contain your query :" & searcgcon.Text
        End If
    End Sub

  Private Sub filterdata(valuetosearch As String)
        Dim srq As String = "Select * from Contacts where CONCAT([Unique id],Prefix,[First name],[Last name],Gender,Title,Company,Phone,Mobile,Fax,[b.email],[p.email],Reference,Address,[Address 2],Country,City,Zip,Facebook,GooglePlus,Instagram,Twitter,Website,Salary,Currency,[Group],[Id/Status],Note,[Added by]) like '%" & valuetosearch & "%' "
        Dim mycmd As New SqlCommand(srq, con)
        Dim adapter As New SqlDataAdapter(mycmd)
        Dim table As New DataTable()
        adapter.Fill(table)
        userdatagrid.DataSource = table
    End Sub



So, where am i doing wrong ?

This post has been edited by arn3342: 26 October 2017 - 08:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to maintain datagridview checkbox state VB.NET

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13488
  • View blog
  • Posts: 53,862
  • Joined: 12-June 08

Re: How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 08:02 AM

Ah.. that shouldn't happen unless you are reloading data into the checkbox grid.
Was This Post Helpful? 0
  • +
  • -

#3 arn3342  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 31-July 17

Re: How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 08:09 AM

View Postmodi123_1, on 26 October 2017 - 08:02 AM, said:

Ah.. that shouldn't happen unless you are reloading data into the checkbox grid.

Yeah, well, this is how the code of the textbox looks like

 Private Sub searcgcon_TextChanged(sender As Object, e As EventArgs) Handles searcgcon.TextChanged
        If searcgcon.Text = "" Then
            filterdata("")
            entrylabel.Text = "There are/is " & userdatagrid.Rows.Count & " contact entries"
        Else
            filterdata(searcgcon.Text)
            entrylabel.Text = "There are/is " & userdatagrid.Rows.Count & " contact entries that contain your query :" & searcgcon.Text
        End If
    End Sub

  Private Sub filterdata(valuetosearch As String)
        Dim srq As String = "Select * from Contacts where CONCAT([Unique id],Prefix,[First name],[Last name],Gender,Title,Company,Phone,Mobile,Fax,[b.email],[p.email],Reference,Address,[Address 2],Country,City,Zip,Facebook,GooglePlus,Instagram,Twitter,Website,Salary,Currency,[Group],[Id/Status],Note,[Added by]) like '%" & valuetosearch & "%' "
        Dim mycmd As New SqlCommand(srq, con)
        Dim adapter As New SqlDataAdapter(mycmd)
        Dim table As New DataTable()
        adapter.Fill(table)
        userdatagrid.DataSource = table
    End Sub



So, where am i doing wrong ?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13488
  • View blog
  • Posts: 53,862
  • Joined: 12-June 08

Re: How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 08:11 AM

Man.. I don't know what's up or down in there. I don't know where these checkboxes are in grids relative to the data that gets refreshed in a grid.. or even if they are the same grid.
Was This Post Helpful? 0
  • +
  • -

#5 arn3342  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 31-July 17

Re: How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 08:13 AM

View Postmodi123_1, on 26 October 2017 - 08:11 AM, said:

Man.. I don't know what's up or down in there. I don't know where these checkboxes are in grids relative to the data that gets refreshed in a grid.. or even if they are the same grid.

The datagridview has a checkbox column...thats's what i meant....is it clear now ?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13488
  • View blog
  • Posts: 53,862
  • Joined: 12-June 08

Re: How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 08:15 AM

If that's the case then you would need to compare the old dataset to the new one and set checkboxes appropriately on the new one before assigning it to the grid.
Was This Post Helpful? 1
  • +
  • -

#7 arn3342  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 31-July 17

Re: How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 08:18 AM

View Postmodi123_1, on 26 October 2017 - 08:15 AM, said:

If that's the case then you would need to compare the old dataset to the new one and set checkboxes appropriately on the new one before assigning it to the grid.

i don't understand , can u explain ?
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6377
  • View blog
  • Posts: 25,768
  • Joined: 12-December 12

Re: How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 08:19 AM

You can filter a datagridview, without reloading the data, by creating a DataView, for example (C#):

DataView dv;
dv = new DataView(ds.Tables[0], "type = 'business' ", "type Desc", DataViewRowState.CurrentRows);
dataGridView1.DataSource = dv;

You should also understand why it isn't working currently. You are going back to the database to get the data, without saving your current changes, so the original data is return (but filtered).
Was This Post Helpful? 1
  • +
  • -

#9 arn3342  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 31-July 17

Re: How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 08:22 AM

View Postandrewsw, on 26 October 2017 - 08:19 AM, said:

You can filter a datagridview, without reloading the data, by creating a DataView, for example (C#):

DataView dv;
dv = new DataView(ds.Tables[0], "type = 'business' ", "type Desc", DataViewRowState.CurrentRows);
dataGridView1.DataSource = dv;

You should also understand why it isn't working currently. You are going back to the database to get the data, without saving your current changes, so the original data is return (but filtered).

So, in my textbo_textchange event, what do i need to change?
Was This Post Helpful? 0
  • +
  • -

#10 arn3342  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 31-July 17

Re: How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 08:29 AM

Dim dv As DataView
dv = New DataView(ds.Tables(0), "type = 'business' ", "type Desc", DataViewRowState.CurrentRows)
dataGridView1.DataSource = dv


if this is the code for VB, then where should i place it ? and what is ds ?

This post has been edited by andrewsw: 26 October 2017 - 08:42 AM
Reason for edit:: removed previous quote(s), use the Reply button

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6377
  • View blog
  • Posts: 25,768
  • Joined: 12-December 12

Re: How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 08:42 AM

There is no need to keep quoting the previous post(s), there is a Reply button further down the page, or a Fast Reply box.

ds would be the DataSet.

"where should i place it" The far more important question is do you understand it?
Was This Post Helpful? 0
  • +
  • -

#12 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,678
  • Joined: 01-February 13

Re: How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 08:44 AM

Have you searched for and studied the DataView Class so you can try getting an understanding of what it is and how to use it? Perhaps read through the links below, the first shows the same example as andrewsw showed you but, it is in VB.Net instead of C# which he posted. It also shows a full example at the bottom of the page so you can try understanding it in full.

DataGridView Sorting/Filtering in VB.NET

Google Search Result For - "use dataview to filter datagridview vb.net"
Was This Post Helpful? 0
  • +
  • -

#13 arn3342  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 31-July 17

Re: How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 08:56 AM

View PostIronRazer, on 26 October 2017 - 08:44 AM, said:

Have you searched for and studied the DataView Class so you can try getting an understanding of what it is and how to use it? Perhaps read through the links below, the first shows the same example as andrewsw showed you but, it is in VB.Net instead of C# which he posted. It also shows a full example at the bottom of the page so you can try understanding it in full.

DataGridView Sorting/Filtering in VB.NET

Google Search Result For - "use dataview to filter datagridview vb.net"

sir, this is my changed code :
        Dim srq As String = "Select * from Contacts where CONCAT([Unique id],Prefix,[First name],[Last name],Gender,Title,Company,Phone,Mobile,Fax,[b.email],[p.email],Reference,Address,[Address 2],Country,City,Zip,Facebook,GooglePlus,Instagram,Twitter,Website,Salary,Currency,[Group],[Id/Status],Note,[Added by]) like '%" & valuetosearch & "%' "
        Dim mycmd As New SqlCommand(srq, con)
        Dim adapter As New SqlDataAdapter(mycmd)
        adapter.Fill(ds, "Contacts")
        userdatagrid.DataSource = ds.Tables(0)



But, if i use this code, then in my form,if i click a button that fires the "filterdata", the datagridview keeps adding datas!
Was This Post Helpful? 0
  • +
  • -

#14 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,678
  • Joined: 01-February 13

Re: How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 09:15 AM

So... you searched and read through all that already? The code you posted is to fill the DataSet and assign it as the DataSource of the DataGridView. You would do that just once when the form loads. We can't seem to see the code you are using to filter the data. However, others here would be better at helping you on this, i don't do much with databases.
Was This Post Helpful? 0
  • +
  • -

#15 arn3342  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 31-July 17

Re: How to maintain datagridview checkbox state VB.NET

Posted 26 October 2017 - 09:29 AM

View PostIronRazer, on 26 October 2017 - 09:15 AM, said:

So... you searched and read through all that already? The code you posted is to fill the DataSet and assign it as the DataSource of the DataGridView. You would do that just once when the form loads. We can't seem to see the code you are using to filter the data. However, others here would be better at helping you on this, i don't do much with databases.

this is code used to filter the data :
 Private Sub filterdata(valuetosearch As String)
        Dim srq As String = "Select * from Contacts where CONCAT([Unique id],Prefix,[First name],[Last name],Gender,Title,Company,Phone,Mobile,Fax,[b.email],[p.email],Reference,Address,[Address 2],Country,City,Zip,Facebook,GooglePlus,Instagram,Twitter,Website,Salary,Currency,[Group],[Id/Status],Note,[Added by]) like '%" & valuetosearch & "%' "
        Dim adapter As New SqlDataAdapter(srq, con)
        adapter.Fill(ds, "Contacts_table")
        userdatagrid.DataSource = ds.Tables(0)
    End Sub
   Private Sub searcgcon_TextChanged(sender As Object, e As EventArgs) Handles searcgcon.TextChanged
        If searcgcon.Text = "" Then
            filterdata("")
            entrylabel.Text = "There are/is " & userdatagrid.Rows.Count & " contact entries"
        Else
            filterdata(searcgcon.Text)
            entrylabel.Text = "There are/is " & userdatagrid.Rows.Count & " contact entries that contain your query :" & searcgcon.Text
        End If
    End Sub


So...as u said, the code i posted is to be used once.That means this code should be in the form's load event . but sir look at my textbox textchange code :
   Private Sub searcgcon_TextChanged(sender As Object, e As EventArgs) Handles searcgcon.TextChanged
        If searcgcon.Text = "" Then
            filterdata("")
            entrylabel.Text = "There are/is " & userdatagrid.Rows.Count & " contact entries"
        Else
            filterdata(searcgcon.Text)
            entrylabel.Text = "There are/is " & userdatagrid.Rows.Count & " contact entries that contain your query :" & searcgcon.Text
        End If
    End Sub


Now, as u suggested, if i put the below codes in form's load event :
    Private Sub filterdata(valuetosearch As String)
        Dim srq As String = "Select * from Contacts where CONCAT([Unique id],Prefix,[First name],[Last name],Gender,Title,Company,Phone,Mobile,Fax,[b.email],[p.email],Reference,Address,[Address 2],Country,City,Zip,Facebook,GooglePlus,Instagram,Twitter,Website,Salary,Currency,[Group],[Id/Status],Note,[Added by]) like '%" & valuetosearch & "%' "
        Dim adapter As New SqlDataAdapter(srq, con)
        adapter.Fill(ds, "Contacts_table")
        userdatagrid.DataSource = ds.Tables(0)
    End Sub


Then i would face a lot of issues.
1. I wouldn't be able to create a new Private Sub called filterdata..
2.If i can't create Private Sub filterdata, how am i supposed to use it in my textbox's textchanged event ?
3.If i am still wrong, please correct me sir.I am just a student!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2