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)
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()GridView