ASPxGridview總結(ASPxGridView的增,刪,改,查,資料繫結,外觀顯示,功能設定,分頁)

來源:互聯網
上載者:User

一。ASPXGridView外觀顯示 屬性: Caption----列的標題( KeyFieldName----資料庫欄位 SEOFriendly 是否啟用搜尋引擎最佳化 Summary 指定分頁匯總資訊的格式

Setting節點的ShowFilterRow=True設定快速尋找功能 SettingsBehavior.AllowFocusedRow=true 高亮選中的行,即選中行變色 SettingsBehavior.AllDragDrop=false禁止拖動標題列頭 SettingsBehavior.AllowSort實現列頭點擊後是否可以排序

SettingsPager.ShowEmptyDataRows=True;當資料行為空白時,顯示空行 SettingsPager.PageSize 每頁顯示的記錄總數 AllButton.Text “全部資料顯示”按鈕的文本 AllButton.Visible  是否顯示“全部資料顯示”按鈕 FirstPageBuotton/LastPageButton/NextPageButton/PrevPageButton/ 對應首頁、末頁、下頁、上頁,設定同上。 NumericButtonCount 最小值為1,控制頁碼顯示個數 protected void ASPxGridView1_PageIndexChanged(object sender, EventArgs e)     {         databind();//只需重新綁定資料即可實現上下翻頁     }

建立的列預設是GridViewDataTextColumn類型,選擇工具列的Change To變更列的類型,可以改變新增或修改時的編輯方式。 設定日期類型顯示格式,在“行為”PropertiesDateEdit--DisplayFormatString--例如:{0:yyyy年MM月}

當選擇了show Group Panel時,FocusedRowChanged事件,重綁定資料,使用時先選中行,再查看 protected void ASPxGridView1_FocusedRowChanged(object sender, EventArgs e)     {         getdata();     } 禁止某一列進行編輯,該列的行為-EditFormSettings-Visible=False 代碼中隱藏編輯列的增加,刪除,更新按鈕 (ASPxGridView1.Columns[編輯列] as GridViewCommandColumn).NewButton .Visible= true; (ASPxGridView1.Columns[編輯列] as GridViewCommandColumn).DeleteButton.Visible = true; (ASPxGridView1.Columns[8] as GridViewCommandColumn).UpdateButton .Visible= true; 每行都有一個CHECKBOX,可以動態選擇,只需要這樣即可 <Columns>              <dx:GridViewCommandColumnShowSelectCheckbox ="true"VisibleIndex="8"></dx:GridViewCommandColumn> ....其它列 </Columns> 二。ASPXGridView綁定資料 ASPxGridView1.KeyFieldName = "ID";//指定主鍵。直接更新資料和子表綁定 需要用到

ASPxGridView1.DataSource = dt.defaultView;//指定Grid的資料 ASPxGridView1.DataBind();  //執行綁定 注意,如果查詢結果欄位有別名,編輯該欄位時,UnboundType應設為Object

三。ASPXGridView尋找 過濾資料,尋找資料
方式一、展開欄位標題旁邊的過濾清單過濾資料(類似Excel的過濾方式)grid.Settings.ShowHeaderFilterButton = true;過濾清單列出了該列出現的所有資料。還可以自訂過濾清單的內容,用法參閱:http://demos.devexpress.com/ASPxGridViewDemos/Filtering/HeaderFilter.aspx

方式二、在列頭顯示欄位過濾條件輸入框 grid.Settings.ShowFilterRow = true; 顯示條件判斷方式下拉式清單grid.Settings.ShowFilterRowMenu = true;

 

四刪除資料   protected void ASPxGridView1_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)     {       e.Cancel = true;//否則,只有重新整理頁面才能看到刪除後的結果         int id =Convert.ToInt32( e.Keys[0]);//擷取ID       upd.DelDownFileList(id);//從資料庫刪除記錄     UpLoadFileListBind();//資料表綁定     } ASPxGridView內建的刪除提示,設兩個屬性即可: SettingsBehavior. ==> ConfirmDelete=True SettingsText ==> ConfirmDelete=要提示的字串

五.更新 取值 用e.NewValues[索引] 並且記得更新資料後 ASPxGridView1.CancelEdit();//結束編輯狀態         e.Cancel = true;         bind(); 例子: //更新     protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)     {         Bill.Message m = new Bill.Message();

        //取值 用e.NewValues[索引]         string id = Convert.ToString(e.Keys[0]);         string event_date = e.NewValues[1].ToString();         string event_title = e.NewValues[2].ToString();         string event_description = e.NewValues[3].ToString();         string tag = e.NewValues[4].ToString();         m.Message_update(id, event_date, event_title, event_description, tag);                ASPxGridView1.CancelEdit();//結束編輯狀態         e.Cancel = true;         bind();     }

六排序 在BeforeColumnSortingGrouping事件中重新綁定一下資料

七.分頁 PageIndexChanged事件裡重新綁定一下資料

--------------------------------------------------------------------------------

1.動態添加非資料繫結列,例子:動態添加行號列 if (!IsPostBack)         {             //動態添加行號非綁定資料             GridViewDataTextColumn dl = new GridViewDataTextColumn();             dl.Caption = "行號";             dl.FieldName = "hh";//該資料行繫結的欄位hh             dl.UnboundType = DevExpress.Data.UnboundColumnType.String;//非資料繫結類型為字元型             dl.PropertiesTextEdit.DisplayFormatString = "c";//顯示字元的格式             dl.PropertiesTextEdit.FocusedStyle.ForeColor = System.Drawing.Color.Red;             dl.VisibleIndex = 0;//設定行號列的索引位置             ASPxGridView1.Columns.Insert(0, dl);//把行號列插入0之前                                   getdata();             ASPxGridView1.Caption = "IP為"+GetClientIP()+"的使用者,正在查看網銀終端更新內容";            } 在CustomUnboundColumnData事件中 protected void ASPxGridView1_CustomUnboundColumnData(object sender, ASPxGridViewColumnDataEventArgs e)     {         if (e.Column.FieldName == "hh" && e.IsGetData)             e.Value = (e.ListSourceRowIndex + 1).ToString();     } 2.ASPxComboBox列的相關操作 簡單的方法是 1.FiledName寫主表與此欄位有關聯外鍵欄位:例如uid 2.在PropertiesCombobox下面找這幾個屬性: 然後在客戶姓名的這一列的DataSourceId,給它綁定上我們字表的ObjectDataSource 在TextField設定欄位名稱,例如:name 在ValueField設定名稱應該就是字表的主鍵(也就是主表引用字表的外鍵),例如:uid 這樣就可以輕鬆做到,不用寫代碼,綁定多張表 手寫代碼的方法來綁定ASPxComboBox 在aspx中將該列的-行為-PropertiesComboBox-ValuesType設為System.String避免ComboBox出現無法選中的現象 using DevExpress.Web.ASPxGridView; using DevExpress.Web.ASPxEditors;

//在頁面載入時,給combox列賦值,這裡的workgroupID是在ASPxGridview中的Combox資料行繫結的欄位         (ASPxGridView1.Columns["WorkgroupID"] as GridViewDataComboBoxColumn).PropertiesComboBox.ValueType = typeof(int);                    fangqm.Netbank.Core.groupInfo group = new fangqm.Netbank.Core.groupInfo();             DataTable dt = group.groupSelectAll();//table             for (int i = 0; i < dt.Rows.Count; i++)             {                 int id = Convert.ToInt32(dt.Rows[i][0]);                 string v= dt.Rows[i][1].ToString();                 (ASPxGridView1.Columns["WorkgroupID"] as GridViewDataComboBoxColumn).PropertiesComboBox.Items.Add(new ListEditItem(v, id));                           } 在表格進行更新,添加操作時,e.NewValues[1])即可取到用戶端的值,例如: model.WorkgroupID = Convert.ToInt32(e.NewValues[1]); 注意應先呈現COMBOX列,後綁定資料,欄位綁定是區分大小寫,要和SELECT語句欄位名一模一樣 3.資料匯總 匯總計算aspxgridview的所有行求得平均或總和並顯示在頁尾。

當settings.showfooter屬性設定為True,才顯示頁尾。

aspxgridview的匯總條目是放在totalsummary屬性裡。設定DisplayFormat例如:總計{0]台終端,

設定FieldName為非綁定欄位,SummaryType設為Sum表示計算這一列的和

4.隱藏編輯列,在DataBound事件中 protected void ASPxGridView1_DataBound(object sender, EventArgs e)     {         if(ASPxGridView1.VisibleRowCount>0)         {             //ASPxGridView1.Columns[命令列索引]             (ASPxGridView1.Columns[4] as GridViewCommandColumn).NewButton.Visible = false;         }     }

--------------------------------------------------------------------------------

六。AspxGridView常見問題 A.點Edit或new按鈕,Delete出來Update和cancel,編輯完資料後點擊Update,出錯:“不支援所指定的方法”.解決方案: 1、確保ASPxGridView已設定了KeyFieldName 2、確保ASPxGridView已定義了事件 OnRowDeleting, OnRowInserting, OnRowUpdating 3、後台代碼中有對 OnRowDeleting, OnRowInserting, OnRowUpdating 事件的處理。

2、 綁定主從表(IList)

List的元素帶有List屬性(Category.Products),並且需要以Grid嵌套的方式顯示。

1、  選中GridView(gird1),右鍵菜單選擇“編輯模板”—“DetailRow”,頁面開啟詳細資料介面,向DetailRow添加一個新的ASPxGridView (grid2)顯示詳細資料,可以設定grid2的Columns相關屬性。Grid2.SettingsDetail.IsDetailGrid = true 指定grid2作為從表資料表格。

2、  增加grid2 DataBinding事件  

Code protected void grid2_DataBinding(object sender, EventArgs e)     {         DevExpress.Web.ASPxGridView.ASPxGridView grid = sender as DevExpress.Web.ASPxGridView.ASPxGridView;         if ((grid != null) && (dict != null))         {                     int i = (int) grid.GetMasterRowKeyValue();/*取主表記錄的Key,主表grid必須設定KeyFieldName*/             if (i >= 0)             {                             grid.DataSource = dict.Products;//通過Key定位元據,指定子表資料來源           }         } }

3、  右鍵點擊DetailRow,選“結束模板編輯”。修改grid1.SettingsDetail的相關屬性

Bool AllowOnlyOneMasterRowExpanded 預設False,是否只允許主表一行展開。True後展開第二行明細記錄時,會關閉上次展開的明細記錄。

Bool ShowDetailButton 是否顯示明細按鈕,True顯示一個“+”在行首

Bool ShowDetailRow True顯示詳細資料

4、 過濾資料

方式一、展開欄位標題旁邊的過濾清單過濾資料(類似Excel的過濾方式) grid.Settings.ShowHeaderFilterButton = true;過濾清單列出了該列出現的所有資料。還可以自訂過濾清單的內容,用法參閱:http://demos.devexpress.com/ASPxGridViewDemos/Filtering/HeaderFilter.aspx

方式二、在列頭顯示欄位過濾條件輸入框 grid.Settings.ShowFilterRow = true; 顯示條件判斷方式下拉式清單grid.Settings.ShowFilterRowMenu = true;

 

5、 使用者自訂欄顯示

Grid.SettingCustomizationWindow

Enabled 運行自訂欄顯示

PopupHorizontalAlign 列編輯窗水平對齊

PopupVerticalAlign 列編輯窗垂直對齊

 

    通過JavaScript開啟列編輯框。

 

Code <script. type="text/javascript">

<%-- 變更按鈕的標題 --%> function UpdateCustomizationWindowValue() {     var element = document.getElementById("btnCustWindow");     if(element == null) return;     element.value = (grid.IsCustomizationWindowVisible() ? "Hide" : "Show") + " Customization Window"; } <%-- 顯示自訂欄編輯窗--%> function ShowHideCustomizationWindow() {     if(grid.IsCustomizationWindowVisible())         grid.HideCustomizationWindow();     else grid.ShowCustomizationWindow();           UpdateCustomizationWindowValue(); } </script> <input id="btnCustWindow" type="button" value="Show Customization Window" nclick="ShowHideCustomizationWindow();"/>

<dxwgv:ASPxGridView ID="gird"…………………….> ………………………….. <ClientSideEvents CustomizationWindowCloseUp="function(s, e) { UpdateCustomizationWindowValue(); }" /> </dxwgv:ASPxGridView>

 

小技巧: AspxGridView在綁定資料的時候,如果資料列的類型是日期型,這時應該用“GridViewDataColumn”而不應該用“GridViewDataTextColumn”,這樣的話,資料顯示出來的格式就如“1900-01-01",而不是"1900-01-01 0:00:00".

AspxGridView中尋找控制項不能像GridView中一樣用FindControl,而應該用 FindRowCellTemplateControl方法。

應該徹底放棄Response.Write()方法來輸出字元中,如果頁面中調用了Response.Write方法,會導致AspxGridView的用戶端排序功能失去控制項,具體表現為:點擊排序時,顯示loading,然後就沒有反應了,一直loading下去,不會完成排序操作。

利用ASPxGridViewExporter匯出aspxgridview中的資料時,如果有資料列,雖然我們可以利用 <PropertiesDateEdit DisplayFormatString="{0:yyyy-MM-dd}">

  </PropertiesDateEdit>來格式化aspxgridview的日期列顯示樣式,但是我們卻無法控制利用ASPxGridViewExporter匯出後的excel檔案中的樣式,這時,匯出的excel檔案中,日期列的顯示格式為資料庫中aspxgridview資料來源中日期列的格式。所以如果想控制匯出後excel日期列的格式,必須從aspxgridview的資料來源下手,在資料來源中格式化日期列,就可以達到格式化匯出後excel檔案日期列格式的目的了 四:匯出ASPxGridView的資料 添加一個ASPxGridViewExporter控制項到Page,設定GridViewID為需要匯出資料的aspxgridview,調用以下方法實現匯出。 ASPxGridViewExporter1.WriteXlsToResponse() ASPxGridViewExporter1.WriteCvsToResponse() ASPxGridViewExporter1.WritePdfToResponse() ASPxGridViewExporter1.WriteRtfToResponse() 關於匯出EXCEL日期格式,操作EXCEL,設定儲存格為相應的格式即可 1.遍曆主鍵列中的所有資料.

List<object> keyValue = AspxGridView1.GetSelectedFieldValues("kid");

string str;

foreach(object key in keyValue)

{

    str = key.ToString(); } 2.動態選擇某一行.

//n為要選擇的行數

AspxGridView1.Selection.SetSelection(n,true);

3.擷取所有選擇行中的資料.這裡要注意去這裡查看 http://space.itpub.net/?uid-23109131-action-viewspace-itemid-676010

 

4.問題:當AspxGridView的儲存格中包含超連結按鈕時,AspxGridView排序後引起超連結錯位.

解決方案:設定超連結按鈕的屬性  EnableViewState=false;

5.根據某行的資料,動態設定選擇複選框不可編輯,及動態設定行背景色.

protected void AspxGridView1_HtmlRowPrepared(object sender,ASPxGridViewTableRowEventArgs e)

{

      //判斷bomcode是否為空白.

  if(e.GetValue("bomcode").ToString().Trim() == "")

    {

          //設定選擇複選框不可編輯  

          e.Row.Cell[0].Enabled = false;

          //將背景色設定為淺灰色

    e.Row.BackColor = Color.LightGray;

    } }

//表中有個連結地址,實現點連結地址下載檔案,引用命名空間DevExpress.Web.ASPxGridView;     //如果不想動態綁定,只需要設定列的FieldName為連結址址欄位,TextField為顯示連結名稱的欄位即可     protected void ASPxGridView1_Init(object sender, EventArgs e)     {         GridViewDataHyperLinkColumn colLink = new GridViewDataHyperLinkColumn();//執行個體化一個超連結列         colLink.Caption = "下載吧";//設定列頭         colLink.PropertiesHyperLinkEdit.Text = "這是個連結";//顯示連結的名稱         colLink.PropertiesHyperLinkEdit.TextField = "LinkName";//顯示連結名稱要綁定的欄位               colLink.FieldName = "LinkURL";//該資料行繫結的欄位         colLink.PropertiesHyperLinkEdit.NavigateUrlFormatString="{0}";//連結地址就是該資料行繫結的欄位

        colLink.Visible = true;         colLink.Width = 200;         ASPxGridView1.Columns.Add(colLink);//把該列添加到ASPxGridview

    }

using System.Collections.Generic;

//取得當前控制項值的集合 直接尋找控制項的ID

List <object> keyValues = this.GridViewmethod.GetSelectedFieldValues("F_XXX");//控制項的ID

foreach (object key in keyValues)//迴圈遍曆出來

{

}

2.在AspGridView取得某一行

List <object> keyValues = this.GridViewmethod.GetCurrentPageRowValues("F_xxxxx");//F_xxxxx是主鍵的值

foreach (object key in keyValues)//迴圈遍曆這一行的每一列的資料

{

}

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/fangqm/archive/2011/03/06/6227805.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.