Devexpress 12.2.8.0 部分学习笔记1 【ASPxGridview】
1 <%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="MasterDetail.aspx.cs" 2 Inherits="MasterDetail_MasterDetail" %> 3 <asp:Content ID="Content1" ContentPlaceHolderID="ContentHolder" runat="Server"> 4 <dx:ASPxCheckBox ID="chkSingleExpanded" runat="server" Text="Keep a single expanded row at a time" 5 AutoPostBack="true" OnCheckedChanged="chkSingleExpanded_CheckedChanged" /> 6 <br /> 7 <dx:ASPxGridView ID="grid" ClientInstanceName="grid" runat="server" DataSourceID="masterDataSource" 8 KeyFieldName="CustomerID" Width="100%"> 9 <Columns> 10 <dx:GridViewDataColumn FieldName="ContactName" VisibleIndex="0" /> 11 <dx:GridViewDataColumn FieldName="CompanyName" VisibleIndex="1" /> 12 <dx:GridViewDataColumn FieldName="City" VisibleIndex="2" /> 13 <dx:GridViewDataColumn FieldName="Country" VisibleIndex="3" /> 14 </Columns> 15 <Templates> 16 <DetailRow> 17 Contact Phone: <b> 18 <%# Eval("Phone")%></b>, Fax: <b> 19 <%# Eval("Fax")%></b> 20 <br /> 21 <br /> 22 <dx:ASPxGridView ID="detailGrid" runat="server" DataSourceID="detailDataSource" KeyFieldName="OrderID" 23 Width="100%" OnBeforePerformDataSelect="detailGrid_DataSelect" OnCustomUnboundColumnData="detailGrid_CustomUnboundColumnData"> 24 <Columns> 25 <dx:GridViewDataColumn FieldName="OrderID" Caption="Order Id" VisibleIndex="1" /> 26 <dx:GridViewDataColumn FieldName="OrderDate" VisibleIndex="2" /> 27 <dx:GridViewDataColumn FieldName="ShipName" VisibleIndex="2" /> 28 <dx:GridViewDataColumn FieldName="Quantity" VisibleIndex="5" Name="Quantity" /> 29 <dx:GridViewDataTextColumn FieldName="UnitPrice" VisibleIndex="4"> 30 <PropertiesTextEdit DisplayFormatString="c" /> 31 </dx:GridViewDataTextColumn> 32 <dx:GridViewDataTextColumn FieldName="Total" VisibleIndex="6" UnboundType="Decimal"> 33 <PropertiesTextEdit DisplayFormatString="c" /> 34 </dx:GridViewDataTextColumn> 35 </Columns> 36 <Settings ShowFooter="True" /> 37 <TotalSummary> 38 <dx:ASPxSummaryItem FieldName="CompanyName" SummaryType="Count" /> 39 <dx:ASPxSummaryItem FieldName="Total" SummaryType="Sum" /> 40 <dx:ASPxSummaryItem FieldName="Quantity" SummaryType="Sum" /> 41 </TotalSummary> 42 </dx:ASPxGridView> 43 </DetailRow> 44 </Templates> 45 <SettingsDetail ShowDetailRow="true" /> 46 </dx:ASPxGridView> 47 <asp:AccessDataSource ID="masterDataSource" runat="server" DataFile="~/App_Data/nwind.mdb" 48 SelectCommand="SELECT * FROM [Customers]"></asp:AccessDataSource> 49 <asp:AccessDataSource ID="detailDataSource" runat="server" DataFile="~/App_Data/nwind.mdb" 50 SelectCommand="SELECT * FROM [Invoices] Where CustomerID = ?"> 51 <SelectParameters> 52 <asp:SessionParameter Name="CustomerID" SessionField="CustomerID" Type="String" /> 53 </SelectParameters> 54 </asp:AccessDataSource> 55 </asp:Content>
1 using System; 2 using System.Web.UI; 3 using DevExpress.Web.ASPxGridView; 4 5 public partial class MasterDetail_MasterDetail : Page { 6 protected void Page_Load(object sender, EventArgs e) { 7 if(!IsPostBack) { 8 grid.DataBind(); 9 grid.DetailRows.ExpandRow(0); 10 } 11 } 12 protected void detailGrid_DataSelect(object sender, EventArgs e) { 13 Session["CustomerID"] = (sender as ASPxGridView).GetMasterRowKeyValue(); 14 } 15 protected void detailGrid_CustomUnboundColumnData(object sender, ASPxGridViewColumnDataEventArgs e) { 16 if(e.Column.FieldName == "Total") { 17 decimal price = (decimal)e.GetListSourceFieldValue("UnitPrice"); 18 int quantity = Convert.ToInt32(e.GetListSourceFieldValue("Quantity")); 19 e.Value = price * quantity; 20 } 21 } 22 protected void chkSingleExpanded_CheckedChanged(object sender, EventArgs e) { 23 grid.SettingsDetail.AllowOnlyOneMasterRowExpanded = chkSingleExpanded.Checked; 24 if(grid.SettingsDetail.AllowOnlyOneMasterRowExpanded) { 25 grid.DetailRows.CollapseAllRows(); 26 } 27 } 28 }
1 /// <summary> 2 /// grid total 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 protected void ASPxGridView4_CustomUnboundColumnData(object sender, ASPxGridViewColumnDataEventArgs e) 7 { 8 if (e.Column.FieldName == "总计") 9 { 10 int unit_price =Convert .ToInt32 ( e.GetListSourceFieldValue("unit_price")); 11 int quantity = Convert.ToInt32(e.GetListSourceFieldValue("quantity")); 12 e.Value = unit_price * quantity; 13 } 14 if (e.Column.FieldName == "折后") 15 { 16 int unit_price = Convert.ToInt32(e.GetListSourceFieldValue("unit_price")); 17 int quantity = Convert.ToInt32(e.GetListSourceFieldValue("quantity")); 18 decimal discount = Convert.ToDecimal (e.GetListSourceFieldValue("discount"));//在这里 不能直接用强制转换,要用convert,否则会报错 19 e.Value = unit_price * quantity * discount; 20 } 21 }
【详细和主表】
绑定到数据 | |||||
数据表示基础 | |||||
对焦和导航 | |||||
数据编辑 | |||||
数据塑造和操纵 | |||||
数据格式 | |||||
主从关系 | |||||
外观定制 | |||||
布局 | |||||
模板 | |||||
出口 | |||||
ASPxGridLookup |
主要就是这些功能
【绑定数据 Binding to data】
sqldatasource这个方式
【数据表示基础 data represention basics】
有一个自定义窗口的特性
【对焦与导航 Focus and navigation】
焦点行
ASPxGridView.FocusedRowIndex属性--->true
ASPxGridView.FocusedRowChanged
选择
multiple row selection
【数据编辑 data edit】
Edit form 有一点值得注意就是,自动绑定的时候,主键那一列
会自动设为readonly=true,如果要启用编辑的话,需要把
那个column的这个属性改变,还有绑定的时候要在高级那个选项的内容勾选才能编辑
【数据验证】
1 using System; 2 using System.Collections.Generic; 3 using System.Web.UI; 4 using DevExpress.Web.ASPxGridView; 5 6 public partial class GridEditing_Validation : Page { 7 protected void Page_Load(object sender, EventArgs e) { 8 9 } 10 protected void grid_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e) { 11 foreach(GridViewColumn column in grid.Columns) { 12 GridViewDataColumn dataColumn = column as GridViewDataColumn; 13 if(dataColumn == null) continue; 14 if(e.NewValues[dataColumn.FieldName] == null) { 15 e.Errors[dataColumn] = "Value can't be null."; 16 } 17 } 18 if(e.Errors.Count > 0) e.RowError = "Please, fill all fields."; 19 if(e.NewValues["FirstName"] != null && e.NewValues["FirstName"].ToString().Length < 2) { 20 AddError(e.Errors, grid.Columns["FirstName"], "First Name must be at least two characters long."); 21 } 22 if(e.NewValues["LastName"] != null && e.NewValues["LastName"].ToString().Length < 2) { 23 AddError(e.Errors, grid.Columns["LastName"], "Last Name must be at least two characters long."); 24 } 25 if(e.NewValues["Email"] != null && !e.NewValues["Email"].ToString().Contains("@")) { 26 AddError(e.Errors, grid.Columns["Email"], "Invalid e-mail."); 27 } 28 29 int age = 0; 30 int.TryParse(e.NewValues["Age"] == null ? string.Empty : e.NewValues["Age"].ToString(), out age); 31 if(age < 18) { 32 AddError(e.Errors, grid.Columns["Age"], "Age must be greater than or equal 18."); 33 } 34 DateTime arrival = DateTime.MinValue; 35 DateTime.TryParse(e.NewValues["ArrivalDate"] == null ? string.Empty : e.NewValues["ArrivalDate"].ToString(), out arrival); 36 if(DateTime.Today.Year != arrival.Year || DateTime.Today.Month != arrival.Month) { 37 AddError(e.Errors, grid.Columns["ArrivalDate"], "Arrival date is required and must belong to the current month."); 38 } 39 40 if(string.IsNullOrEmpty(e.RowError) && e.Errors.Count > 0) e.RowError = "Please, correct all errors."; 41 } 42 void AddError(Dictionary<GridViewColumn, string> errors, GridViewColumn column, string errorText) { 43 if(errors.ContainsKey(column)) return; 44 errors[column] = errorText; 45 } 46 protected void grid_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e) { 47 bool hasError = e.GetValue("FirstName").ToString().Length <= 1; 48 hasError = hasError || e.GetValue("LastName").ToString().Length <= 1; 49 hasError = hasError || !e.GetValue("Email").ToString().Contains("@"); 50 hasError = hasError || (int)e.GetValue("Age") < 18; 51 DateTime arrival = (DateTime)e.GetValue("ArrivalDate"); 52 hasError = hasError || DateTime.Today.Year != arrival.Year || DateTime.Today.Month != arrival.Month; 53 if(hasError) { 54 e.Row.ForeColor = System.Drawing.Color.Red; 55 } 56 } 57 protected void grid_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e) { 58 if(!grid.IsNewRowEditing) { 59 grid.DoRowValidation(); 60 } 61 } 62 protected void grid_ParseValue(object sender, DevExpress.Web.Data.ASPxParseValueEventArgs e) { 63 if(e.FieldName.Equals("Age")) { 64 int age = 0; 65 if(e.Value == null || !int.TryParse(e.Value.ToString(), out age)) 66 throw new Exception("Age must be an ineteger value."); 67 } 68 } 69 }
【数据汇总】
1 Grid Total<dx:ASPxGridView ID="ASPxGridView4" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3" KeyFieldName="id" OnCustomUnboundColumnData="ASPxGridView4_CustomUnboundColumnData"> 2 <TotalSummary> 3 <dx:ASPxSummaryItem FieldName="company_name" ShowInColumn="company_name" ShowInGroupFooterColumn="company_name" SummaryType="Count" /> 4 <dx:ASPxSummaryItem DisplayFormat="c" FieldName="总计" ShowInColumn="总计" ShowInGroupFooterColumn="总计" SummaryType="Sum" Tag="Sum" /> 5 <dx:ASPxSummaryItem DisplayFormat="c" FieldName="折后" ShowInColumn="折后" ShowInGroupFooterColumn="折后" SummaryType="Sum" /> 6 <dx:ASPxSummaryItem FieldName="总计" ShowInColumn="总计" ShowInGroupFooterColumn="总计" SummaryType="Max" /> 7 <dx:ASPxSummaryItem FieldName="总计" ShowInColumn="总计" ShowInGroupFooterColumn="总计" SummaryType="Min" ValueDisplayFormat="c" /> 8 <dx:ASPxSummaryItem FieldName="unit_price" ShowInColumn="unit_price" ShowInGroupFooterColumn="unit_price" SummaryType="Max" /> 9 <dx:ASPxSummaryItem FieldName="unit_price" ShowInColumn="unit_price" ShowInGroupFooterColumn="unit_price" SummaryType="Min" /> 10 <dx:ASPxSummaryItem FieldName="discount" ShowInColumn="discount" ShowInGroupFooterColumn="discount" SummaryType="Max" /> 11 <dx:ASPxSummaryItem FieldName="discount" ShowInColumn="discount" ShowInGroupFooterColumn="discount" SummaryType="Min" /> 12 </TotalSummary> 13 <Columns> 14 <dx:GridViewDataTextColumn FieldName="id" ReadOnly="True" VisibleIndex="0"> 15 </dx:GridViewDataTextColumn> 16 <dx:GridViewDataTextColumn FieldName="company_name" VisibleIndex="1"> 17 </dx:GridViewDataTextColumn> 18 <dx:GridViewDataTextColumn FieldName="country" VisibleIndex="2"> 19 </dx:GridViewDataTextColumn> 20 <dx:GridViewDataTextColumn FieldName="city" VisibleIndex="3"> 21 </dx:GridViewDataTextColumn> 22 <dx:GridViewDataTextColumn FieldName="unit_price" VisibleIndex="4"> 23 <PropertiesTextEdit DisplayFormatString="c"> 24 </PropertiesTextEdit> 25 </dx:GridViewDataTextColumn> 26 <dx:GridViewDataTextColumn FieldName="quantity" VisibleIndex="5"> 27 </dx:GridViewDataTextColumn> 28 <dx:GridViewDataTextColumn FieldName="discount" VisibleIndex="6"> 29 </dx:GridViewDataTextColumn> 30 <dx:GridViewDataDateColumn FieldName="总计" UnboundType="Decimal" VisibleIndex="7"> 31 <PropertiesDateEdit DisplayFormatString="c"> 32 </PropertiesDateEdit> 33 </dx:GridViewDataDateColumn> 34 <dx:GridViewDataTextColumn FieldName="折后" UnboundType="Decimal" VisibleIndex="8"> 35 <PropertiesTextEdit DisplayFormatString="c"> 36 </PropertiesTextEdit> 37 </dx:GridViewDataTextColumn> 38 </Columns> 39 <Settings ShowFooter ="true" /> 40 41 </dx:ASPxGridView>
年轻过得闲,以后被人嫌...
年轻累不死....