Limit datagrid items in asp.net c#
我在ASP.NET C中使用
我知道我们可以控制使用
但我也不想要。我想运行一次
如果我能做到最好
在正确的道路上昂首挺胸的任何一种人都会受到赞赏
然而,当我们使用
我们要做的就是限制这个
限制数据数量
1 | var firstFiveItems = myList.Take(5); |
或
1 | var secondFiveItems = myList.Skip(5).Take(5); |
。
这里有两个选项可以考虑。第一种方法是使用
1 2 3 4 5 | DataTable dt = yourDataTableSource; GridView1.DataSource = dt.AsEnumerable().Take(5).CopyToDataTable(); //Linq example with sorting added //GridView1.DataSource = dt.AsEnumerable().OrderBy(x => x["columnName"]).Take(5).CopyToDataTable(); GridView1.DataBind(); |
或者可以使用通常在.aspx页上找到的相同属性。现在只有在代码隐藏中设置了这些,您仍然可以为此使用当前的数据源。
1 2 3 4 | GridView1.PageSize = 5; GridView1.AllowPaging = true; GridView1.PagerSettings.Visible = false; GridView1.DataBind(); |
。
或者对于数据报
1 2 3 4 | DataGrid1.PageSize = 10; DataGrid1.AllowPaging = true; DataGrid1.PagerStyle.Visible = false; DataGrid1.DataBind(); |
为了完成你的问题,这里有一个小例子,让你开始如何在不刷新页面的情况下获取显示项目中的更改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | </asp:ScriptManager> <!-- a ScriptManager is required when working with UpdatePanels --> <ContentTemplate> </asp:GridView> <br /> </asp:ListItem> </asp:ListItem> </asp:ListItem> </asp:DropDownList> </ContentTemplate> </asp:UpdatePanel> |
。
代码隐藏:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DataTable dt = yourDataTableSource; GridView1.DataSource = dt; GridView1.PageSize = 10; GridView1.AllowPaging = true; GridView1.PagerSettings.Visible = false; GridView1.DataBind(); ViewState["GridView1Content"] = dt; } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { int pageSize = 10; //always do validation with user input... try { pageSize = Convert.ToInt32(DropDownList1.SelectedValue); } catch { } GridView1.PageSize = pageSize; GridView1.DataSource = ViewState["GridView1Content"] as DataTable; GridView1.DataBind(); } |
。
我是这样来的,但我使用的是GridView,当页面太过加载时,我就迁移到具有服务器端页面处理的数据表中。
它是一个通用层。根据你的需要修改它!
下面是我得到的解析器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Web; namespace iMax_WeSite.app.Objects.DataTables { public class DataTable { public DataTable() { } public int sEcho { get; set; } public int iTotalRecords { get; set; } public int iTotalDisplayRecords { get; set; } public string iTotalValue { get; set; } //Somatoria total do valor public string iTotalFilteredValue { get; set; } //Somatoria parcial do valor public List<List<string>> aaData { get; set; } public string sColumns { get; set; } public void Import(string[] properties) { sColumns = string.Empty; for (int i = 0; i < properties.Length; i++) { sColumns += properties[i]; if (i < properties.Length - 1) sColumns +=","; } } } public class DataTableParser<T> { private const string INDIVIDUAL_SEARCH_KEY_PREFIX ="sSearch_"; private const string INDIVIDUAL_SORT_KEY_PREFIX ="iSortCol_"; private const string INDIVIDUAL_SORT_DIRECTION_KEY_PREFIX ="sSortDir_"; private const string DISPLAY_START ="iDisplayStart"; private const string DISPLAY_LENGTH ="iDisplayLength"; private const string ECHO ="sEcho"; private const string SEARCH ="sSearch"; private const string ASCENDING_SORT ="asc"; private IQueryable<T> _queriable; private readonly Dictionary<string, object> _tableParams; private readonly Type _type; private readonly System.Reflection.PropertyInfo[] _properties; public DataTableParser(Dictionary<string, object> tableParams, IQueryable<T> queriable) { _queriable = queriable; _tableParams = tableParams; _type = typeof(T); _properties = _type.GetProperties(); } public DataTable Parse() { var list = new DataTable(); list.Import(_properties.Select(x => x.Name).ToArray()); list.sEcho = (int)_tableParams[ECHO]; list.iTotalRecords = _queriable.Count(); ApplySort(); int skip = 0, take = list.iTotalRecords; if (_tableParams.ContainsKey(DISPLAY_START)) skip = (int)_tableParams[DISPLAY_START]; if (_tableParams.ContainsKey(DISPLAY_LENGTH)) take = (int)_tableParams[DISPLAY_LENGTH]; //tenho de segregar para mostrar o filtrado list.aaData = _queriable.Where(ApplyGenericSearch) .Where(IndividualPropertySearch) .Skip(skip) .Take(take) .Select(SelectProperties) .ToList(); //tenho de segregar para mostrar o filtrado geral list.iTotalDisplayRecords = _queriable.Where(ApplyGenericSearch) .Where(IndividualPropertySearch) .Select(SelectProperties) .Count(); return list; } private void ApplySort() { foreach (string key in _tableParams.Keys.Where(x => x.StartsWith(INDIVIDUAL_SORT_KEY_PREFIX))) { int sortcolumn = (int)_tableParams[key]; if (sortcolumn < 0 || sortcolumn >= _properties.Length) break; string sortdir = _tableParams[INDIVIDUAL_SORT_DIRECTION_KEY_PREFIX + key.Replace(INDIVIDUAL_SORT_KEY_PREFIX, string.Empty)].ToString(); var paramExpr = Expression.Parameter(typeof(T),"val"); var propertyExpr = Expression.Lambda<Func<T, object>>(Expression.Convert(Expression.Property(paramExpr, _properties[sortcolumn]), typeof(object)), paramExpr); if (string.IsNullOrEmpty(sortdir) || sortdir.Equals(ASCENDING_SORT, StringComparison.OrdinalIgnoreCase)) _queriable = _queriable.OrderBy(propertyExpr); else _queriable = _queriable.OrderByDescending(propertyExpr); } } private Expression<Func<T, List<string>>> SelectProperties { get { return value => _properties.Select ( prop => (prop.GetValue(value, new object[0]) ?? string.Empty).ToString() ) .ToList(); } } private Expression<Func<T, bool>> IndividualPropertySearch { get { var paramExpr = Expression.Parameter(typeof(T),"val"); Expression whereExpr = Expression.Constant(true); // default is val => True foreach (string key in _tableParams.Keys.Where(x => x.StartsWith(INDIVIDUAL_SEARCH_KEY_PREFIX))) { int property = -1; if (!int.TryParse(key.Replace(INDIVIDUAL_SEARCH_KEY_PREFIX, string.Empty), out property) || property >= _properties.Length || string.IsNullOrEmpty(_tableParams[key].ToString())) continue; // ignore if the option is invalid string query = _tableParams[key].ToString().ToLower(); var toStringCall = Expression.Call( Expression.Call( Expression.Property(paramExpr, _properties[property]),"ToString", new Type[0]), typeof(string).GetMethod("ToLower", new Type[0])); whereExpr = Expression.And(whereExpr, Expression.Call(toStringCall, typeof(string).GetMethod("Contains", BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase), Expression.Constant(query))); } return Expression.Lambda<Func<T, bool>>(whereExpr, paramExpr); } } private Expression<Func<T, bool>> ApplyGenericSearch { get { if (!_tableParams.ContainsKey(SEARCH) || _properties.Length == 0) return x => true; string search = _tableParams[SEARCH].ToString(); if (String.IsNullOrEmpty(search)) return x => true; var searchExpression = Expression.Constant(search.ToLower()); var paramExpression = Expression.Parameter(typeof(T),"val"); var propertyQuery = (from property in _properties let tostringcall = Expression.Call( Expression.Call( Expression.Property(paramExpression, property),"ToString", new Type[0]), typeof(string).GetMethod("ToLower", new Type[0])) select Expression.Call(tostringcall, typeof(string).GetMethod("Contains"), searchExpression)).ToArray(); Expression compoundExpression = propertyQuery[0]; for (int i = 1; i < propertyQuery.Length; i++) compoundExpression = Expression.Or(compoundExpression, propertyQuery[i]); return Expression.Lambda<Func<T, bool>>(compoundExpression, paramExpression); } } } } |
希望我帮了你的忙!
您是否尝试过DataTable插件?它具有根据我们的要求显示记录的功能。查看此链接
为网格应用DataTable插件以显示数据,您可以根据需要更改
1 2 3 4 5 6 7 8 | $(document).ready(function() { $('#example').dataTable({ "aLengthMenu": [[10, 20, 30, -1], [10, 20, 30,"All"]],// set as per your requirement "iDisplayLength": 10 // dont forget to change as per alengthmenu }); } ); |
号