How can I make the first row of a RadgridView selected by default, if there's any?
Thank You.
LM
9 Answers, 1 is accepted
Actually, the first row of RadGridView is selected by default. Still, you can use the code below to programmatically select rows:
this.radGridView1.Rows[0].IsSelected = true; |
Feel free to contact me if you have further questions.
Kind regards,
Nikolay
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
I get following error when I try to access Rows property
'RadGridView' does not contain a definition for 'Rows' and no accessible extension method 'Rows' accepting a first argument of type 'RadGridView' could be found (are you missing a using directive or an assembly reference?)
Hi, Rikam,
RadGridView from the Telerik UI for WinForms suites offers Rows collection which allows you to access the grid rows. Additional information is available in our online documentation: https://docs.telerik.com/devtools/winforms/controls/gridview/rows/rows-vs-childrows
It would be greatly appreciated if you confirm that you are using RadGridView from the Telerik UI for WinForms suite. What is the exact version that you are currently using?
The first row of a RadGridView is certainly selected by default when the databinding is done.
But if you have set some sort descriptor before (on design as an exemple), it look like the selection of the first row is done before sorting. So the "first selected row" move in a random place if the datasource is not sorted the same way as your descriptor before binding.
I don't think it's a good idea to rely on the fact that your datasource order is the same of the grid because you could be sure that one day somebody will change only one of them and the random selected row effect will come back.
What's the position of Telerik on this behavior ?
​
RadGridView's CurrentRow will be set to the row according to the form's CurrencyManager's Position property. So, when you assign a DataSource to the grid, the Form's CurrencyManager will know about this source, and its Position defaults to 0. Then, when you sort the grid, this Position (and the grid's CurrentRow) is not changed, which is desired and expected.
However, you can easily change the CurrentRow, by simply setting this property of the grid to any of its rows. For example in your case you can use the DataBindingComplete event:
void
radGridView1_DataBindingComplete(
object
sender, GridViewBindingCompleteEventArgs e)
{
radGridView1.CurrentRow = radGridView1.Rows[radGridView1.Rows.Count - 1];
}
I hope that you find this information useful. Should you have any other questions, do not hesitate to contact us.
Regards,
Stefan
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.
Hello
How set NULL row by default on DataBinding?
P/S
found only one option
private void MasterTemplate_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
{
if (this.dataSourceChanging)
return;
Hello, Yaroslav,
If you do not want to have a current row, you could set the CurrentRow property to null just after binding the grid:
this.radGridView1.CurrentRow = null;
I hope this helps. If you have other questions please let me know.
Regards,
Nadya
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
No - for my need need default NULL!
After - DataBindingComplete
GridView - set First row
"this is complete nonsense"
Hello, Yaroslav,
If I understand you correctly you don't want the current row to change. In order to prevent the current row from changing you can use the CurrentRowChanging event that fires right before the current row is about to change and cancel it:
this.radGridView1.CurrentRowChanging += this.RadGridView1_CurrentRowChanging;
private void RadGridView1_CurrentRowChanging(object sender, CurrentRowChangingEventArgs e)
{
e.Cancel = true;
}
Let me know if you have other questions.
Regards,
Nadya
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
There is one issue with the above solution, it stops the user from being able to select rows.
// Store a bool to stop the control from selecting the first row in the GridView
private bool stopRowSelect = false;
private void GridViewProcessingSurveys_CurrentRowChanging(object sender, CurrentRowChangingEventArgs e)
{
// The only time I want to stop the selected row from changing is when the Control tries to do it.
if (stopRowSelect)
{
e.Cancel = true;
stopRowSelect = false;
}
}
private async Task UpdateProcessedGridView()
{
// Capture currently selected Rows
List<int> selectedRows = gridViewProcessingSurveys.SelectedRows.Select(x => x.Index).ToList();
// Set stopRowSelect to true to stop first row being selected
stopRowSelect = true;
_well.WellProcessedSurveys = await _wellsCRUD.ReadWellProcesssedSurveysAsync(_well.WellId);
bool firstTime = gridViewProcessingSurveys.Columns.Count > 0 ? false : true;
_wellMWDSurveyProcessedModels = new BindingList<WellMWDSurveyProcessedModel>(_well.WellProcessedSurveys);
// Set DataSource
gridViewProcessingSurveys.DataSource = _wellMWDSurveyProcessedModels;
gridViewProcessingSurveys.ClearSelection();
if (!firstTime)
{
// Select rows that were previously selected
foreach (int row in selectedRows)
gridViewProcessingSurveys.Rows[row].IsSelected = true;
}
}
Hello, Timothy,
Indeed, cancelling the CurrentRowChanging event would affect the rows selection as these operations are connected. However, you can handle the SelectionChanged event, store the selected row, then set the CurrentRow to null and then select programmatically the already stored rows:
Private Sub RadGridView1_SelectionChanged(sender As Object, e As EventArgs)
Dim SelectedRows As New List(Of GridViewRowInfo)
For Each rowToSelect As GridViewRowInfo In Me.RadGridView1.SelectedRows
SelectedRows.Add(rowToSelect)
Next
Me.RadGridView1.CurrentRow = Nothing
For Each row As GridViewRowInfo In SelectedRows
RemoveHandler Me.RadGridView1.SelectionChanged, AddressOf RadGridView1_SelectionChanged
row.IsSelected = True
AddHandler Me.RadGridView1.SelectionChanged, AddressOf RadGridView1_SelectionChanged
Next
End Sub
As a result, you wouldn't have a current row in RadGridView. However, the selection will behave as expected: