dev grid的一些使用
保留选中数据,其他数据删除,不操作数据库
private void butnoremove_Click(object sender, EventArgs e) { int iSelectRowCount = gridView1.SelectedRowsCount; if (iSelectRowCount > 0) { if (MessageBox.Show("你确定只保留选中的记录吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false) == DialogResult.Yes) { for (int i = 0; i < gridView1.DataRowCount; i++) { int handle = gridView1.GetRowHandle(i); if (!gridView1.IsRowSelected(handle)) { gridView1.DeleteRow(handle); i = i - 1; } } } } else { MessageBox.Show("请先选中要保留的记录", "提示"); } }
行样式和格样式的设置
private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) { //GridView View = sender as GridView; //if (e.RowHandle >= 0) //{ // string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["clores"]); // int i = Convert.ToInt32(category); // if (i % 2 == 0) // { // e.Appearance.BackColor = Color.Salmon; // e.Appearance.BackColor2 = Color.SeaShell; // } //} } private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e) { GridView View = sender as GridView; if (e.Column.FieldName == "content") { string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["content"]); } if (e.Column.FieldName == "volumeWeight" || e.Column.FieldName == "weightes") { e.Appearance.ForeColor = Color.YellowGreen;
//e.Appearance.BackColor = Color.FromArgb(199, 237, 204);//护眼色 } if (e.Column.FieldName == "fweight" || e.Column.FieldName == "cfweight") e.Appearance.ForeColor = Color.Coral; } }
导出excel
try { string path = ""; using (SaveFileDialog sfd = new SaveFileDialog()) { sfd.Filter = "Excel文件|*.xls"; if (sfd.ShowDialog() == DialogResult.Cancel) { return; } path += sfd.FileName; } dvginfo.ExportToXls(path); } catch (Exception ex) { MessageBox.Show("请先关闭要替换的文件", "提示"); }
动态创建gridview,导出Excel数据
GridControl CreateGrid(List<marketinformation> pagemodel) { GridControl grid = new GridControl(); ; GridView view = new GridView(); grid.ViewCollection.Add(view); grid.MainView = view; view.GridControl = grid; foreach (GridColumn column in gridView2.Columns) { DevExpress.XtraGrid.Columns.GridColumn gridColumnNumber = view.Columns.AddVisible (column.FieldName.ToString()); gridColumnNumber.Caption = column.Caption.ToString(); gridColumnNumber.FieldName = column.FieldName.ToString(); } grid.DataSource = pagemodel; this.Controls.Add(grid);//重要 grid.ForceInitialize();//重要 BestColumns(view); view.OptionsPrint.AutoWidth = false; return grid; } 然后数据分页创建 IPrintable[] arr(List<marketinformation> data) { List<IPrintable> Printable=new List<IPrintable>(); int PageIndex=0; int PageSize=50000; int CurrentIndex=0; int TotalCount=data.Count; for(PageIndex=0;CurrentIndex<TotalCount;PageIndex++) { var PageData = data.Skip(PageIndex*PageSize).Take(PageSize).ToList(); Printable.Add(CreateGrid(PageData)); CurrentIndex=(PageIndex+1)*PageSize; } return Printable.ToArray(); }
最后数据多的时候自动列宽或假死,可以循环固定列宽,也可以写字段名
void BestColumns(GridView view) { if (chkeds.Checked) { view.OptionsView.ColumnAutoWidth = false; foreach (GridColumn column in view.Columns) { if (column.Visible) { switch (column.FieldName.ToString()) { case "caccount": view.Columns["caccount"].Width = 150; break; case "cunitname": view.Columns["cunitname"].Width = 300; break; case "ddate": view.Columns["ddate"].Width = 150; break; case "cemskind": view.Columns["cemskind"].Width = 100; break; case "cnum": view.Columns["cnum"].Width = 150; break; case "dsysdate": view.Columns["dsysdate"].Width = 150; break; default: view.Columns[column.FieldName].Width = 100; break; } } } } else { view.OptionsView.ColumnAutoWidth = true; foreach (GridColumn column in view.Columns) { if (column.Visible) { view.Columns[column.FieldName].Width = 75; } } } }
按钮上出现提示框
this.toolTipController1.AutoPopDelay = 2000; ToolTipControllerShowEventArgs args = this.toolTipController1.CreateShowArgs(); this.toolTipController1.SetToolTip(this.button1, "请选择一条记录!"); this.toolTipController1.SetTitle(this.button1, "提示"); this.toolTipController1.SetToolTipIconType(this.button1, DevExpress.Utils.ToolTipIconType.Exclamation); this.toolTipController1.ShowBeak = true; this.toolTipController1.ShowShadow = true; this.toolTipController1.Rounded = true; this.toolTipController1.ShowHint("请选择一条记录!", "提示"); args.ToolTip = "请选择一条记录!"; args.Title = "提示"; return;
用的控件是toolTipController
没数据控件显示文本
private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e) { if ((sender as GridView).RowCount == 0) { int width = 300; int height = 60; int font_size = 18; string str = "* 没有查询到数据!\r\n* no data found!"; Font f = new Font("微软雅黑", font_size, FontStyle.Bold); Rectangle r = new Rectangle((e.Bounds.Width - width) / 2, (e.Bounds.Height - height) / 2, width, height); e.Graphics.DrawString(str, f, Brushes.LightBlue, r); e.Handled = true; } }