一、简单gridview列表展示
1.HomeController
1 public ActionResult Index() 2 { 3 //返回绑定models的index.cshtml 4 return View(NorthwindDataProvider.GetDocuments()); 5 } 6 7 /// <summary> 8 /// 回传方法代码 9 /// </summary> 10 /// <returns></returns> 11 public ActionResult GridViewPartialView() 12 { 13 //局部刷新并绑定models 14 return PartialView("GridViewPartialView", NorthwindDataProvider.GetDocuments()); 15 }
2.view(index.cshtml)
1 <div id="GridViewPartialView"> 2 @Html.Partial("GridViewPartialView", Model) 3 </div>
3.view(GridViewPartialView.cshtml)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 @* DXCOMMENT: Configure GridView *@ 2 @Html.DevExpress().GridView( 3 settings => 4 { 5 settings.Name = "GridView"; 6 settings.CallbackRouteValues = new { Controller = "Home", Action = "GridViewPartialView" }; 7 settings.KeyFieldName = "DocumentId"; 8 settings.Width = System.Web.UI.WebControls.Unit.Percentage(100); 9 settings.SettingsPager.Visible = false; 10 settings.SettingsPager.PageSize = 20; 11 settings.Settings.VerticalScrollBarMode = ScrollBarMode.Visible; 12 settings.Settings.VerticalScrollableHeight = 350; 13 settings.Settings.VerticalScrollBarStyle = GridViewVerticalScrollBarStyle.Virtual; 14 settings.ControlStyle.Paddings.PaddingTop = System.Web.UI.WebControls.Unit.Pixel(10); 15 settings.ControlStyle.Border.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(0); 16 settings.ControlStyle.BorderBottom.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1); 17 //显示序号列 18 settings.Columns.Add(c => 19 { 20 c.Caption = "序号"; 21 c.SetDataItemTemplateContent(s => 22 { 23 ViewContext.Writer.Write(s.ItemIndex + 1); 24 }); 25 }); 26 //绑定主键列,并隐藏该列 27 settings.Columns.Add("DocumentId", "").Visible = false; 28 settings.Columns.Add("DocumentCode", "文书字号"); 29 settings.Columns.Add("DocumentName", "文书名称"); 30 //根据数据库中'1'or'0'显示数据列表为'行政'or'刑事' 31 settings.Columns.Add(c => 32 { 33 c.FieldName = "DocumentType"; 34 c.Caption = "类型"; 35 c.SetDataItemTemplateContent(s => 36 { 37 string showText = s.Text; 38 if (s.Text == "0") 39 { 40 showText = "行政"; 41 } 42 else if (s.Text == "1") 43 { 44 showText = "刑事"; 45 } 46 ViewContext.Writer.Write(showText); 47 }); 48 }); 49 //格式化日期:MVCxGridViewColumnType.DateEdit (年/月/日) 50 settings.Columns.Add(c => 51 { 52 c.FieldName = "DocumentDate"; 53 c.Caption = "文书日期"; 54 c.ColumnType = MVCxGridViewColumnType.DateEdit; 55 c.Settings.AutoFilterCondition = AutoFilterCondition.Equals; 56 }); 57 58 settings.Columns.Add("DocumentMemo", "备注"); 59 //显示复选框列 60 settings.CommandColumn.Visible = true; 61 settings.CommandColumn.ShowSelectCheckbox = true; 62 }).Bind(Model).GetHtml()