- 积分
- 33
- 在线时间
- 53 小时
- 主题
- 28
- 注册时间
- 2013-8-31
- 帖子
- 132
- 最后登录
- 2019-10-21
- 帖子
- 132
- 软币
- 3320
- 在线时间
- 53 小时
- 注册时间
- 2013-8-31
|
由于有些项目中使用的是DevExpress.XtraGrid.Views.BandedGrid.AdvBandedGridView来做的;代码如下
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Nbjjy.Utilities
{
public partial class CustomColumnDialogEx : DevExpress.XtraEditors.XtraForm
{
private DataTable dt;
private GridBandCollection cols;
public CustomColumnDialogEx(GridBandCollection cols)
{
InitializeComponent();
this.cols = cols;
this.GridColumn();
this.gridControl1.DataSource = this.dt;
}
private void GridColumn()
{
this.dt = new DataTable();
this.dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("Selected", typeof(bool)),
new DataColumn("Caption", typeof(string))
});
foreach (GridBand gridBand in cols)
{
DataRow dataRow = this.dt.NewRow();
dataRow["Selected"] = gridBand.Visible;
dataRow["Caption"] = gridBand.Caption;
this.dt.Rows.Add(dataRow);
}
}
private void btnAllSelected_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.dt.Rows.Count; i++)
{
dt.Rows[i]["Selected"] = true;
}
this.gridView1.UpdateCurrentRow();
}
private void btnUnSelected_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.dt.Rows.Count; i++)
{
dt.Rows[i]["Selected"] = false;
}
this.gridView1.UpdateCurrentRow();
}
private void btnResume_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.dt.Rows.Count; i++)
{
dt.Rows[i]["Selected"] = true;
}
this.gridView1.UpdateCurrentRow();
base.DialogResult = DialogResult.Abort;
base.Close();
}
private void btnOK_Click(object sender, EventArgs e)
{
for (int num = 0; num < this.cols.Count; num++)
{
this.cols[num].Visible = Convert.ToBoolean(((bool)this.dt.Rows[num]["Selected"]) ? num : -1);
this.cols[num].Caption = this.dt.Rows[num]["Caption"].ToString();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
base.DialogResult = DialogResult.Abort;
base.Close();
}
}
}
后台页面
namespace Nbjjy.Utilities
{
partial class CustomColumnDialogEx
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnAllSelected = new DevExpress.XtraEditors.SimpleButton();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnResume = new DevExpress.XtraEditors.SimpleButton();
this.gridControl1 = new DevExpress.XtraGrid.GridControl();
this.gridView1 = new DevExpress.XtraGrid.Views.Grid.GridView();
this.gridColumn1 = new DevExpress.XtraGrid.Columns.GridColumn();
this.gridColumn2 = new DevExpress.XtraGrid.Columns.GridColumn();
this.btnUnSelected = new DevExpress.XtraEditors.SimpleButton();
this.btnOK = new DevExpress.XtraEditors.SimpleButton();
this.btnCancel = new DevExpress.XtraEditors.SimpleButton();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.gridControl1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.gridView1)).BeginInit();
this.SuspendLayout();
//
// btnAllSelected
//
this.btnAllSelected.Location = new System.Drawing.Point(318, 19);
this.btnAllSelected.Name = "btnAllSelected";
this.btnAllSelected.Size = new System.Drawing.Size(75, 23);
this.btnAllSelected.TabIndex = 1;
this.btnAllSelected.Text = "全选(&A)";
this.btnAllSelected.Click += new System.EventHandler(this.btnAllSelected_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnResume);
this.groupBox1.Controls.Add(this.gridControl1);
this.groupBox1.Controls.Add(this.btnAllSelected);
this.groupBox1.Controls.Add(this.btnUnSelected);
this.groupBox1.Controls.Add(this.btnOK);
this.groupBox1.Controls.Add(this.btnCancel);
this.groupBox1.Location = new System.Drawing.Point(9, 6);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(406, 246);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
//
// btnResume
//
this.btnResume.Location = new System.Drawing.Point(318, 67);
this.btnResume.Name = "btnResume";
this.btnResume.Size = new System.Drawing.Size(75, 23);
this.btnResume.TabIndex = 3;
this.btnResume.Text = "还原(&R)";
this.btnResume.Click += new System.EventHandler(this.btnResume_Click);
//
// gridControl1
//
this.gridControl1.Location = new System.Drawing.Point(12, 19);
this.gridControl1.MainView = this.gridView1;
this.gridControl1.Name = "gridControl1";
this.gridControl1.Size = new System.Drawing.Size(283, 215);
this.gridControl1.TabIndex = 0;
this.gridControl1.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] {
this.gridView1});
//
// gridView1
//
this.gridView1.ColumnPanelRowHeight = 20;
this.gridView1.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
this.gridColumn1,
this.gridColumn2});
this.gridView1.FixedLineWidth = 1;
this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None;
this.gridView1.GridControl = this.gridControl1;
this.gridView1.HorzScrollVisibility = DevExpress.XtraGrid.Views.Base.ScrollVisibility.Never;
this.gridView1.Name = "gridView1";
this.gridView1.OptionsCustomization.AllowFilter = false;
this.gridView1.OptionsCustomization.AllowSort = false;
this.gridView1.OptionsDetail.EnableMasterViewMode = false;
this.gridView1.OptionsMenu.EnableColumnMenu = false;
this.gridView1.OptionsMenu.EnableFooterMenu = false;
this.gridView1.OptionsView.ColumnAutoWidth = false;
this.gridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;
this.gridView1.OptionsView.ShowGroupPanel = false;
this.gridView1.OptionsView.ShowIndicator = false;
this.gridView1.RowHeight = 20;
//
// gridColumn1
//
this.gridColumn1.AppearanceHeader.Options.UseTextOptions = true;
this.gridColumn1.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
this.gridColumn1.Caption = "选择";
this.gridColumn1.FieldName = "Selected";
this.gridColumn1.Name = "gridColumn1";
this.gridColumn1.OptionsColumn.AllowMove = false;
this.gridColumn1.OptionsColumn.AllowSize = false;
this.gridColumn1.Visible = true;
this.gridColumn1.VisibleIndex = 0;
this.gridColumn1.Width = 40;
//
// gridColumn2
//
this.gridColumn2.AppearanceHeader.Options.UseTextOptions = true;
this.gridColumn2.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
this.gridColumn2.Caption = "列名";
this.gridColumn2.FieldName = "Caption";
this.gridColumn2.Name = "gridColumn2";
this.gridColumn2.OptionsColumn.AllowMove = false;
this.gridColumn2.OptionsColumn.AllowSize = false;
this.gridColumn2.Visible = true;
this.gridColumn2.VisibleIndex = 1;
this.gridColumn2.Width = 220;
//
// btnUnSelected
//
this.btnUnSelected.Location = new System.Drawing.Point(318, 43);
this.btnUnSelected.Name = "btnUnSelected";
this.btnUnSelected.Size = new System.Drawing.Size(75, 23);
this.btnUnSelected.TabIndex = 2;
this.btnUnSelected.Text = "全清(&L)";
this.btnUnSelected.Click += new System.EventHandler(this.btnUnSelected_Click);
//
// btnOK
//
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.Location = new System.Drawing.Point(318, 187);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(75, 23);
this.btnOK.TabIndex = 4;
this.btnOK.Text = "确定(&O)";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(318, 211);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 23);
this.btnCancel.TabIndex = 5;
this.btnCancel.Text = "取消(&C)";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// CustomColumnDialog
//
this.AcceptButton = this.btnOK;
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(424, 258);
this.ControlBox = false;
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "CustomColumnDialogEx";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "自定义列";
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.gridControl1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private DevExpress.XtraEditors.SimpleButton btnAllSelected;
private System.Windows.Forms.GroupBox groupBox1;
private DevExpress.XtraEditors.SimpleButton btnResume;
private DevExpress.XtraGrid.GridControl gridControl1;
private DevExpress.XtraGrid.Views.Grid.GridView gridView1;
private DevExpress.XtraEditors.SimpleButton btnUnSelected;
private DevExpress.XtraEditors.SimpleButton btnOK;
private DevExpress.XtraEditors.SimpleButton btnCancel;
private DevExpress.XtraGrid.Columns.GridColumn gridColumn1;
private DevExpress.XtraGrid.Columns.GridColumn gridColumn2;
}
}
调用方法: new CustomColumnDialogEx(this.advBandedGridView1.Bands).ShowDialog();
|
|