Winfrom datagridview page display, datagridview
This time, the datagridview binds data and performs paging operations. Because webservice is used, the code is described in detail. QueryByCondition is a query function.
Client:
PageData pageData = new PageData (); // used to receive data
Public bool QueryByCondition (Entity. ProductStatus status, int pageIndex ){
SoapHeaderTransferData (); // soap authentication of webservice, which can be used to access the webservice server. If you do not use webservice, you do not need to add it. String query_condition = txt_title.Text.Trim (). ToString (); // query Condition
// Return the string type. serialization means that the class object is converted to the string type, which is convenient for transmission between WebServices.
String str = ps. QueryByCondition (query_condition, (pm. ProductStatus) Enum. Parse (typeof (pm. ProductStatus), status. ToString ()),
User_id, pageIndex, pageSize); // There are five parameters. The first three are query conditions, the last two are paging conditions, and pageIndex is input on the current page according to your needs, pageSize is the number of data entries on a page. if (str = null) {return false;} else {
// Deserialize str to obtain the Class Object Data pageData = VCommons. objectXmlExtensions. toObj <Entity. ot. pageData> (VCommons. utils. urlDecode (str); this. dataGridViewProduct. autoGenerateColumns = false; // you cannot set the datagridview to automatically add this column. dataGridViewProduct. dataSource = pageData. data; DataGridViewProduct. clearSelection (); lblPageCount. text = pageData. totalPage. toString (); lbCurrentPage. text = pageData. pageIndex. toString (); return true;
}}
Server:
# Region Query product information by condition [SoapHeader ("myHeader")] [WebMethod (EnableSession = true)] public string QueryByCondition (string query_condition, Entity. productStatus status, string userid, int pageIndex, int pageSize ){
// Perform authentication if (myHeader. CheckLogin () {using (var repository = new DataE. VAERP. Repository ()){
// Query data based on the condition var linq = from product in repository. getIQueryable <Entity. VAERP. product> () join data in repository. getIQueryable <Entity. VAERP. productData> () on product. ID equals data. productID where (product. userID = userid | data. sellerID = userid) & product. status = status select product; IQueryable <Entity. VAERP. product> pros = string. isNullOrWhiteSpace (query_condition )? Linq: linq. Where (item => item. Title. StartsWith (query_condition) | item. Title. EndsWith (query_condition) | item. Title. IndexOf (query_condition )! =-1 );
// Perform Paging
Var pageData = new Entity. pagedList <Entity. VAERP. product> (pros, pageIndex, pageSize); // serialize var str = VCommons. utils. urlEncode (new Entity. ot. pageData () {Data = pageData. toArray (), PageIndex = pageData. pageIndex, PageSize = pageData. pageSize, TotalPage = pageData. totalPages }. toXml (); return str ;}} else {return VCommons. utils. urlEncode (new Entity. pagedList <Entity. VAERP. product> (null, pageIndex, pageSize ). toXml () ;}# endregion
PagedList (,,)
Public PagedList (IQueryable <T> source, int index, int pageSize) {if (source! = Null) // determines whether the passed object set is null {int total = source. count (); this. totalCount = total; this. totalPages = total/pageSize; if (total % pageSize> 0) TotalPages ++; this. pageSize = pageSize; if (index> this. totalPages) {index = this. totalPages;} if (index <1) {index = 1;} this. pageIndex = index; this. addRange (source. skip (index-1) * pageSize ). take (pageSize ). toList (); // Skip is the number of pages to jump to and how many to Take returns }}