GridControlhtml
(1)層次設計器編輯器
有五種視圖模式,banded gridview多行表頭,數據仍是一行一組,最靠近數據的表頭與數據一一對應;advanced banded gridview多行表頭,數據能夠多行一組;cardview 一個卡片是一組數據,其中左側是標題,右側是數據;layout view是card view的集合,佈局能夠自定義ide
(2)視圖函數
視圖的層次結構佈局
視圖屬性:字體
可經過GridControl.Views訪問全部視圖。this
可經過ColumnView.Columns訪問當前視圖的全部列spa
(3)設計器(層次設計器中點擊Run Designer可調出).net
設計器有四大功能:①主要功能的設計;②選擇外觀;③能夠選擇多個視圖,內嵌編輯器的設置;④打印功能的設置設計
① View:
(a)Option:
OptionsBehavior能夠設置行爲(是否容許新增行/是否容許刪除行/是否容許編輯/是否容許展開全部分組)
OptionsCustomization(是否容許排序/分組/過濾/列的移動/列的大小的調整)
OptionsDetail(設置從表的屬性)、OptionsFilter(過濾屬性的設置)OptionsView(顯示或隱藏某些東西,好比標題行)
(b)AppearancePrint : (行高…)
Columns://列的相應的屬性
FeatureBrowser://設置事件,Grid的綁定、列的綁定(有方法提示),什麼均可以設置
Layout:設置佈局
Group Summary Items添加分組統計
② Appearance:
Appearances可設置行的外觀
Format Conditions條件樣式
Style Schemes可設置主題(比較方便)
③ Repository
View Repository設計視圖,與①差很少
In-place Editor Repository內嵌編輯器
④ Printing
(4)gridControl:給該列添加組件(如按鈕) 三種方法
法一:打開設計器(Run Designer),選擇左側Repository,點擊In-place Editor Repository(內嵌編輯器),點擊Add右側的下拉菜單,選擇你想添加的控件,好比ComboBoxEdit,再在右側編輯控件的屬性,ComboBox的話就能夠設置Data下的Items集合,而後點左側Main裏的Columns,點你想在上頭添加控件的列,找右側Data下的ColumnEdit,點最右邊的下拉菜單,點Existing左側的加號,就能找到你剛纔的控件,添加。會默認給該列的每個格都加上這個控件。
法二:點擊你想添加控件的列標題,再點屬性欄中的ColumnEdit,下拉菜單點new,選擇想添加的控件,而後Column Edit左側會出現加號,展開加號,下方會出現許多子屬性,這些都是設置內嵌的控件的屬性的。
法三:直接寫代碼。
② 拖動一個gridControl控件,改變視圖爲BandedGridview,屬性欄中點OptionView,選擇NewItemRowPosition,選Bottom
②添加引用:usingDevExpress.XtraGrid.Views.BandedGrid;
form的構造函數中添加代碼:
BandedGridColumn newColumn = bandedGridView1.Columns.Add() as BandedGridColumn;
newColumn.Caption = "Country";
bandedGridView1.Bands[0].Columns.Add(newColumn);
newColumn.Visible = true;
②(假設添加的控件是計算器)添加引用:usingDevExpress.XtraEditors.Repository;
form的構造函數中繼續添加代碼,效果圖
RepositoryItemCalcEdit calcEdit = new RepositoryItemCalcEdit();
gridControl.RepositoryItems.Add(calcEdit);
newColumn.ColumnEdit = calcEdit;
(5)給gridview添加篩選器
點擊gridview1,再點屬性欄中的OptionsView,下頭有個ShowFilterPanelMode,默認的是不顯示,改爲老是顯示,這樣gridview下方就會出現一個篩選器,能夠根據條件對數據進行篩選,再顯示。
(6)gridview導出爲PDF
能夠在窗體中添加個按鈕,把gridview導出爲PDF並在Adobe Reader中顯示,前提是電腦上安裝的Adobe Reader.
按鈕中添加以下代碼:
DevExpress.XtraGrid.Views.Grid.GridView View = gridControl1.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
if(View != null)
View.ExportToPdf("ShowData.pdf");//pdf的文件名必須是英文
ProcesspdfExport = new Process();
pdfExport.StartInfo.FileName = "AcroRd32.exe";
pdfExport.StartInfo.Arguments = "ShowData.pdf";
pdfExport.Start();
添加引用using System.Diagnostics;
實際效果圖:(列標題沒法顯示,多是漢字字體的問題,圖中默認爲宋體)
(7)鼠標滑過gridview時,鼠標所指行顯示橙色
①添加兩個引用:usingDevExpress.XtraGrid.Views.Grid;
usingDevExpress.XtraGrid.Views.Grid.ViewInfo;
②聲明一個私有成員變量:private inthotTrackRow = DevExpress.XtraGrid.GridControl.InvalidRowHandle;
③寫一個方法: private int HotTrackRow {
get{
returnhotTrackRow;
}
set{
if(hotTrackRow != value)
{
intprevHotTrackRow = hotTrackRow;
hotTrackRow = value;
gridView1.RefreshRow(prevHotTrackRow);
gridView1.RefreshRow(hotTrackRow);
if(hotTrackRow >= 0)
gridControl1.Cursor = Cursors.Hand;
else
gridControl1.Cursor = Cursors.Default;
}
}
}
④ 在gridview的屬性欄中找到事件,添加一個MouseMove事件:
private voidgridView1_MouseMove(object sender, MouseEventArgs e)
{
GridViewview = sender as GridView;
GridHitInfoinfo = view.CalcHitInfo(new Point(e.X, e.Y));
if(info.InRowCell)
HotTrackRow = info.RowHandle;
else
HotTrackRow =DevExpress.XtraGrid.GridControl.InvalidRowHandle;
}
⑤ 在gridview的屬性欄中找到事件,給gridview添加一個RowCellStyle事件:
private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgse)
{
if(e.RowHandle == HotTrackRow)
e.Appearance.BackColor = Color.PaleGoldenrod;
}
效果圖:(鼠標滑過期,鼠標所指行顯示橙色)
(8)gridview中的當前所選框四周加粗,效果圖:
① 添加兩個引用:using System.Reflection;
usingDevExpress.Utils.Paint;
②再寫一個類:
public class MyXPaint : XPaint
{
public overridevoid DrawFocusRectangle(Graphics g, Rectangle r, Color foreColor, ColorbackColor)
{
if(!CanDraw(r)) return;
Brushhb = Brushes.Black;
g.FillRectangle(hb, new Rectangle(r.X,r.Y, 2, r.Height - 2));//left
g.FillRectangle(hb, new Rectangle(r.X,r.Y, r.Width - 2, 2));//top
g.FillRectangle(hb, new Rectangle(r.Right-2,r.Y,2, r.Height-2));//right
g.FillRectangle(hb, new Rectangle(r.X,r.Bottom-2,r.Width, 2));//bottom
}
}
③在當前form的load事件中添加兩行代碼:
FieldInfo fi = typeof(XPaint).GetField("graphics",BindingFlags.Static | BindingFlags.NonPublic);
fi.SetValue(null, new MyXPaint());
(9)gridControl內嵌導航欄(能夠翻頁、添加或刪除行)
點擊gridControl1,點屬性欄中最下方的UseEmbeddedNavigator,選true,gridview下方就會出現內嵌導航欄
刪除行時能夠添加提示按鈕,方法:
添加引用using DevExpress.XtraEditors;
點擊gridControl1,點屬性欄中的事件,雙擊EmbeddedNavigator中的ButtonClick事件,添加代碼:
if(e.Button.ButtonType == NavigatorButtonType.Remove)
{
if (MessageBox.Show("你想刪除當前行麼?", "確認刪除", MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question) != DialogResult.Yes)
e.Handled = true;
}
翻頁:若是隻要翻頁按鈕,能夠把其他按鈕設爲不可見
gridControl1屬性 EmbededNavigator-Buttons—PrevPage和NextPage的visible設爲true,其他的visible全設爲false
(10)右鍵菜單(只能對當前單元格進行復制、粘貼、全選、剪切、操做)
選擇gridView1,在屬性欄的事件中雙擊ShownEditor事件,添加代碼,效果圖:↑
System.Windows.Forms.ContextMenu cm;
private voidgridView1_ShownEditor(object sender, EventArgs e)
{
if(cm == null)
{
InitializeCustomontextMenu();
}
this.bandedGridView1.ActiveEditor.ContextMenu= cm;
}
private voidInitializeCustomontextMenu()
{
cm = newSystem.Windows.Forms.ContextMenu();
cm.MenuItems.Add(new MenuItem("剪切", new EventHandler(OnCut)));
cm.MenuItems.Add(new MenuItem("複製", new EventHandler(OnCopy)));
cm.MenuItems.Add(new MenuItem("粘貼", new EventHandler(OnPaste)));
cm.MenuItems.Add("-");
cm.MenuItems.Add(new MenuItem("全選", new EventHandler(OnSelectAll)));
}
private voidOnCut(object sender, EventArgse)
{
TextEdittextEdit = this. gridView1.ActiveEditor as TextEdit;
if(textEdit != null)
{
textEdit.Cut();
}
}
private voidOnCopy(object sender, EventArgse)
{
TextEdittextEdit = this. gridView1.ActiveEditor as TextEdit;
if(textEdit != null) {
textEdit.Cut();
}
}
private voidOnPaste(object sender, EventArgse)
{
TextEdittextEdit = this. gridView1.ActiveEditor as TextEdit;
if(textEdit != null)
{
textEdit.Paste();
}
}
private voidOnSelectAll(object sender, EventArgs e)
{
TextEdittextEdit = this.gridView1.ActiveEditor as TextEdit;
if(textEdit != null)
{
textEdit.SelectAll();
}
}
(12)對某列進行求和或取最大值最小值,顯示在gridview底部。
gridview1綁定數據後,點擊Run Designer,點Column,右側點Data下的
Symmary Item,Summary Type的選項根據需求來選。而後關閉當前窗口,gridView1的屬性設置頁腳可見(屬性欄中點OptionView,ShowFooter改成True)。該列的頁腳的單元格顏色能夠設置。選擇gridView1事件中的CustomDrawFooterCell,添加以下代碼,再添加引用UsingSystem.Drawing.Drawing2D;
(13)gridView不可編輯
OptionBehavior的Editable改成false
(14)單元格添加顏色,效果圖:
在gridView1的RowCellStyle事件中添加以下代碼:
if (e.RowHandle != bandedGridView1.FocusedRowHandle &&((e.RowHandle % 2 == 0 && e.Column.VisibleIndex % 2 == 1) ||(e.Column.VisibleIndex % 2 == 0 && e.RowHandle % 2 == 1)))
e.Appearance.BackColor = Color.AliceBlue;
(15)gridView按某列標題進行分組(各個組能夠展開收起,分組條件爲用戶的拖拽列,把列標題拖拽到最上方便可),效果圖:
添加引用:using DevExpress.XtraGrid.Views.Base;
在gridView1的FocusedRowChanged事件中添加代碼:
if (gridView1.IsGroupRow(e.FocusedRowHandle))
{
boolexpanded = gridView1.GetRowExpanded(e.FocusedRowHandle);
gridView1.SetRowExpanded(e.FocusedRowHandle,!expanded);
}
(15)根據單元格的值改變其背景色(右圖是將Date列中,值小於2015的單元格染色)
雙擊進入gridView1的CustomDrawCell事件:
private void gridView1_CustomDrawCell(object sender, RowCellCustomDrawEventArgse)
{
if(e.Column.FieldName == "Date")
{
intx = Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle,"Date"));
if(x < 2015)
e.Appearance.BackColor=Color.MistyRose;
}
}
(16)列對齊方式 gridView1.Columns["Date"].AppearanceCell.TextOptions.HAlignment= DevExpress.Utils.HorzAlignment.Near;
//Near左對齊 Center居中對齊 Far右對齊 Default數據默認的對齊方式
(17)gridControl綁定數據源DataTable
出處:https://blog.csdn.net/a462575515/article/details/51378067