473,225 Members | 1,361 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

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
Programmatically" 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.FindControl("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(whatever).Controls(whatever) but how
on earth can I now the correct position - that would involve guessing.

************my code snippet*********************
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
DataGrid1.UpdateCommand

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.FindControl("CheckBox1"), CheckBox)
bolValue = cb.Checked, Boolean

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

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

RunSQLString(str1)
DataGrid1.EditItemIndex = -1
RefreshDataGrid()
************end of my code snippet***************

I still believe that "e.Item.FindControl("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 Programmatically
The following sample code adds the TemplateColumn and CheckBox controls to
the grid and binds the data programmatically. 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(DsAuthors1)

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

DataGrid1.Columns.Add(tcol)
If Not IsPostBack Then
DataGrid1.DataBind()
End If

Type or paste the following code after the Public Class WebForm1, End class.
Public Class DynamicItemTemplate
' 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(ByVal container As
Control) Implements ITemplate.InstantiateIn
' 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.DataBinding, AddressOf BindCheckBox
'Add the CheckBox to the controls collection.
container.Controls.Add(oCheckBox)
End Sub
Public Sub BindCheckBox(ByVal 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.NamingContainer,
DataGridItem)
'Evaluate the data from the Grid item and set the Checked property
' appropriatly
If container.DataItem("contract").GetType.ToString = "System.DBNull"
Then
oCheckBox.Checked = False
Else
oCheckBox.Checked = CBool(container.DataItem("contract"))
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.DataBinding, 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 2363

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

Similar topics

0
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. ...
2
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...
0
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...
1
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....
8
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...
2
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...
7
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...
30
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...
3
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...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.