Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

automatically check a checkbox fields in a datagridview windows form application

hello all,

i need a windows form application written in vb 2005 that talks to a sql server 2005 database.

i have a database in my local installation of sql server 2005 called "test". there is one table in it, called "users".

the fields of the table(see attached snippet) are

userid(identity-primary key),username(varchar(50)),isactive(datatype=bit default value=false=0)

some sample data for this table could be

userid    username         isactive
1             sin                      false

2             prn                    false

3            george               false

4           jane                    false

1) i need to connect to this database

2) and bring all the information from this table to a datagridview

3) i have a simple textbox and a button in this form.

what i want to do is this

1) type a number p.x 3 in the textfield

2) then press  the button on the form

3) the row in the datagridview that has userid=3, to check the checkbox and make the isactive checkbox=checked = true

thanks a lot
CREATE TABLE [dbo].[users](
	[userid] [int] IDENTITY(1,1) NOT NULL,
	[username] [varchar](50) NOT NULL,
	[isactive] [bit] NOT NULL,
	[userid] ASC

Open in new window

  • 7
  • 4
  • 2
1 Solution
Let me know if the following code helps you ... if not, I have other solutions.

dim dtTemp as DataTable = New DataTable
Dim colItem As New DataColumn("Items")
colItem.DataType = System.Type.GetType("System.String")
Dim colSelection1 As New DataColumn("Selection1")
colSelection1.DataType = System.Type.GetType("System.Boolean")
Dim colSelection2 As New DataColumn("Selection2")
colSelection2.DataType = System.Type.GetType("System.Boolean")
'That will give you 3 columns and then you can add your data by adding rows
Dim newRow As DataRow
newRow = dtTemp.NewRow
newRow.Item("Item")= "Some Item"
newRow.Item("Selection1")=False   'blank checkbox
newRow.Item("Selection2")=False   'blank checkbox

Open in new window

nikolaoskAuthor Commented:
should i place this code in the button click event handler
Yes. Or, any other event.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

nikolaoskAuthor Commented:
well ,where is the code tha connects with the db?
You have it.
Just be creative to integrate your code with the code above.
You can add the code above to your onload event and combine it with your connection string.
would you like me to propose another solution?
nikolaoskAuthor Commented:
thanks. no, no at the moment. give me a sec to grasp this solution.
here is what I would do
1- Bring the data from the table to the Application as a dataset and bind the dataset to the Gridview on page/form load

2- On the button click
     a. Wrtire a routine to change the IsActive Filed to True where userId = 3
     b. Rebind the data to the Gridview
nikolaoskAuthor Commented:

1) i have created a windows vb application form

2) i have placed a button , a textfield and a datagridview on the form

3) i have used server explorer to successfully connect to my sql server 2005 and connect successfully to my database and table

4) i have used the disconnected model (dataset) and through my connection string i was able to get all the data in my datagridview

that is done

so i have all my data in the gridview and the name of the dataset is "testdataset".

what i want is this.

when i write in the textbox the number "3" , then i want by clicking the button, the record with userid=3 to be selected and the checkbox to be checked=value =true.

the controls are:button1,form1,textfield1,datagridview1 and the name of the dataset is testdataset.

nikolaoskAuthor Commented:
i am trying to be as clear as possible. i do not want to write to the table. just to retrieve tha data. i have done that.

then i want programmatically to check the checkbox if the number in the textbox matches the number  of the userid field
nikolaoskAuthor Commented:
you wrote

"here is what I would do
1- Bring the data from the table to the Application as a dataset and bind the dataset to the Gridview on page/form load

2- On the button click
     a. Wrtire a routine to change the IsActive Filed to True where userId = 3
     b. Rebind the data to the Gridview"

exactly. i need this code. thanks a lot
Assuming you know the rowindex associated with the userid that you wanna modify, here is the code to to check the checkbox. WITHOUT writing to the database to persist the change.

Remeber those Indexes are zero based
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'say you know the row index associated with the user id.
        'Here we are just gonna assume the the rowidex is the same as the value you type in the taxtbox
        Dim rowIndex As Integer = CInt(TextBox1.Text)
        Dim check As DataGridViewCheckBoxCell = TryCast(Me.DataGridView1.Rows(rowIndex).Cells(2), DataGridViewCheckBoxCell)
        If check IsNot Nothing Then
            check.Value = True
        End If
    End Sub

Open in new window

nikolaoskAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now