Results 1 to 12 of 12

Thread: Selecting items in a grid using checkboxes and getting a list of items selected...

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Selecting items in a grid using checkboxes and getting a list of items selected...

    I have a dataset with the following fields in a table.

    DocketNumber, Status, Description

    I also bring back contractor info, but that irrelevant for this as it's only for display purposes

    These are displayed in a datagrid.

    I also have a 3rd column which is a checkbox

    What I want to do is select x many items from the grid by ticking the checkbox, and then get an array of DocketNumers that have been selected.

    Is this possible?

    Attached is the exact screen shot of my grid.

    Woka
    Attached Images Attached Images  

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Selecting items in a grid using checkboxes and getting a list of items selected..

    I would have thought the best way to do it is set the DocketNumber as a DataKey on the DataGrid so it is easily accessible.

    Then you need a method that loops through each item in the DataGrid i.e. DataGrid.Items.

    For each item you could load the checkbox into a local variable (where dgrdName is the ID of the DataGrid and checkboxID is the CheckBox ID):
    Code:
    	CheckBox chkBox = (CheckBox) dgrdName.Items[itemNum].FindControl("checkboxID");
    You can then use this local variable to test if it is checked or not and then add the Docket Number into an ArrayList if needed:
    Code:
    	ArrayList DocketNumbers = new ArrayList();
    
    	if (chkBox.Checked) {
    		DocketNumbers.Add(dgrdName.DataKeys[itemNum]);
    	}
    Note: itemNum is the item index as iterated by a for loop (to loop through all the datagrid items).

    Hope that makes sense.

    DJ

  3. #3

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Selecting items in a grid using checkboxes and getting a list of items selected...

    Fantastic

    Cheers.

    However, when I click a button, and try to loop the datagrid seems to have 0 DataGridItems

    I thought the ViewState was supposed to reconstruct the grid...am I missing something?

    Woka

  4. #4
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Selecting items in a grid using checkboxes and getting a list of items selected..

    It could be a databinding problem i.e. when the page posts back it is rebinding the data every time and you are trying to access the items when it has been cleared out. You really only want to call DataGrid.DataBind() when it isn't a postback or after you have updated some information and you want it to be show on screen.

    Check that first let me know if it is still returning 0.

    DJ

  5. #5

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Selecting items in a grid using checkboxes and getting a list of items selected...

    Hmmm...I did have SetupGrid in the Page_load event.
    Code:
    	Private Sub SetGridProperties()
    		With grdDockets
    			.AutoGenerateColumns = False
    			.AllowPaging = False
    			.AllowSorting = False
    			.PagerStyle.Mode = PagerMode.NextPrev
    			.PagerStyle.NextPageText = "Next"
    			.PagerStyle.PrevPageText = "Prev"
    			.PageSize = 10
    		End With
    	End Sub
    However, I have now moved this into my BindDockets function that does only get called once.
    So, when I have a postback, no grid props are being changed etc. However, it still says I have 0 rows
    Ahhhh...not sure if this is a major issue, but my grid is in a user web control...this shouldn't make a difference should it? I bet it does

    Woka

  6. #6
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Selecting items in a grid using checkboxes and getting a list of items selected..

    Can you chuck up a bit more code - attach a file or something?

    DJ

  7. #7

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Selecting items in a grid using checkboxes and getting a list of items selected...

    OK. My usercontrol code is:
    Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    	End Sub
    
    	Public Sub BindDockets(ByRef Dockets As GateKeeperElevator.Dockets, ByVal AllowSelect As Boolean)
    		SetGridProperties()
    		CreateColumns(AllowSelect)
    		grdDockets.DataSource = Dockets
    		grdDockets.DataBind()
    	End Sub
    
    	Private Sub CreateColumns(ByVal AllowSelect As Boolean)
    		Dim tc As TemplateColumn
    
    		grdDockets.Columns.Clear()
    		tc = New TemplateColumn
    		tc.HeaderTemplate = New DocketTemplate(ListItemType.Header, "Docket")
    		tc.ItemTemplate = New DocketTemplate(ListItemType.Item, "DocketNo")
    		grdDockets.Columns.Add(tc)
    		tc = New TemplateColumn
    		tc.HeaderTemplate = New ContractorTemplate(ListItemType.Header, "Contractor")
    		tc.ItemTemplate = New ContractorTemplate(ListItemType.Item, "Contractor")
    		grdDockets.Columns.Add(tc)
    		If AllowSelect Then
    			tc = New TemplateColumn
    			tc.HeaderTemplate = New SelectTemplate(ListItemType.Header, "")
    			tc.ItemTemplate = New SelectTemplate(ListItemType.Item, "")
    			grdDockets.Columns.Add(tc)
    		End If
    	End Sub
    
    	Private Sub SetGridProperties()
    		With grdDockets
    			.AutoGenerateColumns = False
    			.AllowPaging = False
    			.AllowSorting = False
    			.PagerStyle.Mode = PagerMode.NextPrev
    			.PagerStyle.NextPageText = "Next"
    			.PagerStyle.PrevPageText = "Prev"
    			.PageSize = 10
    		End With
    	End Sub
    
    	Public Function GetSelectedDocketNos() As String()
    		Dim DataItem As DataGridItem
    		Dim chk As CheckBox
    		Dim Dockets() As String
    		For Each DataItem In grdDockets.Items
    			chk = CType(DataItem.FindControl("chkSelected"), System.Web.UI.WebControls.CheckBox)
    			If chk.Checked Then
    				'change Dockets array
    				Dockets(Dockets.Length - 1) = CType(DataItem.FindControl("lblDocketNo"), System.Web.UI.WebControls.Label).Text
    			End If
    		Next
    		Return Dockets
    	End Function
    End Class
    I use template items to create the funky cell structures you see in the grid.
    Here's the Template to Display the docket.
    Code:
    Public Class DocketTemplate
    	Implements ITemplate
    
    	Private TemplateType As ListItemType
    	Private ColumnHeading As String
    
    	Public Sub New(ByVal Type As ListItemType, ByVal Heading As String)
    		TemplateType = Type
    		ColumnHeading = Heading
    	End Sub
    
    	Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
    		Select Case TemplateType
    			Case ListItemType.Header
    				Dim lbl As New Label
    				lbl.Text = ColumnHeading
    				container.Controls.Add(lbl)
    			Case ListItemType.Item
    				Dim tbl As New Table
    				Dim Row As TableRow
    				Dim Cell As TableCell
    				Dim lbl As Label
    				Row = New TableRow
    				Cell = New TableCell
    				lbl = New Label
    				lbl.ID = "lblDocketNo"
    				lbl.Font.Size = New FontUnit(8)
    				lbl.Font.Bold = True
    				Cell.Controls.Add(lbl)
    				AddHandler lbl.DataBinding, AddressOf DocketNo_DataBinding
    				Row.Cells.Add(Cell)
    				tbl.Rows.Add(Row)
    
    				Row = New TableRow
    				Cell = New TableCell
    				lbl = New Label
    				lbl.Font.Size = New FontUnit(7)
    				lbl.ForeColor = Color.Gray
    				Cell.Controls.Add(lbl)
    				AddHandler lbl.DataBinding, AddressOf Status_DataBinding
    				Row.Cells.Add(Cell)
    				tbl.Rows.Add(Row)
    
    				Row = New TableRow
    				Cell = New TableCell
    				lbl = New Label
    				lbl.Font.Size = New FontUnit(7)
    				lbl.ForeColor = Color.Gray
    				Cell.Controls.Add(lbl)
    				AddHandler lbl.DataBinding, AddressOf Description_DataBinding
    				Row.Cells.Add(Cell)
    				tbl.Rows.Add(Row)
    
    				container.Controls.Add(tbl)
    				Cell = CType(container, TableCell)
    				Cell.VerticalAlign = VerticalAlign.Top
    		End Select
    	End Sub
    
    	Private Sub DocketNo_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
    		Dim lbl As Label
    		lbl = CType(sender, Label)
    		Dim container As DataGridItem
    		container = CType(lbl.NamingContainer, DataGridItem)
    		lbl.Text = CType(DataBinder.Eval(container.DataItem, "DocketNo"), String)
    		'lnk.NavigateUrl = "Docket.aspx?Key=" & CType(DataBinder.Eval(container.DataItem, "DocketNo"), String)
    	End Sub
    
    	Private Sub Description_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
    		Dim lbl As Label
    		lbl = CType(sender, Label)
    		Dim container As DataGridItem
    		container = CType(lbl.NamingContainer, DataGridItem)
    		lbl.Text = CType(DataBinder.Eval(container.DataItem, "Description"), String)
    	End Sub
    
    	Private Sub Status_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
    		Dim lbl As Label
    		lbl = CType(sender, Label)
    		Dim container As DataGridItem
    		container = CType(lbl.NamingContainer, DataGridItem)
    		lbl.Text = (New GateKeeperBR.DocketsBR).GetStatusDescription(CType(DataBinder.Eval(container.DataItem, "Status"), GateKeeperBR.DocketsBR.vbDocketStatusConstants))
    	End Sub
    
    	Private Sub ContractorName_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
    		Dim lbl As Label
    		lbl = CType(sender, Label)
    		Dim container As DataGridItem
    		container = CType(lbl.NamingContainer, DataGridItem)
    		lbl.Text = CType(DataBinder.Eval(container.DataItem, "ContractorName"), String)
    	End Sub
    End Class
    Does that help.
    I know it may be a little evil.

    Any pointers would be most welcome.

    Woof

  8. #8
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Selecting items in a grid using checkboxes and getting a list of items selected..

    I'll have a read in the morning and get back to you.

    DJ

  9. #9

  10. #10
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Selecting items in a grid using checkboxes and getting a list of items selected..

    Just done a quick pass on the code - VB.NET ain't my primary language so its slowing me down a tad.

    First thing in GetSelectedDocketNos() you declare the Dockets Array and then use:
    Code:
    Dockets(Dockets.Length - 1) = CType(DataItem.FindControl("lblDocketNo"), System.Web.UI.WebControls.Label).Text
    Surely as the inital length of the Array is 0 and you are on the first iteration uning 0 - 1 or in other words an array index of -1.

    Does that help at all?

    DJ

  11. #11

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Selecting items in a grid using checkboxes and getting a list of items selected...

    No...that code doesn't get executed. I know there was an err with that code.
    I have create a new project, with a new user control, with a new data grid...etc and this works.

    Apparently its because I am dynamically adding columns
    This sucks

    Woka

  12. #12
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Selecting items in a grid using checkboxes and getting a list of items selected..

    It shouldn't matter that you are dynamically adding columns the only problem is at what point you are adding them in comparison to when you are trying to access the cell contents.

    Could you post the complete code for the page and the user control (including HTML part) as I'm having real trouble linking everything together in my head.

    Cheers

    DJ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width