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

How to get the datagrid row count with pagination

8 Answers 124 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Cp
Top achievements
Rank 1
Cp asked on 20 Apr 2012, 10:25 AM
Hi,
   telerik team

  When I verified  a cell's value in a datagrid  with pagination, I failed to get all the pages row count but only the current page.
  It seems the 'grid.Rows.Count' just get the current page row count, 5 here.
  And I want to get all the 7 pages row count, how can I to realize this? Reminder, the records on the last page,Page 7, may be less than 5.
  And the values I verified ('test for comment26') is indeed on the 1st page, even though, it failed.

  Attachment the screenshot and my coded step fails below, and hope they help.

  Thanks in advance and have a nice weekend.


//Get the data grid.
SilverlightApp app = ActiveBrowser.SilverlightApps()[0];
DataGrid grid = app.Find.ByAutomationId<DataGrid>("RelatedCommentsDataGrid");
 
int r = grid.Rows.Count;

List<string> list = new List<string>();
 
//Place the TextBlock content of the comment subject cell from each row into the string list.

for (int i = 0; i < r; i++)
{
   DataGridRow row = grid.Rows[i];
   DataGridCell cell = row.Cells[2];   
   list.Add(cell.TextBlockContent);
   Log.WriteLine(cell.TextBlockContent);
}

//Get the created comment subject
object createdComment = GetExtractedValue("CommentSubject");

//Compare the created comment subject to the list and determine if it created successfully.
for (int j = 0; j < list.Count; j++)
{
   if (j+1 == list.Count)
   {
      break;
   }
    else
   {
    if (createdComment.Equals(list[j])){
        Log.WriteLine(createdComment+"is created successfully");
    }
    else{
        Log.WriteLine(createdComment+"fails to created" );
    }

  }
}

 



8 Answers, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 20 Apr 2012, 02:49 PM
Hello Cp,

Please see this article from our 'Code Samples' section for a solution to this problem.

Greetings,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Cp
Top achievements
Rank 1
answered on 23 Apr 2012, 09:27 AM
Hi,Plamen 
   Thank you for your quick response.

   I went through that article before and noticed it's about Radgridview, is that method also applicable to datagrid?

   When I applied them to my test case, it failed at this line, and I will attach the detailed error:
  ----------
   // Grab the VirtualizingPanel contained in the RadGridView. This is used to control the viewable portion of the grid.
FrameworkElement VirtualizingPanel = grid.Find.ByType("GridViewVirtualizingPanel");
  -----------

I have no idea what 'GridViewVirtualizingPanel' mentioned, is it property of radgridview?
If yes, is it also property of datagrid?
Due to the error 'Element Not found!', maybe it's not.

Best Regards.
0
Plamen
Telerik team
answered on 25 Apr 2012, 02:50 PM
Hi Cp,

The VirtualizingPanel is a RadGridView child element, which contains the row elements. The provided code sample solution is not applicable for a regular DataGrid.

However, I've managed to create a working sample for you against the MS Silverlight DataGrid. Here's the code: 
// Get the Silverlight app
SilverlightApp app = ActiveBrowser.SilverlightApps()[0];
  
// Get the DataGrid
DataGrid grid = app.Find.ByType<DataGrid>();
  
// Rows per page
int totalPageRows = 0;
// Overall rows
int totalOverallRows = 0;
  
// Get the two navigation buttons 
Button lastPageButton = app.Find.ByName<Button>("LastPageButton");
Button nextPageButton = app.Find.ByName<Button>("NextPageButton");
  
  
// Calculate the overall rows count, page by page, while the LastPageButton is enabled. 
do
{
    // Get the rows count for the current page
    totalPageRows = grid.RowElements.Count;
  
    // Calculate the overall count
    totalOverallRows = totalPageRows + totalOverallRows;
  
    // Click to navigate to next page
    nextPageButton.User.Click();
  
    System.Threading.Thread.Sleep(1500);
}
  
while (lastPageButton.IsEnabled);
  
totalOverallRows = totalPageRows + totalOverallRows;
  
//Log the result
Log.WriteLine("Total overall rows count is:  " + totalOverallRows.ToString());

Sample Test Studio project is attached. Check this video to see how it works and let me know if you need further assistance on this.


Kind regards,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Cp
Top achievements
Rank 1
answered on 26 Apr 2012, 04:58 AM
Hi, Plamen

   Thank you very much for your detailed reply.
   But the get row count method may be not applicable to my application.

  '// Get the rows count for the current page
    totalPageRows = grid.RowElements.Count; '

