Check out my check boxes!
In this post I hope to show you how to create a gridview that has an extra column to house checkboxes. This will allow the user to select checkboxes in rows which will then be programatically assessed by the code behind. I will also show you how to implement a checkbox which will be contained in the header that will select all or deselect all the checkboxes in that column.
Lets take the following gridview as an example:
This is the .aspx code:
|
<asp:GridView ID="gvConfirmOrders" runat="server" AutoGenerateColumns="False" DataKeyNames="OrderNumber">
<Columns>
<asp:BoundField DataField="OrderNumber" HeaderText="OrderNumber" ReadOnly="True"
SortExpression="OrderNumber" />
<asp:BoundField DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate" />
<asp:BoundField DataField="ShipMethod" HeaderText="ShipMethod" SortExpression="ShipMethod" />
<asp:BoundField DataField="Weight" HeaderText="Weight" SortExpression="Weight" />
</Columns>
</asp:GridView>
|
This gridview(gv) gvConfirmOrders will show you 4 columns.
The gridview is populated programatically in the vb in the page load as follows:
gvOrders.DataSource = dtOrders
'I'll assume you can make the datatable yourself
gvOrders.DataBind()
You want the user to be able to check the relevant boxes he wants to do something with.
We do this by putting TemplateFields into the gridview. These boyos are customisable columns where you can insert controls that will be shown in each row. In this case we will put in checkbox controls. These will go into ItemTemplate within TemplateField. Item template is the area that will be repeated for each row in the gv. Here is the code we will stick in as the last column in our gv.
|
<asp:TemplateField HeaderText="Print Order Documentation">
<HeaderTemplate>
Check Box Column
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="RowLevelCheckBox" />
</ItemTemplate>
</asp:TemplateField>
So now the gv code should look like this:
|
<asp:GridView ID="gvConfirmOrders" runat="server" AutoGenerateColumns="False" DataKeyNames="OrderNumber">
<Columns>
<asp:BoundField DataField="OrderNumber" HeaderText="OrderNumber" ReadOnly="True"
SortExpression="OrderNumber" />
<asp:BoundField DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate" />
<asp:BoundField DataField="ShipMethod" HeaderText="ShipMethod" SortExpression="ShipMethod" />
<asp:BoundField DataField="Weight" HeaderText="Weight" SortExpression="Weight" />
<asp:TemplateField HeaderText="Print Order Documentation">
<HeaderTemplate>
Check Box Column
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="RowLevelCheckBox" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
|
So that's part 1, you now have a column with a checkbox in it for each row in the gv.
Part 2: How to find out through vb which rows were checked by our user.
We will find this out via the code behind for a button called btnGetChecked
|
Dim intSelected As Integer
Dim orderid As String
Dim arOrdersToPrint() As String
intSelected = 0
'For each row in the gv we want to assess where or not the checkbox was checked
For index As Integer = 0 To gvOrders.Rows.Count - 1
'Here we create a checkbox in memory and make it equal to the check box of the row which the for loop is currently on
Dim cb As CheckBox = CType(gvOrders.Rows(index).FindControl("RowLevelCheckBox"), CheckBox)
'You then see if it was checked
If cb.Checked Then
intSelected = intSelected + 1
orderid = CType(gvOrders.Rows(index).Cells(0).Text, String)
'Make an array to hold the ordernumbers using intSelected as the index point in the array to store the ordernumber
ReDim Preserve arOrdersToPrint(0 To intSelected - 1)'This will add another row to this array
arOrdersToPrint(intSelected - 1) = orderid
Session("arOrders") = arOrdersToPrint
End If
Next
|
OK, so as you can see above, we have cycled through all the rows in the gv and where the checkboxes were ticked we then added the value of the corrosponding OrdeNumber to the array arOrdersToPrint. this array will store what orders were selected by the user. I then made a session variable called arOrders which can be accessed across the website.
Now so, Part 3 Check / Uncheck all checkboxes
This is very useful if there are many rows and the user wants all or nothing to be checked.
--- I'm going to stop here for now and continue this post another day