473,729 Members | 2,235 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with CheckBox in a datagrid

Hi there:

I've read an excellent "how to"-article by Microsoft (no. 306227) - partly
cited cited at the end of this email).

I have implemented the code related to the part "How to Add a CheckBox
Programmaticall y" in my code. I have changed it to use a OLDDB.DBReader to
populate the datagrid and for the databinding. It works perfectly - also
when using the edit-mode in the datagrid.

However, the problem is: in "edit-Mode" in the Datagrid using some
update-coding of mine I cannot read the "value" of the CheckBox which was
added pogrammatically in case the user changed it. I tried using
"e.Item.FindCon trol("CheckBox1 ")" but it won't work, hoping that the ID of
the CheckBox was simply CheckBox1 (see code below) . I have also been trying
to read the value by using e.Item.Cells(wh atever).Control s(whatever) but how
on earth can I now the correct position - that would involve guessing.

************my code snippet******** *************
Private Sub DataGrid1_Updat eCommand(ByVal source As Object, ByVal e As
System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles
DataGrid1.Updat eCommand

Dim ContactId As Integer, ContactPerson, email, phone, remarks As String
Dim tb As TextBox, cb As CheckBox, bolValue As Boolean

ContactId = e.Item.Cells(1) .Text

'****THIS IS THE PART THAT DOES NOT WORK

cb = CType(e.Item.Fi ndControl("Chec kBox1"), CheckBox)
bolValue = cb.Checked, Boolean

'****END OF CODE THAT DOES NOT WORK
tb = CType(e.Item.Ce lls(4).Controls (0), TextBox)
ContactPerson = tb.Text
tb = CType(e.Item.Ce lls(5).Controls (0), TextBox)
email = tb.Text
tb = CType(e.Item.Ce lls(6).Controls (0), TextBox)
phone = tb.Text
tb = CType(e.Item.Ce lls(7).Controls (0), TextBox)
remarks = tb.Text

Dim str1 As String = _
"UPDATE tblContacts " & _
"SET ContactPerson=' " & ContactPerson & "', ContactPerson_e mail='" & email &
"', " & _
"ContactPerson_ phone='" & phone & "', Remarks='" & remarks & "' WHERE
ContactID=" & ContactId

RunSQLString(st r1)
DataGrid1.EditI temIndex = -1
RefreshDataGrid ()
************end of my code snippet******** *******

I still believe that "e.Item.FindCon trol("CheckBox1 ")" is the right way to
go. But how do I know the ID (name) of the CheckBox-control which is added
by your code ? Is it possible to assign the ID somewhere in your code ?

I'd appreciate your help and assistance if you do not mind. Thank-you.

Rgds,
Mikael

*************** *************** *************** **********
How to Add a CheckBox Programmaticall y
The following sample code adds the TemplateColumn and CheckBox controls to
the grid and binds the data programmaticall y. First, the sample code adds a
TemplateColumn control and then adds the check boxes to the Template column.
Finally, the code adds an event handler to bind the CheckBox control to the
database.
Type or paste the following code sample in the Page_Load event. The code
creates a TemplateColumn object and sets its header text.
SqlDataAdapter1 .Fill(DsAuthors 1)

'Create a new TemplateColumn object.
Dim tcol As New TemplateColumn( )
With tcol
.HeaderText = "CheckBox Column"
' Call DynamicItemTemp late to add the child controls to the Template
' Column and bind them to the Data source.
.ItemTemplate = New DynamicItemTemp late()
End With

DataGrid1.Colum ns.Add(tcol)
If Not IsPostBack Then
DataGrid1.DataB ind()
End If

Type or paste the following code after the Public Class WebForm1, End class.
Public Class DynamicItemTemp late
' ITemplate - When implemented by a class, defines the Control object
' to which child controls and templates belong. These child controls
' are in turn defined within an inline template.
Implements ITemplate

Public Overridable Overloads Sub InstantiateIn(B yVal container As
Control) Implements ITemplate.Insta ntiateIn
' InstantiateIn - When implemented by a class, defines the Control
' object to which child controls and templates belong. These child
' controls are, in turn, defined within an inline template.
'
' Create an instance of a CheckBox object.
Dim oCheckBox As CheckBox = New CheckBox()

' When the DataBinding event of the CheckBox fires, call the sub
' BindCheckBox to properly bind.
' AddHandler oCheckBox.DataB inding, AddressOf BindCheckBox
'Add the CheckBox to the controls collection.
container.Contr ols.Add(oCheckB ox)
End Sub
Public Sub BindCheckBox(By Val sender As Object, ByVal e As EventArgs)
'Create a new instance of a CheckBox.
Dim oCheckBox As CheckBox = CType(sender, CheckBox)
Dim container As DataGridItem = CType(oCheckBox .NamingContaine r,
DataGridItem)
'Evaluate the data from the Grid item and set the Checked property
' appropriatly
If container.DataI tem("contract") .GetType.ToStri ng = "System.DBN ull"
Then
oCheckBox.Check ed = False
Else
oCheckBox.Check ed = CBool(container .DataItem("cont ract"))
End If

End Sub
End Class

NOTE: The binding code is commented to make the process easier to
understand.
Save and run the code. Notice that the Template Column and unbound CheckBox
controls were added.
How to Iterate Through the Control to Test the Value of the CheckBox
Uncomment the following line of code in the aforementioned code sample:
'AddHandler oCheckBox.DataB inding, AddressOf BindCheckBox

This line calls the event handler, Public Sub BindCheckBox, when the
DataBinding event of the CheckBox control executes. The BindCheckBox
procedure evaluates the data and properly sets the Checked property of the
current check box. Notice that the procedure is called for every row in the
grid as the DataGrid binds each row.
Nov 18 '05 #1
0 2403

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1569
by: Job Lot | last post by:
I have an Expense Data Entry form which contains a DataGrid showing various expense categories. There are three columns Description, Cash Exp, Credit Exp, where Description column is readonly. Users have to press a checkbox to specify whether they want to use DataGrid to provide break-up or provide total value in textbox. Now when checkbox is unchecked I am setting the Enabled property of DataGrid to False and vice versa. The problem is...
2
1229
by: Mortel | last post by:
Hi, I have a DataGrid on my page. I show CheckBox in every row to select my rows and show one CheckBox in Title of that column to select all. I can use function and parameter id to read data and set my CheckBox for DataSet, for any Arrays, nevermind. But I have problem with get data from CheckBox when user checked/unchecked it. How I can add my event to CheckBoxes or How I can call any method when user change value. Thanks for all....
0
2158
by: Just D | last post by:
Hi All, Does anybody know how to write the following code? I have a database table with a few columns including: ID , "Task" , ForceRun . I need to show the DataGrid on the ASPX page with these selected fields and allow user to click one or more CheckBox controls to activate this bit on the database table for a required ID. I have already added a template column to the DataGrid and I can get the
1
2522
by: Joe Bloggs | last post by:
I am trying display the contents of a table in a web page, select certain rows from that table and then display the fields that I have selected (now table columns) as text in a Label object. Amazingly I have managed to display the table - no problem, I can select rows - no problem, BUT I can't work out how to display more than one of the fields (now columns in the table in the web page). I can display one field OK, but not more than one...
8
1955
by: Inigo Jimenez | last post by:
I have an ASP .net web application installed in a Windows 2003 server. This web application has a webform that has a Datagrid. This Datagrid is filled with the data of a SQL table. I have a button that inserts a new row in the SQL table and then refresh the datagrid.
2
3331
by: buran | last post by:
Dear ASP.NET Programmers, I have the following problem. I have a datagrid (ID: grdAllActions). This datagrid has two template columns: one column with the dropdownlist control (ID: ddlPS) and another with a checkbox control (ID: cbPS). My goal is to enable or disable the dropdownlist control when the user checks or unchecks the checkbox. I am trying the following code: <asp:TemplateColumn HeaderText="Payment Status"> <ItemTemplate>
7
2277
by: Lars Netzel | last post by:
If I put a checkbox in a datagrid (ASP.NET) and set the Autopostback to true I can catch OnChange event on checkbox but how do I then catch what DataGridItemIndex is? Can I use some Event in the Datagrid that will fire off when the Checkbox is changed? regards /Lars
30
4597
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a corresponding field in the lookup table. In my data table we store the ID in what I will call the 'key' field. == Description of the desired operation:
3
3150
by: Fao, Sean | last post by:
I have a DataGrid that I'm adding CheckBox controls to at runtime (in the code behind) and I'm not sure if I'm doing it correctly. First of all, I noticed that the MyDataGrid.Columns.Add() method expects a DataGridColumn so I instantiated an object of type TemplateColumn that I had hoped I could add a CheckBox to. I soon discovered that the ItemTemplate property of the TemplateColumn class returned an object that had implemented the...
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9280
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.