DevExpress的GridControl的使用以及怎样添加列和绑定数据源
場(chǎng)景
Winform控件-DevExpress18下載安裝注冊(cè)以及在VS中使用:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100061243
在上面搭建好DevExpress的環(huán)境后,要使用其GridControl控件。
注:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號(hào)
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費(fèi)下載
實(shí)現(xiàn)
首先在窗體中拖拽一個(gè)GridControl
?
然后在窗體的Load時(shí)事件中對(duì)其進(jìn)行添加列和樣式設(shè)置
private void FrmSearch_Load(object sender, EventArgs e){//設(shè)置GridControl樣式Common.GridControl.GridControlHelper.SetStyles(this.gridControl1.MainView as DevExpress.XtraGrid.Views.Base.ColumnView);//訂閱行點(diǎn)擊事件this.gridView1.RowClick += gridView1_RowClick;}進(jìn)入設(shè)置樣式的方法
public static void SetStyles(DevExpress.XtraGrid.Views.Base.ColumnView view){if (view is DevExpress.XtraGrid.Views.Grid.GridView){DevExpress.XtraGrid.Views.Grid.GridView gridView = view as DevExpress.XtraGrid.Views.Grid.GridView;gridView.OptionsView.ShowGroupPanel = false;????????????????????????????????????????????? //隱藏最上面的GroupPanelgridView.OptionsView.ShowIndicator = false;?????????????????????????????????????????????? //隱藏指示列g(shù)ridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None;?????????? //設(shè)置焦點(diǎn)框?yàn)檎術(shù)ridView.OptionsSelection.EnableAppearanceFocusedCell = false;????????????????????????????????? //禁用單元格焦點(diǎn)gridView.OptionsSelection.EnableAppearanceFocusedRow = true;??????????????????????????????????? //啟用整行焦點(diǎn)gridView.OptionsSelection.EnableAppearanceFocusedRow = true;??????????????????????????????????? //啟用整行焦點(diǎn)gridView.OptionsSelection.EnableAppearanceHideSelection = false;gridView.OptionsView.EnableAppearanceEvenRow = true;??????????????????????????????????????????? //啟用偶數(shù)行背景色gridView.OptionsView.EnableAppearanceOddRow = true;???????????????????????????????????????????? //啟用奇數(shù)行背景色//gridView.Appearance.EvenRow.BackColor = System.Drawing.Color.FromArgb(150, 237, 243, 254);????? //設(shè)置偶數(shù)行背景色//gridView.Appearance.OddRow.BackColor = System.Drawing.Color.FromArgb(150, 199, 237, 204);?????? //設(shè)置奇數(shù)行背景色//gridView.Appearance.FocusedRow.BackColor = System.Drawing.Color.Red;//gridView.Appearance.SelectedRow.BackColor = System.Drawing.Color.Red;}//禁用自動(dòng)生成列view.OptionsBehavior.AutoPopulateColumns = false;//禁用自動(dòng)列寬if (view is DevExpress.XtraGrid.Views.Grid.GridView){(view as DevExpress.XtraGrid.Views.Grid.GridView).OptionsView.ColumnAutoWidth = false;}//禁用數(shù)據(jù)過濾面板view.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;#region 添加列view.Columns.Clear();int index = 0;DevExpress.XtraGrid.Columns.GridColumn col = null;col = new DevExpress.XtraGrid.Columns.GridColumn();col.FieldName = "DBName";col.Caption = "數(shù)據(jù)庫名";col.Width = 200;col.VisibleIndex = index++;view.Columns.Add(col);col = new DevExpress.XtraGrid.Columns.GridColumn();col.FieldName = "ShortNodeText";col.Caption = "文件名";col.Width = 200;col.VisibleIndex = index++;view.Columns.Add(col);col = new DevExpress.XtraGrid.Columns.GridColumn();col.FieldName = "CreateDate";col.Caption = "創(chuàng)建日期";col.Width = 130;col.VisibleIndex = index++;view.Columns.Add(col);col = new DevExpress.XtraGrid.Columns.GridColumn();col.FieldName = "TaskFile";col.Caption = "任務(wù)文件";col.Width = 180;col.VisibleIndex = index++;view.Columns.Add(col);col = new DevExpress.XtraGrid.Columns.GridColumn();col.FieldName = "FullPath";col.Caption = "完整路徑";col.Width = 180;col.VisibleIndex = index++;view.Columns.Add(col);col = new DevExpress.XtraGrid.Columns.GridColumn();col.FieldName = "Barcode";col.Caption = "電池條碼";col.Width = 180;col.VisibleIndex = index++;view.Columns.Add(col);#endregionSetAllowEdit(view, false);????????????????????????????????????????? //禁用編輯SetAllowSort(view, DevExpress.Utils.DefaultBoolean.False);????????? //禁用排序SetAllowFilter(view, false);??????????????????????????????????????? //禁用數(shù)據(jù)過濾}在上面方法中進(jìn)行樣式的設(shè)置以及列的添加
注意在添加列時(shí)FieldName 屬性要與將來設(shè)置數(shù)據(jù)源時(shí)的字段一致。
然后上面的禁用編輯的方法
public static void SetAllowEdit(DevExpress.XtraGrid.Views.Base.ColumnView view, bool isAllow){foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns){col.OptionsColumn.AllowEdit = isAllow;}}禁用排序的方法
public static void SetAllowSort(DevExpress.XtraGrid.Views.Base.ColumnView view, DevExpress.Utils.DefaultBoolean value){foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns){col.OptionsColumn.AllowSort = value;}}禁用數(shù)據(jù)過濾的方法
?public static void SetAllowFilter(DevExpress.XtraGrid.Views.Base.ColumnView view, bool isAllow){foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns){col.OptionsFilter.AllowAutoFilter = isAllow;col.OptionsFilter.AllowFilter = isAllow;}}初始化完樣式和添加列后就要設(shè)置數(shù)據(jù)源
首先新建一個(gè)實(shí)體對(duì)象,對(duì)象要有與上面添加列時(shí)FieldName 所對(duì)應(yīng)的屬性。
下面是部門字段和屬性,其他省略
?public class DataTreeNode{private string id;private string parentId;private string nodeText;private string createDate;private string fullPath;private string taskFile;private string barcode;private DataTreeNodeTypes nodeType = DataTreeNodeTypes.Folder;/// <summary>/// 去掉擴(kuò)展名的數(shù)據(jù)文件完整路徑/// </summary>public string Id{get { return id; }set { id = value; }}/// <summary>/// 父級(jí)節(jié)點(diǎn)的Id/// </summary>public string ParentId{get { return parentId; }set { parentId = value; }}/// <summary>/// 數(shù)據(jù)文件名稱/// </summary>public string NodeText{get { return nodeText; }set { nodeText = value; }}}構(gòu)建數(shù)據(jù)源
List<DataTreeNode> data = new List<DataTreeNode>(); data = DataTreeListHelper.ParseDir(Common.Global.AppConfig.TestDataDir, data); var result = data.Where(p => p.NodeType = = DataTreeNodeTypes.File);首先聲明上面實(shí)體對(duì)象的List,然后使用ParseDir方法將文件目錄進(jìn)行遞歸查詢。
然后進(jìn)行篩選出文件類型。
然后可以直接設(shè)置數(shù)據(jù)源
this.gridControl1.DataSource = result;?
?
總結(jié)
以上是生活随笔為你收集整理的DevExpress的GridControl的使用以及怎样添加列和绑定数据源的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#中使用Path、Directory、
- 下一篇: ZedGraph曲线图实现X轴与Y轴可拖