DataGrid Demo of a CheckBox Column Using Andy Smith's RowSelectorColumn
This demo illustrates how to create a column of checkboxes.
Source Code
<script language="vb" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
BindData()
End If
End Sub
Sub BindData()
'1. Create a connection
Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
'2. Create the command object, passing in the SQL string
Const strSQL as String = "sp_Popularity"
Dim myCommand as New SqlCommand(strSQL, myConnection)
'Set the datagrid's datasource to the datareader and databind
myConnection.Open()
dgPopularFAQs.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
dgPopularFAQs.DataBind()
End Sub
Sub RecordVote(sender as Object, e as EventArgs)
Dim rsc as RowSelectorColumn = RowSelectorColumn.FindColumn(dgPopularFAQs)
Dim selIndex as Integer
For Each selIndex in rsc.SelectedIndexes
lblVoteResults.Text &= "You chose item " & selIndex & _
", which is the FAQ with FAQID " & _
dgPopularFAQs.DataKeys(selIndex) & "<br>"
Next
lblVoteResults.Text &= "<p>"
End Sub
</script>
<form runat="server">
<h3>What FAQs Would Your Like to Learn More About?</h3>
<asp:Label runat="server" id="lblVoteResults" ForeColor="Red"
Font-Italic="True" Font-Size="14pt" />
<asp:DataGrid runat="server" id="dgPopularFAQs"
BackColor="#eeeeee" Width="85%"
HorizontalAlign="Center"
Font-Name="Verdana" CellPadding="4"
DataKeyField="FAQID"
Font-Size="10pt" AutoGenerateColumns="False">
<HeaderStyle BackColor="Black" ForeColor="White" Font-Bold="True" HorizontalAlign="Center" />
<AlternatingItemStyle BackColor="White" />
<Columns>
<mbrsc:RowSelectorColumn AllowSelectAll="True" SelectionMode="Multiple" />
<asp:BoundColumn DataField="FAQID" HeaderText="FAQ ID" />
<asp:BoundColumn DataField="Description" HeaderText="FAQ Description" />
</Columns>
</asp:datagrid>
<br />
<asp:Button runat="server" onclick="RecordVote" text="Record Vote" />
</form>