1.初始化
/// <summary>
/// 显示的列名
/// </summary>
public class ColumnsNames
{
public Image Image { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public static List<string> GetColNames()
{
return new List<string>() {
nameof(Image),
nameof(Id),
nameof(Name),
};
}
}
/// <summary>
/// 表格初始化,添加列名
/// </summary>
private void Init()
{
List<string> names = ColumnsNames.GetColNames();
for (int i = 0; i < names.Count; i++)
{
GridColumn column = new GridColumn();
if (i == 0)
{
column.ColumnEdit = new DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit();
((DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit)column.ColumnEdit).SizeMode = PictureSizeMode.Stretch;
((DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit)column.ColumnEdit).PictureAlignment = System.Drawing.ContentAlignment.MiddleCenter;
}
column.Name = names[i];
column.FieldName = names[i];
column.Caption = names[i];
column.AppearanceHeader.Font = new Font("Microsoft Sans Serif", 8.25f);
column.AppearanceHeader.Options.UseFont = true;
column.AppearanceCell.Font = new Font("Microsoft Sans Serif", 8.25f);
column.AppearanceCell.Options.UseFont = true;
column.Visible = true;
column.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;//列内容字体居中
column.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center; //列头文字居中
gridView2.Columns.Add(column);
}
}
/// <summary>
/// 添加数据
/// </summary>
private void SetData()
{
gridControl2.DataSource = null;
List<ColumnsNames> columns = new List<ColumnsNames>();
for (int i = 0; i < 2; i++)
{
ColumnsNames columns1 = new ColumnsNames();
columns1.Image = Resource1._1691132214659;
columns1.Id = i;
columns1.Name = "数字" + i.ToString();
columns.Add(columns1);
}
gridControl2.DataSource = columns;
button1.BackgroundImage = Resource1._1691132214659;
}
2.获取选中行,获取行数据时可以直接转换成ColumnsNames数据使用
/// <summary>
/// 获取选中行(包含非整行返回空)
/// </summary>
/// <param name="isFlagcontrol">true 非普通样本</param>
/// <returns></returns>
public List<ColumnsNames> GetSelectRows()
{
List<ColumnsNames> rows = new List<ColumnsNames>();
if (IsSelectRows())
{
int[] rowHandles = gridView2.GetSelectedRows();
if (rowHandles != null && rowHandles.Length > 0)
{
for (int i = rowHandles.Length - 1; i >= 0; i--)
{
var autoFluoItem = gridView2.GetRow(rowHandles[i]) as ColumnsNames;
if (autoFluoItem == null) continue;
rows.Add(autoFluoItem);
}
}
}
return rows;
}
3.右键自定义隐藏,显示列
private void GridView_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
{
GridViewPopMenuShow(tabPageLibrary, e);
}
private void GridViewPopMenuShow(DevExpress.XtraTab.XtraTabControl xtraTabControl, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
{
DevExpress.XtraGrid.Views.Grid.GridView view = new DevExpress.XtraGrid.Views.Grid.GridView();
List<string> colNames =ColumnsNames.GetColNames();;
if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Column)
{
e.Menu.Items.Clear();
foreach (GridColumn column in view.Columns.Where(r => colNames.Contains(r.Name)))
{
if (column.Visible)
{
DXMenuCheckItem item = new DXMenuCheckItem(column.Caption, true, null, new EventHandler(OnDXMenuCheckItem));
item.Tag = view;
e.Menu.Items.Add(item);
}
else
{
DXMenuCheckItem item = new DXMenuCheckItem(column.Caption, false, null, new EventHandler(OnDXMenuCheckItem));
item.Tag = view;
e.Menu.Items.Add(item);
}
}
}
}
public void OnDXMenuCheckItem(object sender, EventArgs e)
{
DXMenuCheckItem item = sender as DXMenuCheckItem;
DevExpress.XtraGrid.Views.Grid.GridView view = item.Tag as DevExpress.XtraGrid.Views.Grid.GridView;
int columnindex = view.Columns.IndexOf(view.Columns.FirstOrDefault(c => c.Caption == item.Caption && c.Name != nameof(SpectraLibraryItemVM.Check)));
if (columnindex > -1)
{
if (item.Checked)
{
view.Columns[columnindex].Visible = true;
view.Columns[columnindex].VisibleIndex = GetPreVisableIndex(view, columnindex) + 1;
}
else
{
view.Columns[columnindex].Visible = false;
}
}
}
private int GetPreVisableIndex(DevExpress.XtraGrid.Views.Grid.GridView view, int index)
{
int visibleindexpre = 0;
for (int i = index - 1; i > 0; i--)
{
if (view.Columns[i].Visible)
{
visibleindexpre = view.Columns[i].VisibleIndex;
break;
}
}
return visibleindexpre;
}
4.设置过滤文本
gridView.FindFilterText = "'" + searchControl1.Text + "'";
5.右键对单元格复制,粘贴
private void gridView_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
{
DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
List<string> columns =ColumnsNames.GetColNames();
if (e.HitInfo.InRowCell
&& e.HitInfo.Column.Name == nameof(_slVM.Note)
&& (_formType == FormType.Library || _formType == FormType.AddToLibrary)
&& !view.GetSelectedCells().Any(r=>r.Column.Name != nameof(_slVM.Note)))
{
e.Menu.Items.Clear();
DXMenuItem item = new DXMenuItem("Copy", new EventHandler(OnCopy));
item.Tag = e.HitInfo.Column;
e.Menu.Items.Add(item);
item = new DXMenuItem("Paste", new EventHandler(OnPaste));
item.Tag = e.HitInfo.Column;
e.Menu.Items.Add(item);
}
}
private void OnCopy(object sender, EventArgs e)
{
string aa="asd";
Clipboard.SetText(aa);
}
[DllImport("SHCore.dll", SetLastError = true)]
private static extern bool SetProcessDpiAwareness(PROCESS_DPI_AWARENESS awareness);
private void OnPaste(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
try
{
if (gridView2.GetSelectedCells().Length > 0)
{
try
{
SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.PROCESS_DPI_UNAWARE);
}
catch (Exception)
{
}
string strPaste = Clipboard.GetText(TextDataFormat.Text);
string[] strRow = strPaste.TrimEnd('\n').Split('\n');
int i = 0;
foreach (var selectedCell in gridView2.GetSelectedCells())
{
gridView2.SetRowCellValue(selectedCell.RowHandle, selectedCell.Column, strRow[i]);
i++;
if (i == strRow.Length)
{
i = 0;
}
}
}
}
finally { Cursor = Cursors.Default; }
}