Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

automatically check a checkbox fields in a datagridview windows form application

Posted on 2008-10-15
13
Medium Priority
?
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
13 Comments
 
LVL 5

Expert Comment

by:web4net
ID: 22721838
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
ID: 22722066
should i place this code in the button click event handler
?
0
 
LVL 5

Expert Comment

by:web4net
ID: 22722147
Yes. Or, any other event.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

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

Expert Comment

by:web4net
ID: 22722309
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
ID: 22722314
would you like me to propose another solution?
0
 

Author Comment

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

Expert Comment

by:the_bachelor
ID: 22722393
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
ID: 22722534
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
ID: 22722617
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
ID: 22722660
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 1000 total points
ID: 22722881
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
ID: 31506286
thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

670 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