c# - Constant length for gridview -
recently came accross 1 issue. issue though not impacting process make gui if resolved.
issue is...i have search screen on basis of search criteria records filtered displayed in gridview having itemtemplate defined. problem grid's length adjusting according number of records in grid. need have constant height of grid page length remains constant searches. height should increased when user wishes show more 10 records per page.
please me problem.
since requirement is: you have button " show more", when clicked displays next more 10 rows.
one way use list
of objects
, call list<t>.getrange()
method. can return required number of records using take(n)
extension in linq
, in case using already. had counttodisplay
variable save current number of records display, intially set 0 (zero)
getemployees
method
protected list<employee> getemployees(int counttodisplay) { list<employee> employees= new list<employee>(); // sample code fill list. use `take` extension dbdatacontext db = new dbdatacontext(); var e= ( c in db.employees select c ).take(counttodisplay); //iterate through results , add list<employee> foreach(var c in e) { employee emp = new employee { name = c.name, address = c.address }; employees.add(emp); } return employees; }
here employee
class:
public class employee { public string name; public string address; }
now comes interesting part. suppose have button " show more", when clicked displays next more 10 rows. goes on until reach end. in case used link button , used server method when clicked load , refresh grid.
<asp:linkbutton id="btnshowmore" class="showmorelink" runat="server" onclick="showmoreresults"></asp:linkbutton>
and showmoreresults
function:
private void showmoreresults() { // keep incrementing minimum rows displayed, 5, 10 ... counttodisplay = counttodisplay + convert.toint16(webconfigurationmanager.appsettings["empgridviewmincount"]); // called grid refresh method refreshgrid(); }
grid refresh method:
private void refreshgrid() { list<employee> employees = getemployees(counttodisplay) if (employees != null) { empgrid.datasource = employees; } // **hide showmore link in case count display exceeds total record.** btnshowmore.visible = employees.count > counttodisplay; // bind gridview empgrid.databind(); }
Comments
Post a Comment