This is a migrated thread and some comments may be shown as answers.

Select First Row

9 Answers 1520 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Xavier Soares
Top achievements
Rank 2
Xavier Soares asked on 12 Jun 2008, 06:32 PM
Hello,

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

Sort by
0
Nikolay
Telerik team
answered on 13 Jun 2008, 04:15 PM
Hi Luis,

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
Rikam
Top achievements
Rank 1
commented on 12 Jan 2022, 07:29 AM | edited

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?)

 
Dess | Tech Support Engineer, Principal
Telerik team
commented on 12 Jan 2022, 07:33 AM

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?

 

0
Marco
Top achievements
Rank 2
Veteran
answered on 04 Dec 2014, 09:59 AM
Sorry for the ressurection of this topic but I need some precision about it.

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 ?

​
0
Stefan
Telerik team
answered on 05 Dec 2014, 02:23 PM
Hello Marco,

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.

 
0
Marco
Top achievements
Rank 2
Veteran
answered on 05 Dec 2014, 02:34 PM
Thanks for this explanation Stefan.
0
Yaroslav
Top achievements
Rank 1
answered on 29 Jan 2021, 08:26 AM

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;

0
Nadya | Tech Support Engineer
Telerik team
answered on 29 Jan 2021, 01:07 PM

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/.

0
Yaroslav
Top achievements
Rank 1
answered on 29 Jan 2021, 04:39 PM

 

No - for my need need default NULL!

0
Yaroslav
Top achievements
Rank 1
answered on 29 Jan 2021, 04:49 PM

After - DataBindingComplete

GridView - set First row

"this is complete nonsense"

0
Nadya | Tech Support Engineer
Telerik team
answered on 03 Feb 2021, 10:03 AM

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/.

Timothy
Top achievements
Rank 1
commented on 06 Jun 2022, 03:15 PM | edited

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;
            }

        }

Dess | Tech Support Engineer, Principal
Telerik team
commented on 07 Jun 2022, 09:01 AM

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:

Tags
GridView
Asked by
Xavier Soares
Top achievements
Rank 2
Answers by
Nikolay
Telerik team
Marco
Top achievements
Rank 2
Veteran
Stefan
Telerik team
Yaroslav
Top achievements
Rank 1
Nadya | Tech Support Engineer
Telerik team
Share this question
or