Solved

automatically check a checkbox fields in a datagridview windows form application

Posted on 2008-10-15
13
474 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

710 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