14 Answers, 1 is accepted
Hello Kannan,
Currently there is not built-in functionality that would allow you to do such selection but we do have plans to allow this in the future. We would really appreciate it if you can give us more details about your scenario and the way you would like to utilize multiple column selection. Would you use is to select all cells that appear in the respective columns? Or you have some other functionality in mind?
All the best,
Milan
the Telerik team
Hi Milan,
I need this for functionalities like,
* Selecting multiple columns (like excel) and deleting those columns from grid or
* Selecting (similar) multiple columns and displaying a context menu
Thanks & Regards,
Kannan
I need to select multiple columns by clicking the column header (by pressing Ctrl/Shift key).
It will be better if those columns are highlighted.
Those selected columns can be accessed by a property like grid.SelectedColumns (like grid.SelectedItems for selected rows)
(Then, by right clicking, I need to display a common context menu and the column selection should be still there)
Thanks & Regards,
Kannan
Hi Kannan,
I am afraid that this scenario is not supported at the moment. I could prepare an example with similar functionality but it would use workarounds which will definitely be hard to maintain and I would not like you to go on that path.
Have you seen our Header Context Menu demo where we show you columns can be shown, hidden, sorted using a context menu.
Best wishes,
Milan
the Telerik team
It sounds like what you need is a new SelectionUnit = GridViewSelectionUnit.FullColumn.
Then, you could set the SelectionMode to SelectionMode.Extended or Multiple as desired.
Then, clicks on cells (instead of the header) would select a column and click and drag would select multiple columns (if Extended or Multiple).
Of course, a new Column.IsSelected and RadGridView.SelectedColumns list would be needed to support this.
But, I am not sure is you just want to select columns or if you need to also select rows or single cells?
If you really want only FullColumn selection and you are not willing to wait for Telerik to implement it, I suggest that you emulate it by setting the SelectionUnit to Cell and handling the proper event (SelectionChanging?) to select all of the column's cells when any one of them is selected. To select a cell, use:
GridViewCellInfo ci =
new
GridViewCellInfo(cell);
if
(!grid.SelectedCells.Contains(ci))
grid.SelectedCells.Add(ci);
Then, you would need to work with the SelectedCells list. Header clicks would not be needed.
Handling the header click would requirie more code because you would still need to handle the cell clicks to keep single cell selection from occuring.
Let me know if you plan on persuing this. I have some selection code that can save you a lot of time.
Dean
I am very interested in your code to select a column if you're willing to share it. Any way that you can upload it somewhere?
Thanks
What I have is some general selection code. I have implemented it as extensions to RadGridView.
Attached is a set of general extensions in the class: GridViewExtensions that I put in the namespace:
Telerik.Windows.Controls.GridView.
They add a bunch of methods to the RadGridView.
You would need to write the click capture code etc. to call MyRadGridView.SelectColumn(MyGridViewColumn,select).
Perhaps by getting the column from cell.Column (clicked)?
It would depend on how you want the user interface to work.
Most of the code was collected from this site. I wrote some. There may be bugs, something may not work etc.... there is no support or warranty. :-)
using
System;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Ink;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
Telerik.Windows.Data;
namespace
Telerik.Windows.Controls.GridView
{
/// <summary>
/// A set of extension methods added to RadGridView.
/// Collected from various sources
/// </summary>
public
static
class
GridViewExtensions
{
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#region Column Name
public
static
GridViewColumn GetColumnByName(
this
RadGridView grid,
string
columnName)
{
return
grid.Columns[columnName];
}
public
static
GridViewCell GetCellByName(
this
RadGridView grid,
int
rowIndex,
string
columnName)
{
GridViewColumn column = grid.Columns[columnName];
int
columnIndex = grid.Columns.IndexOf(column);
GridViewRow row = grid.GetRowByIndex(rowIndex);
return
row.Cells[columnIndex]
as
GridViewCell;
}
public
static
GridViewCell GetCellByName(
this
RadGridView grid, GridViewRow row,
string
columnName)
{
GridViewColumn column = grid.Columns[columnName];
int
columnIndex = grid.Columns.IndexOf(column);
return
row.Cells[columnIndex]
as
GridViewCell;
}
#endregion Column Name
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#region Content
public
static
GridViewRow GetRowByContent(
this
RadGridView grid,
object
item)
{
return
grid.ItemContainerGenerator.ContainerFromItem(item)
as
GridViewRow;
}
public
static
object
GetRowContent(
this
RadGridView grid, GridViewRow row)
{
return
row.Item;
}
// If needed for large grids, this should be re-written to get rid of the call to ChildrenOfType because it can be very slow
public
static
GridViewCell GetCellByContent(
this
RadGridView grid,
object
cellValue)
{
return
(from cell
in
grid.ChildrenOfType<GridViewCell>()
where cell.Value == cellValue
select cell).FirstOrDefault();
}
// If needed for large grids, this should be re-written to get rid of the call to ChildrenOfType because it can be very slow
public
static
GridViewCell GetCellByContent(
this
RadGridView grid,
string
cellString)
{
return
(from cell
in
grid.ChildrenOfType<GridViewCell>()
where cell.Value.ToString() == cellString
select cell).FirstOrDefault();
}
#endregion Content
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#region Index
public
static
GridViewRow GetRowByIndex(
this
RadGridView grid,
int
rowIndex)
{
return
grid.GetRowByContent(grid.Items[rowIndex]);
}
public
static
int
GetIndexByRow(
this
RadGridView grid, GridViewRow row)
{
return
grid.Items.IndexOf(row.Item);
}
public
static
GridViewColumn GetColumnByIndex(
this
RadGridView grid,
int
columnIndex)
{
return
grid.Columns[columnIndex];
}
public
static
int
GetIndexByColumn(
this
RadGridView grid, GridViewColumn column)
{
return
grid.Columns.IndexOf(column);
}
// If needed for large grids, this should be re-written to get rid of the call to ChildrenOfType because it can be very slow
public
static
GridViewCell GetCellByIndexes(
this
RadGridView grid,
int
rowIndex,
int
columnIndex)
{
return
(from cell
in
grid.ChildrenOfType<GridViewCell>()
where grid.Columns.IndexOf(cell.Column) == columnIndex
select cell).Skip(rowIndex).FirstOrDefault();
}
public
static
void
GetIndexesByCell(
this
RadGridView grid, GridViewCell cell,
out
int
rowIndex,
out
int
columnIndex)
{
rowIndex = -1;
columnIndex = cell.Column.DisplayIndex;
GridViewRow row = cell.ParentOfType<GridViewRow>();
if
(row !=
null
)
rowIndex = grid.Items.IndexOf(row.Item);
}
#endregion Index
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#region Selection
public
static
void
SelectAll(
this
RadGridView grid,
bool
select)
{
if
(select)
grid.SelectAll();
else
{
if
(grid.SelectedItems !=
null
)
grid.SelectedItems.Clear();
if
(grid.SelectedCells !=
null
)
grid.SelectedCells.Clear();
grid.SelectedItem =
null
;
}
}
public
static
void
SelectRow(
this
RadGridView grid, GridViewRow row,
bool
select)
{
row.IsSelected = select;
}
public
static
void
SelectRow(
this
RadGridView grid,
int
rowIndex,
bool
select)
{
if
(rowIndex >= 0)
{
GridViewRow row = grid.GetRowByIndex(rowIndex);
if
(row !=
null
)
row.IsSelected = select;
}
}
public
static
void
SelectColumn(
this
RadGridView grid, GridViewColumn column,
bool
select)
{
if
(grid.SelectionUnit == GridViewSelectionUnit.Cell)
{
int
columnIndex = grid.GetIndexByColumn(column);
grid.SelectColumn(columnIndex, select);
}
}
public
static
void
SelectColumn(
this
RadGridView grid,
int
columnIndex,
bool
select)
{
if
(grid.SelectionUnit == GridViewSelectionUnit.Cell)
{
if
(columnIndex >= 0)
{
int
rowIndex = 0;
while
(rowIndex < grid.Items.Count)
{
grid.SelectCell(rowIndex, columnIndex, select);
rowIndex++;
}
}
}
}
public
static
void
SetCurrentCell(
this
RadGridView grid,
int
rowIndex,
int
columnIndex)
{
if
((rowIndex >= 0) && (columnIndex >= 0))
{
GridViewCell cell = grid.GetCellByIndexes(rowIndex, columnIndex);
grid.CurrentCellInfo =
new
GridViewCellInfo(cell);
}
}
public
static
void
SetCurrentCell(
this
RadGridView grid, GridViewCell cell)
{
grid.CurrentCellInfo =
new
GridViewCellInfo(cell);
}
public
static
void
SelectCell(
this
RadGridView grid, GridViewCell cell,
bool
select)
{
if
(grid.SelectionUnit == GridViewSelectionUnit.Cell)
{
if
(select)
{
GridViewCellInfo ci =
new
GridViewCellInfo(cell);
if
(!grid.SelectedCells.Contains(ci))
grid.SelectedCells.Add(ci);
}
else
{
GridViewCellInfo ci =
new
GridViewCellInfo(cell);
if
(grid.SelectedCells.Contains(ci))
grid.SelectedCells.Remove(ci);
}
}
}
public
static
void
SelectCell(
this
RadGridView grid,
int
rowIndex,
int
columnIndex,
bool
select)
{
if
(grid.SelectionUnit == GridViewSelectionUnit.Cell)
{
GridViewCell cell = grid.GetCellByIndexes(rowIndex, columnIndex);
if
(cell !=
null
)
grid.SelectCell(cell, select);
}
}
public
static
bool
HasSelection(
this
RadGridView grid)
{
return
(
(grid.SelectedItem !=
null
) ||
((grid.SelectedCells !=
null
) && (grid.SelectedCells.Count > 0)) ||
((grid.SelectedItems !=
null
) && (grid.SelectedItems.Count > 0))
);
}
public
static
bool
RowHasSelection(
this
RadGridView grid, GridViewRow row)
{
if
(row.IsSelected)
return
true
;
if
(grid.SelectionUnit == GridViewSelectionUnit.Cell)
{
GridViewCellBase cellInRow =
(from cell
in
row.Cells
where (cell.ParentRow == row.Item) && ((cell
as
GridViewCell).IsSelected)
select cell).FirstOrDefault();
if
(cellInRow !=
null
)
return
true
;
}
return
false
;
}
public
static
bool
ColumnHasSelection(
this
RadGridView grid, GridViewColumn column)
{
if
(grid.SelectionUnit == GridViewSelectionUnit.Cell)
{
GridViewCell cellInColumn =
(from cell
in
grid.ChildrenOfType<GridViewCell>()
where (cell.Column == column) && (cell.IsSelected)
select cell).FirstOrDefault();
if
(cellInColumn !=
null
)
return
true
;
}
return
false
;
}
#endregion Selection
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#region Commands
public
static
void
CopyToClipboard(
this
RadGridView grid)
{
RoutedUICommand command = RadGridViewCommands.Copy
as
RoutedUICommand;
command.Execute(
null
, grid);
}
public
static
void
PasteFromClipboard(
this
RadGridView grid)
{
RoutedUICommand command = RadGridViewCommands.Paste
as
RoutedUICommand;
command.Execute(
null
, grid);
}
public
static
void
DeleteSelected(
this
RadGridView grid)
{
RoutedUICommand command = RadGridViewCommands.Delete
as
RoutedUICommand;
command.Execute(
null
, grid);
}
#endregion Commands
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
}
Indeed, this solution is not reliable when you have items outside the visible area. Generally we do not recommend working with the visual elements as RadGridView is a virtualized control and its elements are reused as they go in and out the visual area. You can check our online documentation explaining how the UI virtualization works.
I can suggest you working with the data items instead. All the items currently presented in RadGridView are available in the RadGridView.Items collection.
Regards,
Didie
Telerik
Are you supporting this feature in the latest build ? or planning to include this in near future ?
Thanks & Regards,
Kannan
I am afraid we still do not have this feature. As to our plans to support it in the near future, there is a feedback item on a very similar feature request. You can find it on our feedback portal and vote for it: Add: column selection.
If it gathers enough interest, i.e. more votes, we will allocate resources for developing the requested feature.
Regards,
Didie
Telerik
Does it mean there is no way I can select a single column(or multiple) in the Silverlight RadGridView with the current build? Thanks!
I am afraid this feature request is still not implemented. You can vote for it in order to gather more interest in adding it.
Regards,
Dimitrina
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
As an example on how to achieve selection of entire column, I would suggest you to check the Column Selection sdk demo. Although GitHub is a very well-known platform we saw a better and easier approach for reviewing our examples by developing our SDK Samples Browser.
Regards,
Dimitrina
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.