Binding RadGridView to DataTable and Adding Items to DataTable

5 posts, 2 answers
  1. Ralph
    Ralph avatar
    17 posts
    Member since:
    May 2011

    Posted 31 May 2013 Link to this post

    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
  2. Ralph
    Ralph avatar
    17 posts
    Member since:
    May 2011

    Posted 31 May 2013 Link to this post

    I am using telerik Win controls v 2013.1.321.40
  3. Ralph
    Ralph avatar
    17 posts
    Member since:
    May 2011

    Posted 03 Jun 2013 Link to this post

    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?
  4. Answer
    Stefan
    Admin
    Stefan avatar
    2911 posts

    Posted 05 Jun 2013 Link to this post

    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?
  5. Answer
    Ralph
    Ralph avatar
    17 posts
    Member since:
    May 2011

    Posted 05 Jun 2013 Link to this post

    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
Back to Top