Link to home
Start Free TrialLog in
Avatar of Jason
JasonFlag for Australia

asked on

Objectlistview how check checkboxes in bound data in listview for each row

I need help with objectlistview.
each row has 3 check boxes they are in columns 6, 7 , 8
If conditions are met I need the system to  check the checkboxes
some instances non will be checked or 1 , 2 or all 3 could be checked depends what condition was met

the boxes are data bound

not very good with this control
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Hi,

Check this sample, it will check the check-boxes based on the values in some column.

<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="itemPlaceHolder1" OnItemDataBound="ListView1_ItemDataBound">
                <LayoutTemplate>
                    <table cellpadding="4" cellspacing="0" class="auto-style1">
                        <tr>
                            <td>Name</td>
                            <td>IsAdmin</td>
                            <td>IsUser</td>
                            <td>IsGuest</td>
                        </tr>
                        <asp:PlaceHolder ID="itemPlaceHolder1" runat="server"></asp:PlaceHolder>
                    </table>
                </LayoutTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:Literal ID="Literal1" runat="server" Text='<%# Eval("Id") %>'></asp:Literal></td>
                        <td>
                            <asp:CheckBox ID="CheckBox1" runat="server" /></td>
                        <td>
                            <asp:CheckBox ID="CheckBox2" runat="server" /></td>
                        <td>
                            <asp:CheckBox ID="CheckBox3" runat="server" /></td>
                    </tr>
                </ItemTemplate>
            </asp:ListView>

Open in new window


Protected Sub Page_Load(sender As Object, e As EventArgs)
	If Not IsPostBack Then
		Dim dt As New DataTable()
		dt.Columns.Add("Id")

		For i As Integer = 0 To 3
			dt.Rows.Add(i)
		Next

		ListView1.DataSource = dt
		ListView1.DataBind()
	End If
End Sub

Open in new window


Protected Sub ListView1_ItemDataBound(sender As Object, e As ListViewItemEventArgs)
	If e.Item.ItemType = ListViewItemType.DataItem Then
		' Display the e-mail address in italics.
		Dim ltrId As Literal = DirectCast(e.Item.FindControl("Literal1"), Literal)

		Dim checkBox1 As CheckBox = DirectCast(e.Item.FindControl("CheckBox1"), CheckBox)
		Dim checkBox2 As CheckBox = DirectCast(e.Item.FindControl("CheckBox2"), CheckBox)
		Dim checkBox3 As CheckBox = DirectCast(e.Item.FindControl("CheckBox3"), CheckBox)

		' By default all the checkboxes will be UNCHECKED
		'based on some conditions check box will be ticked as below
		If ltrId.Text = "1" Then
			checkBox1.Checked = True
		End If

		If ltrId.Text = "2" Then
			checkBox1.Checked = True

			checkBox2.Checked = True
		End If

		If ltrId.Text = "3" Then
			checkBox1.Checked = True
			checkBox2.Checked = True
			checkBox3.Checked = True
		End If
	End If
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jason

ASKER

Hi Saige

Thanks again can work with that

Great

Cheers
Jason
Avatar of Jason

ASKER

On thing RaceData not defines
Avatar of Jason

ASKER

Having troubles tiring in the money gap process
Can you assist.

The process id dependent on the fWinOdds column being sorted before calculations being done

Here is my code
    Sub ProcessMoneyGap()
        For ic As Integer = 0 To Form1.ObjectListView1.Items.Count - 1
            If ic <> Form1.ObjectListView1.Items.Count - 1 Then
                Dim OldVal = Form1.ObjectListView1.Items(ic).SubItems(0).Text
                Dim NewVal = Form1.ObjectListView1.Items(ic + 1).SubItems(0).Text
                Dim Cp = Math.Round(((NewVal - OldVal) / OldVal) + 1, 2, MidpointRounding.AwayFromZero)
                Dim CalcPercent = Math.Round(((NewVal - OldVal) / OldVal * 100), 2, MidpointRounding.AwayFromZero)
                Form1.ObjectListView1.Items(ic).SubItems(6).Text = Cp '& " / " & CalcPercent
            End If
        Next
        Dim count
        For Each sb As OLVListItem In Form1.ObjectListView1.Items
            If sb.SubItems(6).Text <> "" Then
                If sb.SubItems(6).Text >= 1.5 Then
                    If count < 3 Then
                        sb.UseItemStyleForSubItems = False
                        sb.SubItems(6).ForeColor = Color.Red
                        count += 1
                        If count = 1 Then
                            For u = 0 To 10
                                sb.SubItems(u).BackColor = Color.LightGreen
                            Next
                        ElseIf count = 2 Then
                            For u = 0 To 10
                                sb.SubItems(u).BackColor = Color.LightSalmon
                            Next
                        ElseIf count = 3 Then
                            For u = 0 To 10
                                sb.SubItems(u).BackColor = Color.LightSteelBlue
                            Next
                        ElseIf count <> 1 Or count <> 2 Or count <> 3 Then
                            For u = 0 To 10
                                sb.SubItems(u).BackColor = Color.Transparent
                            Next
                        End If
                    ElseIf count <> 1 Or count <> 2 Or count <> 3 Then
                        For u = 0 To 10
                            sb.SubItems(u).BackColor = Color.Transparent
                        Next
                    End If
                Else
                    sb.UseItemStyleForSubItems = False
                    sb.SubItems(6).ForeColor = Color.Black
                End If
            End If
        Next
    End Sub

