dev的grid封装组件,拖拽初始化属性
[ToolboxItem(true)] public partial class UserGridControl : GridControl { public new BaseView MainView { get { return base.MainView; } set { var gridView = value as GridView; if (gridView == null) return; gridView.TopRowChanged += OnTopRowChanged; gridView.CustomDrawRowIndicator += gridView_CustomDrawRowIndicator; gridView.CustomDrawEmptyForeground += gridView1_CustomDrawEmptyForeground; base.MainView = gridView; } } public override BaseView CreateView(string name) { var baseView = base.CreateView(name); if (!(baseView is GridView)) return baseView; var gridView = baseView as GridView; InitView(gridView); return gridView; } public void InitView(GridView gridView) { gridView.Appearance.FocusedRow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); gridView.Appearance.FocusedRow.Options.UseBackColor = true; gridView.Appearance.HideSelectionRow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); gridView.Appearance.HideSelectionRow.Options.UseBackColor = true; gridView.Appearance.SelectedRow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); gridView.Appearance.SelectedRow.Options.UseBackColor = true; gridView.OptionsBehavior.ReadOnly = true; gridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseDownFocused; gridView.OptionsDetail.EnableMasterViewMode = false; gridView.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect; gridView.OptionsSelection.EnableAppearanceFocusedCell = false; //gridView.OptionsSelection.MultiSelect = true; gridView.OptionsView.ShowAutoFilterRow = true; gridView.OptionsView.ShowGroupPanel = false; gridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus; gridView.OptionsPrint.AutoWidth = false; gridView.IndicatorWidth = 30; gridView.TopRowChanged += OnTopRowChanged; gridView.CustomDrawRowIndicator += gridView_CustomDrawRowIndicator; gridView.CustomDrawEmptyForeground += gridView1_CustomDrawEmptyForeground; } public void BestColunm(GridView gridView, bool bestColumn) { gridView.OptionsView.ColumnAutoWidth = !bestColumn; gridView.BestFitColumns(); } private void gridView_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e) { var gridView = (GridView)sender; if (!e.Info.IsRowIndicator || e.RowHandle < 0) return; e.Info.DisplayText = (e.RowHandle + 1).ToString(); e.Info.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far; } private void OnTopRowChanged(object sender, EventArgs e) { GridView view = sender as GridView; if (view == null) return; int width = CalcIndicatorBestWidth(view); if ((view.IndicatorWidth - 4 < width || view.IndicatorWidth + 4 > width) && view.IndicatorWidth != width) { if (width < 30) width = 30; view.IndicatorWidth = width; } } /// <summary> /// 计算行头宽度 /// </summary> /// <param name="sender"></param> /// <returns></returns> private int CalcIndicatorBestWidth(DevExpress.XtraGrid.Views.Grid.GridView view) { Graphics graphics = new Control().CreateGraphics(); SizeF sizeF = new SizeF(); int count = view.TopRowIndex + ((DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo)view.GetViewInfo()).RowsInfo.Count; if (count == 0) { count = 30; } sizeF = graphics.MeasureString(count.ToString(), view.Appearance.Row.Font); return Convert.ToInt32(sizeF.Width) + 20; } 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; } }
/// <summary>
/// 获取刷选后的GridView数据
/// </summary>
public List<T> GetGridViewFilteredAndSortedData<T>(DevExpress.XtraGrid.Views.Grid.GridView view) where T : class
{
var list = new List<T>();
for (int i = 0; i < view.RowCount; i++)
{
if (view.IsGroupRow(i))
continue;
var entity = view.GetRow(i) as T;
if (entity == null)
continue;
list.Add(entity);
}
return list;
}
//public System.Collections.IList GetGridViewFilteredAndSortedData(DevExpress.XtraGrid.Views.Grid.GridView view)
//{
// return view.DataController.GetAllFilteredAndSortedRows();
//}
/// <summary>[/font] /// 获取GridView过滤或排序后的数据集
/// </summary>
/// <param name="view">GridView</param>
/// <returns></returns>
public DataTable GetGridViewFilteredAndSortedDataToDataTable(DevExpress.XtraGrid.Views.Grid.GridView view)
{
DataTable _dt = view.GridControl.DataSource as DataTable;
if (_dt == null)
return null;
DataTable dt = _dt.Clone();
for (int i = 0; i < view.RowCount; i++)
{
if (view.IsGroupRow(i))
continue;
var dr = view.GetDataRow(i);
if (dr == null)
continue;
dt.Rows.Add(dr.ItemArray);
}
return dt;
}
}