Reputation: 61
Recently i came accross one issue. The issue though is not impacting my process but will make the GUI look good if resolved.
Issue is...I have a search screen where on the basis of search criteria some records are filtered which are displayed in a gridview having some ItemTemplate defined. My problem is the grid's length is adjusting according to the number of records in the grid. I need to have a constant height of grid so that my page length remains constant for all searches. This height should only be increased only when user wishes to show more than 10 records per page.
Please help me with this problem.
Upvotes: 1
Views: 194
Reputation: 10565
since your requirement is: you have a button say " Show More", when clicked displays next more 10 rows.
One way is to use List
of objects
and then call List<T>.GetRange()
method. you can also return only the required number of records using Take(n)
extension in LINQ
, in case you are using this already. I had CountToDisplay
as a variable to save the current number of records to display, intially set to 0 (Zero)
GetEmployees
method
protected List<Employee> GetEmployees(int CountToDisplay)
{
List<Employee> employees= new List<employee>();
// Sample code to fill the List. Use the `Take` Extension
dbDataContext db = new dbDataContext();
var e= ( from c in db.Employees
select c ).Take(CountToDisplay);
//iterate through results and add to List<Employee>
foreach(var c in e)
{
employee emp = new employee { name = c.name, address = c.address };
employees.Add(emp);
}
return employees;
}
Here Employee
is a class:
public class Employee
{
public string name;
public string address;
}
Now comes the interesting part. Suppose you have a button " Show More", when clicked displays next more 10 rows. This goes on until you reach the end. So in my case I used a link button and used a server method when clicked to load and refresh the grid.
<asp:LinkButton ID="btnShowMore" class="ShowMoreLink" runat="server"
OnClick="ShowMoreResults"></asp:LinkButton>
And this is the ShowMoreResults
function:
private void ShowMoreResults()
{
// Keep incrementing with the Minimum rows to be displayed, 5, 10 ...
CountToDisplay = CountToDisplay +
Convert.ToInt16(WebConfigurationManager.AppSettings["EmpGridViewMinCount"]);
// finally called the Grid refresh method
RefreshGrid();
}
Grid Refresh method:
private void RefreshGrid()
{
List<Employee> employees = GetEmployees(CountToDisplay)
if (employees != null)
{
empGrid.DataSource = employees;
}
// **Hide the ShowMore link in case Count to display exceeds the total record.**
btnShowMore.Visible = employees.Count > CountToDisplay;
// Finally bind the GridView
empGrid.DataBind();
}
Upvotes: 1