DataGrid Demo Using Andy Smith's RowSelectorColumn

This demo illustrates how to use Andy Smith's RowSelectorColumn control to create a column of radio buttons in a DataGrid. Check out Andy Smith's controls (which are free and include source code) by visiting MetaBuilders.com.


Vote for Your Favorite FAQ!

 FAQ IDFAQ Description
181How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency.
161How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset?
115I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem?
83How can I quickly sort a VBScript array?
190How do I display data on a web page using arrays instead of Do...While...MoveNext...???...
106How can I find out if a record already exists in a database? If it doesn't, I want to add it.
118When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or...
30For session variables to work, must the Web visitor have cookies enabled?
14Can I send emails without using CDONTS?
149How can I remove multiple spaces between words in a string? That is, if I have: Hi    there how can I get: Hi there?


Source Code
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<%@ Register TagPrefix="mbrsc" Namespace="MetaBuilders.WebControls" Assembly="MetaBuilders.WebControls.RowSelectorColumn" %>
<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)
    
    
    If rsc.SelectedIndexes.Length = 0 then
      lblVoteResults.Text = "You did not select a FAQ!"
    Else
      Dim selIndex as Integer = rsc.SelectedIndexes(0)
      lblVoteResults.Text = "You voted for item " & selIndex & _
              ", which is the FAQ with FAQID " & _
              dgPopularFAQs.DataKeys(selIndex) & "

" End If End Sub </script> <form runat="server"> <h3>Vote for Your Favorite FAQ!</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 SelectionMode="Single" /> <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>


[Return to the article]