Open in new window

You need to change the properties of the object that the row is built from, not the fields/cells/columns in the row.

-saige-
Avatar of Jason

ASKER

I dont quiet understand
Could you give me an example
The problem im having is in order for  me to calculate the gap or difference it needs to exist first for me to use.
Ie row (0) money  gap need to know what  the odds are for  that row which is  is easy  but also that next row (1)
Ok, so then for example:
Sub ProcessMoneyGap()
	For ic As Integer = 0 To ObjectListView1.Items.Count - 1
		If ic <> ObjectListView1.Items.Count - 1 Then
			Dim OldVal = CType(CType(ObjectListView1.Items(ic), OLVListItem).RowObject, RaceInfo)
			Dim NewVal = CType(CType(ObjectListView1.Items(ic + 1), OLVListItem).RowObject, RaceInfo)
			Dim Cp = Math.Round(((NewVal.WinOdds - OldVal.WinOdds) / OldVal.WinOdds) + 1, 2, MidpointRounding.AwayFromZero)
			Dim CalcPercent = Math.Round(((NewVal.WinOdds - OldVal.WinOdds) / OldVal.WinOdds * 100), 2, MidpointRounding.AwayFromZero)
			OldVal.MG = Cp '& " / " & CalcPercent
		End If
	Next
	Dim count
	For Each sb As OLVListItem In ObjectListView1.Items
		If sb.SubItems(6).Text <> "" Then
			If sb.SubItems(6).Text >= 1.5 Then
				If count < 3 Then
					sb.UseItemStyleForSubItems = False
					sb.SubItems(6).ForeColor = Color.Red
					count += 1
					If count = 1 Then
						For u = 0 To 10
							sb.SubItems(u).BackColor = Color.LightGreen
						Next
					ElseIf count = 2 Then
						For u = 0 To 10
							sb.SubItems(u).BackColor = Color.LightSalmon
						Next
					ElseIf count = 3 Then
						For u = 0 To 10
							sb.SubItems(u).BackColor = Color.LightSteelBlue
						Next
					ElseIf count <> 1 Or count <> 2 Or count <> 3 Then
						For u = 0 To 10
							sb.SubItems(u).BackColor = Color.Transparent
						Next
					End If
				ElseIf count <> 1 Or count <> 2 Or count <> 3 Then
					For u = 0 To 10
						sb.SubItems(u).BackColor = Color.Transparent
					Next
				End If
			Else
				sb.UseItemStyleForSubItems = False
				sb.SubItems(6).ForeColor = Color.Black
			End If
		End If
	Next
End Sub

Open in new window

-saige-
Avatar of Jason

ASKER

Great thx
I am slowly understanding how to use this control
Avatar of Jason

ASKER

Saige

Having 2 problems

See Pic
Can you explain how I can get sort to work in module.  Works fine if I place it in form1 but not under any other module.

User generated image
secondly
When I run code to alter through the program checkbox ticks are not visible.

would you know why????
User generated image
Because the module does not know about the objects on the form.  Thats why olv1WOdds has the squiggly line underneath it.

Probably because you are not modifying the underlying object that makes up the row.

-saige-
Avatar of Jason

ASKER

solved the first one

any clue on the second one.
when i use the fwinodds tick appear in listview but if i change to fmg then objects in raceinfo indicated true value but the ticks dont appear on the screen.  
what is funny is if i click on the true check box it activates all that were true for that row.

any help would  be greatly appreciated
Avatar of Jason

ASKER

Saige
It appear that ticks dont work in checkboxes under a edit situation you  have to call the entire process