Solved

automatically check a checkbox fields in a datagridview windows form application

Posted on 2008-10-15
13
473 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
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 

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 250 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

Industry Leaders: 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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

762 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