Skip to main content Skip to footer

HowTo: Single Click Row Selection in C1DataGrid

ComponentOne DataGrid for Silverlight offers great deal of flexibility in the way its rows/ cells are selected. Please refer to the documentation for a list of the SelectionModes offered by C1DataGrid. MultiRow SelectionMode allows users to select multiple rows using the mouse and the Ctrl/ Shift keys. However, in this blog we are going to discuss how to Select/ Deselect Multiple Rows on Single Mouse Click without pressing any key. We’re going to look at how to do that through this blog. First of all, let’s set up the C1DataGrid along with setting its SelectionMode property to MultiRow.


ObservableCollection<PersonName> people = new ObservableCollection<PersonName>();  
public MainPage()  
{  
    InitializeComponent();  
    people.Add(new PersonName("Johnathan", "Gartner"));  
    people.Add(new PersonName("Jeannine", "Richart"));  
    people.Add(new PersonName("Merry", "Sparacino"));  
    people.Add(new PersonName("Sandi", "Willman"));  
    people.Add(new PersonName("Damion", "Dagley"));  
    people.Add(new PersonName("Johnathan", "Gartner"));  
    people.Add(new PersonName("Jeannine", "Richart"));  
    people.Add(new PersonName("Merry", "Sparacino"));  
    people.Add(new PersonName("Sandi", "Willman"));  
    people.Add(new PersonName("Damion", "Dagley"));  
    people.Add(new PersonName("Johnathan", "Gartner"));  
    people.Add(new PersonName("Jeannine", "Richart"));  
    people.Add(new PersonName("Merry", "Sparacino"));  
    people.Add(new PersonName("Sandi", "Willman"));  
    people.Add(new PersonName("Damion", "Dagley"));  
    DataGrid.ItemsSource = people;  
    DataGrid.SelectionMode = C1.Silverlight.DataGrid.DataGridSelectionMode.MultiRow;  
}  

Then in order to select multiple rows on a single click, we need to handle the MouseLeftButtonUp event of the RowPresenter in the LoadedRowPresenter event.


void DataGrid_LoadedRowPresenter(object sender, C1.Silverlight.DataGrid.DataGridRowEventArgs e)  
{  
    e.Row.Presenter.MouseLeftButtonUp += Presenter_MouseLeftButtonUp;  
}  
void Presenter_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)  
{  
    int rowIndex = ((C1.Silverlight.DataGrid.DataGridRowPresenter)sender).Row.Index;  
    if (!selectedRows.Contains(rowIndex))  
    {  
        selectedRows.Add(rowIndex);  
    }  
    else  
    {  
        selectedRows.Remove(rowIndex);  
    }  
}  
void DataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)  
{  
    e.Handled = true;  
    this.DataGrid.Selection.Clear();  
    for (int i = 0; i <= selectedRows.Count - 1; i++)  
    {  
        DataGrid.Rows[0].CanUserResize = true;  
        this.DataGrid.Selection.Add(new C1.Silverlight.DataGrid.DataGridCellsRange(this.DataGrid.Rows[selectedRows[i]]));  
    }  
    SelectedRowCountBlock.Text = "Selected RowCount : " + this.DataGrid.Selection.SelectedRows.Count;  
}  

Complete sample with the above implementation may be downloaded from these links:- DownloadSample_CS DownloadSample_VB

MESCIUS inc.

comments powered by Disqus