--- It returns the wrong value for the last page. eg. the last page is 2nd page with 1 records.
    But the "Total overall rows count is:  " 10, no matter how many records on the last page.
    (The max row count is 5)

How to handle the page with the random rows(not the maximum row count)?

 Attached is the 1st & last page view, hope it can help.
  
Thanks.
0
Plamen
Telerik team
answered on 27 Apr 2012, 08:55 AM
Hi Cp,

I'm sorry to hear it doesn't work in your case. First, try adding the following line after the right after the next button click line:
// Click to navigate to next page
nextPageButton.User.Click();
// Refresh DataGrid
grid.Refresh();

This should refresh the grid and you should be able to get the correct row count. If it still doesn't work. Please try the following code as work-around:

// Get the Silverlight app
SilverlightApp app = ActiveBrowser.SilverlightApps()[0];

// Get the DataGrid
DataGrid grid = app.Find.ByType<DataGrid>();

// Rows per page
int totalPageRows = 0;

// Overall rows
int totalOverallRows = 0;

// Get the two navigation buttons 
Button lastPageButton = app.Find.ByName<Button>("LastPageButton");
Button nextPageButton = app.Find.ByName<Button>("NextPageButton");

IList<DataGridRow> rows;

// Calculate the overall rows count, page by page, while the LastPageButton is enabled. 
do
{
    rows = grid.Find.AllByType<DataGridRow>();

    // Get the rows count for the current page
    foreach (DataGridRow row in rows)
    {
        totalPageRows++;
    }

    // Calculate the overall count
    totalOverallRows = totalPageRows + totalOverallRows;

    // Click to navigate to next page
    nextPageButton.User.Click();

    // Refresh DataGrid
    grid.Refresh();
    totalPageRows = 0;

    System.Threading.Thread.Sleep(1500);
}
while (lastPageButton.IsEnabled);

rows = grid.Find.AllByType<DataGridRow>();
foreach (DataGridRow row in rows)
{
    totalPageRows++;
}
totalOverallRows = totalPageRows + totalOverallRows;
  
//Log the result
Log.WriteLine("Total overall rows count is:  " + totalOverallRows.ToString());
Hope this helps!

Kind regards,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Cp
Top achievements
Rank 1
answered on 15 May 2012, 08:49 AM
Hi, Plamen

    Sorry for responsing so late as some other on-going project.
    And Thank you very much for your detailed answer.

    But I'm sorry to say that it still doesn't work.
    The rows account returns is still as 'the maximum rows/page * page numbers', no matter how many rows actually exsit on the last page.
0
Jonas
Top achievements
Rank 2
answered on 15 May 2012, 10:41 AM
Hello,

Have you tried with this approach? To find each grid every time inside the loop instead of outside, and before using Find try refresh on entire App window just to be sure. The code below works for me in a simular case, but then by using RadGridViews.

// Get the Silverlight app
SilverlightApp app = ActiveBrowser.SilverlightApps()[0];
   
// Rows per page
int totalPageRows = 0;
// Overall rows
int totalOverallRows = 0;
   
// Get the two navigation buttons
Button lastPageButton = app.Find.ByName<Button>("LastPageButton");
Button nextPageButton = app.Find.ByName<Button>("NextPageButton");
   
// Calculate the overall rows count, page by page, while the LastPageButton is enabled.
while (lastPageButton.IsEnabled);
{
    //Refresh the entire appWindow then search for the DataGrid
    app.Find.RefreshRoot();
    DataGrid grid = app.Find.ByType<DataGrid>();
    // Get the rows count for the current page
    totalPageRows = grid.RowElements.Count;
   
    // Calculate the overall count
    totalOverallRows = totalPageRows + totalOverallRows;
   
    // Click to navigate to next page
    nextPageButton.User.Click();
   
    System.Threading.Thread.Sleep(1500);
}
totalOverallRows = totalPageRows + totalOverallRows;
   
//Log the result
Log.WriteLine("Total overall rows count is:  " + totalOverallRows.ToString());

Best Regards
Jonas
0
Plamen
Telerik team
answered on 17 May 2012, 11:46 AM
Hi Cp,

Please try Jonas's approach and let us know if it works for you. If you still have problem getting the rows count, the best way for us to troubleshoot this is to provide the means to reproduce the issue locally(either in your app or on a public site). If you feel that information is sensitive, you can file a support ticket, which is confidential unlike this forum thread.

@Jonas. I've updated your Telerik Points for assisting another customer.

Regards,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
Tags
General Discussions
Asked by
Cp
Top achievements
Rank 1
Answers by
Plamen
Telerik team
Cp
Top achievements
Rank 1
Jonas
Top achievements
Rank 2
Share this question
or