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

Binding RadGridView to DataTable and Adding Items to DataTable

4 Answers 579 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ralph
Top achievements
Rank 1
Ralph asked on 31 May 2013, 03:36 PM
I am doing a new UserControl in studio.
I then add the Telerik RadGridView to said control and change the Dock property to FILL.

When the grid loads I do

if (dataSet == null) //TODO TESTING
{
    dataSet = new DataSet();
    dataTable = dataSet.Tables.Add("Results");
}
 
dataTable.Columns.Add("ID");
dataTable.Columns.Add("Name");
 
dataTable.Rows.Add(i++, "Jack");
dataTable.Rows.Add(i++, "Donna");
dataTable.Rows.Add(i++, "Jack");
dataTable.Rows.Add(i++, "Donna");
dataTable.Rows.Add(i++, "Jack");
dataTable.Rows.Add(i++, "Donna");
dataTable.Rows.Add(i++, "Jack");
dataTable.Rows.Add(i++, "Donna");
dataTable.Rows.Add(i++, "Jack");
dataTable.Rows.Add(i++, "Donna");
dataTable.Rows.Add(i++, "Jack");
dataTable.Rows.Add(i++, "Donna");

(dataSet is a DataSet that is a member of the usercontrol so is around for the life time of the control.

I then bind the data table to the control 

this.batchDisplay.DataSource = dataSet.Tables[0];

Now I have a button on my app that simply adds data to the datatable

dataTable.Rows.Add(i++, "Ralph"); //TODO TEST CODE
dataTable.Rows.Add(i++, "Walf");
dataTable.Rows.Add(i++, "Ralph");
dataTable.Rows.Add(i++, "Walf");


My problem is I can't figure out how to get the scroll bar to do what I want.

What I want is for the scroll bar to stay where it is in the collection while items are added to the bottom.

If I just straight up add the items to the dataTable my scroll bar jumps to the bottom.

I have tried doing:

this.batchDisplay.BeginUpdate();
 
 dataTable.Rows.Add(i++, "Ralph"); //TODO TEST CODE
 dataTable.Rows.Add(i++, "Walf");
 dataTable.Rows.Add(i++, "Ralph");
 dataTable.Rows.Add(i++, "Walf");
 
 this.batchDisplay.EndUpdate();
 this.batchDisplay.TableElement.RowScroller.UpdateScrollRange();


But this causes the scroll bar to do some kind of odd warp where the bar grows and shrinks depending on where you are in the scroll.

The only way I could find to make the bar actually size correctly was 

this.batchDisplay.BeginUpdate();
 
dataTable.Rows.Add(i++, "Ralph"); //TODO TEST CODE
dataTable.Rows.Add(i++, "Walf");
dataTable.Rows.Add(i++, "Ralph");
dataTable.Rows.Add(i++, "Walf");
 
this.batchDisplay.Height += 1;
this.batchDisplay.Height -= 1;
 
this.batchDisplay.EndUpdate();

But that seems just silly, and while the scroll bar does size right it jumps to the top.

I am sure I am just making this harder than it needs to be, but I just can't seem to find a way to make the scroll bar stay where it is while I  add items.
Any assistance on this would be greatly appreciated.

Thanks,
Ralph

4 Answers, 1 is accepted

Sort by
0
Ralph
Top achievements
Rank 1
answered on 31 May 2013, 03:48 PM
I am using telerik Win controls v 2013.1.321.40
0
Ralph
Top achievements
Rank 1
answered on 03 Jun 2013, 12:05 PM
It seems anytime I wrap my updates in:

this.batchDisplay.BeginUpdate();
//Stuff
this.batchDisplay.EndUpdate();

Then the grid jumps to the top of the vertical scroll bar.

Am I using these incorrectly?
0
Accepted
Stefan
Telerik team
answered on 05 Jun 2013, 08:26 AM
Hello Ralph,

Thank you for writing.

There is a property designed to prevent this behavior when rows are added to the underlying data source. Just set it to false for the desired template and you should be OK:
radGridView1.MasterTemplate.SelectLastAddedRow = false;

I hope this helps.
 

Regards,
Stefan
Telerik
RadChart for WinForms is obsolete. Now what?
0
Accepted
Ralph
Top achievements
Rank 1
answered on 05 Jun 2013, 12:41 PM
this.radGridView1.MasterTemplate.SelectLastAddedRow = false;

Did not prevent 
private void button1_Click(object sender, EventArgs e)
{
    this.radGridView1.BeginUpdate();
 
    dataTable.Rows.Add(i++, "Jack");
    dataTable.Rows.Add(i++, "Donna");
    dataTable.Rows.Add(i++, "Jack");
    dataTable.Rows.Add(i++, "Donna");
    dataTable.Rows.Add(i++, "Jack");
    dataTable.Rows.Add(i++, "Donna");
 
    this.radGridView1.EndUpdate();
 
}

from popping the grid back to the top.

But it DID prevent make the below work exactly as I was hoping per my first post.

private void button1_Click(object sender, EventArgs e)
{
    dataTable.Rows.Add(i++, "Jack");
    dataTable.Rows.Add(i++, "Donna");
    dataTable.Rows.Add(i++, "Jack");
    dataTable.Rows.Add(i++, "Donna");
    dataTable.Rows.Add(i++, "Jack");
    dataTable.Rows.Add(i++, "Donna");
 
}


Many Thanks,
Ralph
Anthony
Top achievements
Rank 1
commented on 05 Jul 2022, 11:38 PM

I know this issue is several years old but I'm experiencing the same issue with regards to the grid popping back to the top and wondering if you figured out what was causing this. My grid is databound and I'm experiencing the same issue when adding a new row and grid is longer than the visible screen
Dinko | Tech Support Engineer
Telerik team
commented on 08 Jul 2022, 09:44 AM

By setting the SelectLastAddedRow property of the control to false I am able to disable this behavior. The last row won't be selected and the last selected row won't be changed thus the scrollbar will stay at its position when adding new items at the bottom. Can you confirm that you have set this property to false?

radGridView1.MasterTemplate.SelectLastAddedRow = false;

Anthony
Top achievements
Rank 1
commented on 08 Jul 2022, 01:39 PM | edited

Hi Dinko,

Unfortunately, this does not resolve my issue;

1. the way the grid is implemented (by client) each new row will cause the grid to grow. Scrollbars have been disabled/removed.

2. I want the last row to be selected and be visible, it is currently jumping to the top of the gird with new row add. The last row is selected but not visible which is the issue, for if I start typing without any mouse click or screen scroll it jumps back to the last row and starts typing in the new row.

Dinko | Tech Support Engineer
Telerik team
commented on 12 Jul 2022, 01:27 PM

In this case, you can set the RadGridView.AutoSize functionality to true. Thus, the grid will calculate its size according to the rows count and the width of the column. In this case, the scrollbars won't appear. As for the second case, as you want the last row to be visible, you need to set the above property to true (SelectLastAddedRow = true). If this does not help, can you send me a video of the described behavior so that I can have a better look at it? In addition, it will be great if you could send a sample project which demonstrates this so that I can debug it on my side.
Anthony
Top achievements
Rank 1
commented on 12 Jul 2022, 06:30 PM

Hi Dinko,

Yes we already have AutoSize functionality set and that is working as expected however, the SelectLastAddedRow functionality does not work. It's a rather large application but I'll see if I can create a working sample to send. I can't send any video via this forum only because the screens may contain sensitive information.

Dess | Tech Support Engineer, Principal
Telerik team
commented on 13 Jul 2022, 11:27 AM

Hi, Anthony,

If the last added row is selected but not visible in the view, note that you can use the TableElement.ScrollToRow method or the TableElement.EnsureRowVisible method. Otherwise, you can handle the CurrentRowChanged event and detect what causes selecting the first row:

this.radGridView1.TableElement.EnsureRowVisible(this.radGridView1.Rows.Last());

this.radGridView1.TableElement.ScrollToRow(this.radGridView1.Rows.Last());

Anthony
Top achievements
Rank 1
commented on 13 Jul 2022, 09:02 PM

HI Dess,

So the jump the top is actually not selecting the top row, the last row remains selected, however, It's just moving the user's screen to the top of the grid while keeping the last row active. If I begin typing then the last row becomes visible.

When do you suggest calling the ScrollToRow or EnsureRowVisible methods. Am I supposed to call those methods in the UserAddedRow or UserAddingRow events? 

Dess | Tech Support Engineer, Principal
Telerik team
commented on 14 Jul 2022, 04:31 AM

If only the vertical scrollbar jumps to the top, but the selected row is preserved, it is possible to handle the TableElement.VScrollBar.ValueChanged event and detect the moment when the scrollbar's value is set to 0. Thus, you can determine what causes this behavior. Then, it would be easy to determine the appropriate moment for calling the previously suggested methods. 
Anthony
Top achievements
Rank 1
commented on 14 Jul 2022, 03:31 PM

As mentioned previously we don't have scrollbars as they are hidden, the grid will grow as items are added. I implemented the VScrollBar.ValueChanged event just to see and it is never triggered.
Dess | Tech Support Engineer, Principal
Telerik team
commented on 18 Jul 2022, 04:58 AM

Hi, Anthony,

Without replicating the issue locally, it wouldn't be easy to determine what causes this undesired behavior on your end. That is why I would kindly ask you to submit a support ticket from your Telerik account and provide a sample runnable project demonstrating the problem you are facing. Thus, we would be able to make an adequate analysis of the precise case and provide further assistance. Thank you in advance for your cooperation.

Tags
GridView
Asked by
Ralph
Top achievements
Rank 1
Answers by
Ralph
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or