Solved

automatically check a checkbox fields in a datagridview windows form application

Posted on 2008-10-15
13
469 Views
Last Modified: 2013-11-07
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,

 CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED 

(

	[userid] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]

Open in new window

0
Comment
Question by:nikolaosk
  • 7
  • 4
  • 2
13 Comments
 
LVL 5

Expert Comment

by:web4net
Comment Utility
Greetings,
 
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")

dtTemp.Columns.Add(colItem)

 

Dim colSelection1 As New DataColumn("Selection1")

colSelection1.DataType = System.Type.GetType("System.Boolean")

dtTemp.Columns.Add(colSelection1)

 

Dim colSelection2 As New DataColumn("Selection2")

colSelection2.DataType = System.Type.GetType("System.Boolean")

dtTemp.Columns.Add(colSelection2)

 

'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

dtTemp.Rows.Add(newRow)

Open in new window

0
 

Author Comment

by:nikolaosk
Comment Utility
should i place this code in the button click event handler
?
0
 
LVL 5

Expert Comment

by:web4net
Comment Utility
Yes. Or, any other event.
0
 

Author Comment

by:nikolaosk
Comment Utility
well ,where is the code tha connects with the db?
0
 
LVL 5

Expert Comment

by:web4net
Comment Utility
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.
0
 
LVL 5

Expert Comment

by:web4net
Comment Utility
would you like me to propose another solution?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:nikolaosk
Comment Utility
thanks. no, no at the moment. give me a sec to grasp this solution.
0
 
LVL 7

Expert Comment

by:the_bachelor
Comment Utility
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
0
 

Author Comment

by:nikolaosk
Comment Utility
well

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.


0
 

Author Comment

by:nikolaosk
Comment Utility
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
0
 

Author Comment

by:nikolaosk
Comment Utility
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
0
 
LVL 7

Accepted Solution

by:
the_bachelor earned 250 total points
Comment Utility
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

0
 

Author Closing Comment

by:nikolaosk
Comment Utility
thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
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.

772 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

10 Experts available now in Live!

Get 1:1 Help Now