将多个GridControl导出到一个Excel文件中
public static bool ToExcelDev(List lstGridControl)
{
SaveFileDialog dig = new SaveFileDialog();
dig.Filter = ExcelHelper.FILTER_EXCEL;
if (dig.ShowDialog() == DialogResult.Cancel)
return false;
try
{
CompositeLink complink = new CompositeLink(new PrintingSystem());
foreach (GridControl item in lstGridControl)
{
if (item.DataSource != null)
{
foreach (GridView gridView in item.Views)
{
gridView.OptionsPrint.AutoWidth = false;
}
PrintableComponentLink link = new PrintableComponentLink();
link.Component = item;
complink.Links.Add(link);
}
}
complink.CreatePageForEachLink();
complink.ExportToXlsx(dig.FileName, new XlsxExportOptions() { ExportMode = XlsxExportMode.SingleFilePageByPage, TextExportMode = TextExportMode.Text });
MessageBox.Show(ValidateHelper.GetText("Success", "成功导出到Excel!"), ValidateHelper.GetText("Alert", "提示"));
return true;
}
catch (Exception er)
{
MessageBox.Show(er.Message, ValidateHelper.GetText("Alert", "提示"));
return false;
}
}
注意:用户电脑上必须安装Office2007或以上的版本,否则查看到导出的文件是空的。
注意:导出前需将GridView.OptionsPrintAutoWidth=false;,否则导出后数据挤在一起。
advBandedGridView1.OptionsPrint.AutoWidth = false;
多文档documentManager1,的tabbedView视图中,更改文档Tab显示顺序
tabbedView1.Controller.Move((currentDoc as DevExpress.XtraBars.Docking2010.Views.Tabbed.Document), 0);
将gridView列中的某行的编辑控件设置为空
private DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit EmptyEditor;
private void InitEmptyEditor()
{
EmptyEditor = new DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit();
EmptyEditor.Buttons.Clear();
EmptyEditor.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.HideTextEditor;
gridControl1.RepositoryItems.Add(EmptyEditor);
new DevExpress.XtraGrid.Design.XViewsPrinting(gridControl1);
}
private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
if (e.Column.FieldName == SELECTFIELDNAME)
{
if (IsMachineRow(e.RowHandle) == false)
{
e.RepositoryItem = EmptyEditor;
}
}
}
去掉spinEdit文本框中的小数点
spinEdit1.Properties.IsFloatValue = false;
解决gridView1编辑时光标未离开单元格就直接点保存后,最新数据无法写入数据库。
//在保存前提交编辑数据
gridView1.PostEditor();
启用了奇偶行样式 gridView1.OptionsView.EnableAppearanceEvenRow = true;后,gridView1_RowStyle事件中的e.Appearance.BackColor属性不启作用,可以通过以下方法变向解决。
private void ConditionsAdjustment()
{
StyleFormatCondition cn;
cn = new StyleFormatCondition(FormatConditionEnum.GreaterOrEqual, dgvPO.Columns["Status"], null, 1);
cn.ApplyToRow = true;
cn.Appearance.BackColor = Color.Gray;
dgvPO.FormatConditions.Add(cn);
// 删除线
cn = new StyleFormatCondition(FormatConditionEnum.Equal, dgvPO.Columns["Status"], null, -1);
cn.ApplyToRow = true;
cn.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Strikeout);
cn.Appearance.ForeColor = SystemColors.ControlDark;
dgvPO.FormatConditions.Add(cn);
//
}
gridView垂值滚动条事件和值
void gvFacility_TopRowChanged(object sender, EventArgs e)
{
int index= gvFacility.TopRowIndex;
}
自定义绘制列标题CustomDrawColumnHeader为CheckBox
private void FrmAllPlan_Load(object sender, EventArgs e)
{
var itemIndex = gcResult.RepositoryItems.Add(new RepositoryItemCheckEdit());
var columnEdit = gcResult.RepositoryItems[itemIndex] as RepositoryItemCheckEdit;
columnEdit.ValueChecked = true;
columnEdit.ValueUnchecked = false;
ColIsSelected.ColumnEdit = columnEdit;
//dgvResult.CustomDrawColumnHeader+=dgvResult_CustomDrawColumnHeader;
}
private void dgvResult_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column == null)
return;
if (e.Column.AbsoluteIndex != 0)
return;
Rectangle rect = e.Bounds;
rect.Inflate(-1, -1);
e.Info.InnerElements.Clear();
e.Painter.DrawObject(e.Info);
DrawCheckBox(e.Graphics, rect, e.Column.ColumnEdit as RepositoryItemCheckEdit, IsAllSelected());
e.Handled = true;
}
void DrawCheckBox(Graphics g,Rectangle r,RepositoryItemCheckEdit checkEdit,bool isCheck)
{
DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info = default(DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo);
DevExpress.XtraEditors.Drawing.CheckEditPainter painter = default(DevExpress.XtraEditors.Drawing.CheckEditPainter);
DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args = default(DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs);
info = (CheckEditViewInfo)checkEdit.CreateViewInfo();
painter = (CheckEditPainter)checkEdit.CreatePainter();
info.EditValue = isCheck;
info.Bounds = r;
info.CalcViewInfo(g);
args = new ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
painter.Draw(args);
args.Cache.Dispose();
}
private void dgvResult_MouseUp(object sender, MouseEventArgs e)
{
if(e.Button== System.Windows.Forms.MouseButtons.Left && e.Clicks==1)
{
GridView gridview = sender as GridView;
if(gridview!=null)
{
GridHitInfo hitinfo = gridview.CalcHitInfo(e.Location);
if(hitinfo.InRow==false && hitinfo.InColumn==true)
{
SetAllCheckBox(!IsAllSelected());
//if (AffectCheckBoxChange != null)
// AffectCheckBoxChange(this, null);
}
}
this.dgvResult.RefreshData();
DXMouseEventArgs args = DXMouseEventArgs.GetMouseArgs(e);
args.Handled = true;
}
}
private void SetAllCheckBox(bool p)
{
Model.BindCollection lstRe = gcResult.DataSource as Model.BindCollection;
if (lstRe != null)
{
foreach (ReportData model in lstRe)
{
model.IsSelected = p;
}
}
gcResult.RefreshDataSource();
}
private bool IsAllSelected()
{
bool result = true;
Model.BindCollection lstRe = gcResult.DataSource as Model.BindCollection;
if (lstRe != null)
{
foreach (ReportData model in lstRe)
{
if (model.IsSelected == false)
{
result = false;
break;
}
}
}
else
{
result = false;
}
return result;
}
按条件合并单元格
private void gvSupplierReplyOfShortage_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e)
{
SupplierReplyOfShortageInfo model1 = gvSupplierReplyOfShortage.GetRow(e.RowHandle1) as SupplierReplyOfShortageInfo;
SupplierReplyOfShortageInfo model2 = gvSupplierReplyOfShortage.GetRow(e.RowHandle2) as SupplierReplyOfShortageInfo;
if (e.Column.FieldName == colTotalShortageMatOfPlanHasBroughtAmount.FieldName || e.Column.FieldName == colUnit.FieldName)
{
if (model1.MatName==model2.MatName)
{
e.Merge = true;
}
else
{
e.Merge = false;
}
e.Handled = true;//必须设置为true,否则合并不起作用。
}
}
定位到指定行(排序后也能正确定位)
注意:列的值必须能唯一确定一行
gvPO.FocusedRowHandle = gvPO.LocateByValue(0, colWOCode, model.Code);
在GridView中输入整数,以%格式显示,数据库中存放的为实际数值。
this.colEfficiency.DisplayFormat.FormatString = "{0:n}%";
this.colEfficiency.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
注意:实际值在显示时要先*100,而在保存到数据库之前要/100
在TextEdit中输入整数,以%格式显示
this.textEdit1.Properties.Mask.EditMask = "p";
this.textEdit1.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
this.textEdit1.Properties.Mask.UseMaskAsDisplayFormat = true;
编辑“选择”列时,点击鼠标立即钩选或取消钩选
//编辑“选择”列时,点击鼠标立即钩选或取消钩选
gridView1.OptionsBehavior.EditorShowMode = EditorShowMode.MouseDown;
得到表格过滤后的数据行
private void allSelectedToolStripMenuItem_Click(object sender, EventArgs e)
{
IList list= gridView1.DataController.GetAllFilteredAndSortedRows();
foreach(var item in list)
{
DataRowView dataView = (item as DataRowView);
dataView.Row[colIsSelected.FieldName] = true;
}
gridView1.RefreshData();
}
GridView全部展开或收宿
///
/// 展开全部行
///
///
public static void ExpandAllRows(this GridView view)
{
view.BeginUpdate();
try
{
int dataRowCount = view.DataRowCount;
for (int rHandle = 0; rHandle < dataRowCount; rHandle++)
view.SetMasterRowExpanded(rHandle, true);
}
finally
{
view.EndUpdate();
}
}
gvCapacityAllocation.CollapseAllDetails();