|
RowSelector Column
Many of us with struggled with getting our Datagrid rows to be select-able with radio buttons or checkboxes,
and determining which row(s) were selected after PostBack. Solve this problem by using
Andy Smith's RowSelectorColumn (source below),
which you can use in your current Datagrids to solve this problem.
See this column in action!
Once you've downloaded the DLL for the RowSelectorColumn, here are the steps you'll need to follow:
1) Place the DLL (MetaBuilders.WebControls.RowSelectorColumn.dll) in your application's /bin directory.
2) Include the following directive at the top of your ASPX page to register the control:
<%@ Register TagPrefix="mbrsc" Namespace="MetaBuilders.WebControls" Assembly="MetaBuilders.WebControls.RowSelectorColumn" %>
3) Add this code to your <Columns> listing in your <asp:Datagrid>:
<mbrsc:RowSelectorColumn SelectionMode="Multiple" AllowSelectAll="false"/>
The SelectionMode option, which can be set to "Single" for single selection of rows (renders radio buttons),
or "Multiple" if multiple rows can be selected (renders checkboxes).
The AllowSelectAll option only applies when SelectionMode is set to "Multiple".
When set to true, AllowSelectAll will create a "Select All" checkbox at the top of your
column of checkboxes, and will automagically create the JavaScript necessary to select all the rows
when this option is checked.
4) To get a reference to your RowSelectorColumn after PostBack (C#):
RowSelectorColumn rsc = RowSelectorColumn.FindColumn( NameOfYourGridHere );
VB.Net version:
Dim rsc As RowSelectorColumn = RowSelectorColumn.FindColumn( NameOfYourGridHere )
5) We're almost done! To refer to the selected item or items ("Message" refers to an <asp:Label> control) (C#):
Message.Text = "Total selected rows = " + rsc.SelectedIndexes.Length.ToString() + "<br>";
foreach( Int32 selectedIndex in rsc.SelectedIndexes ) {
Message.Text += selectedIndex.ToString() + "<br>";
}
VB.Net version:
Dim selectedIndex As Integer
Message.Text = "Total selected rows = " + rsc.SelectedIndexes.Length.ToString() & "<br>"
For Each selectedIndex in rsc.SelectedIndexes
Message.Text = Message.Text & selectedIndex.ToString() & "<br>"
Next
That's it! You should find that this solution makes row selection in a Datagrid much simpler to handle.
To discuss this custom column, or any Datagrid topic for that matter, sign up for the
Datagrid Mailing List
at ASPAdvice.com.
Download the complete zip file including custom column source,
VS.Net project files, and a full sample ASPX page to try out the column.
|