【例子介绍c#的grid的用法
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Data;
using System.Collections;
using DevExpress.XtraGrid;
using System.Diagnostics;
using DevExpress.Utils.Drawing;
using System.Threading;
using System.Reflection;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using System.Runtime.InteropServices;
using DevExpress.XtraEditors.Repository;
using DevExpress.Utils.Menu;
using System.IO;
using System.Xml.Linq;
using DevExpress.XtraPrinting;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.Utils.Paint;
using System.ComponentModel;
using DevExpress.Utils.Win;
using DevExpress.Data.Filtering;
using System.Drawing.Drawing2D;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
namespace GridSample
{
public partial class Form1 : Form
{
RepositoryItemCheckEdit chkedit = new RepositoryItemCheckEdit();
public Form1()
{
InitializeComponent();
BindingSource bs = new BindingSource();
bs.DataSource = CreateTable(5);
gridControl1.DataSource = bs;
}
private static DataTable CreateTable(int RowCount)
{
DataTable tbl = new DataTable();
tbl.Columns.Add("Name", typeof(string));
tbl.Columns.Add("ID", typeof(int));
tbl.Columns.Add("Number", typeof(int));
tbl.Columns.Add("Date", typeof(DateTime));
tbl.Columns.Add("Select", typeof(bool));
for (int i = 0; i < RowCount; i )
tbl.Rows.Add(new object[] { String.Format("Name{0}", i), i, 3 - i, DateTime.Now.AddDays(i), false });
return tbl;
}
protected void DrawCheckBox(Graphics g, Rectangle r, bool Checked)
{
DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info;
DevExpress.XtraEditors.Drawing.CheckEditPainter painter;
DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;
info = chkedit.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
painter = chkedit.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter;
info.EditValue = Checked;
info.Bounds = r;
info.PaintAppearance.ForeColor = Color.Black;
info.CalcViewInfo(g);
args = new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
painter.Draw(args);
args.Cache.Dispose();
}
private void CView_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column == (sender as GridView).Columns["Select"])
{
e.Info.InnerElements.Clear();
e.Info.Appearance.ForeColor = Color.Blue;
e.Painter.DrawObject(e.Info);
DrawCheckBox(e.Graphics, e.Bounds, getCheckedCount() == gridView1.DataRowCount);
e.Handled = true;
}
}
int getCheckedCount()
{
int count = 0;
for (int i = 0; i < gridView1.DataRowCount; i )
{
if ((bool)gridView1.GetRowCellValue(i, gridView1.Columns["Select"]) == true)
count ;
}
return count;
}
void CheckAll()
{
for (int i = 0; i < gridView1.DataRowCount; i )
{
gridView1.SetRowCellValue(i, gridView1.Columns["Select"], true);
}
}
void UnChekAll()
{
for (int i = 0; i < gridView1.DataRowCount; i )
{
gridView1.SetRowCellValue(i, gridView1.Columns["Select"], false);
}
}
private void view_MouseDown(object sender, MouseEventArgs e)
{
if (e.Clicks == 1 && e.Button == MouseButtons.Left)
{
GridHitInfo info;
Point pt = gridView1.GridControl.PointToClient(Control.MousePosition);
info = gridView1.CalcHitInfo(pt);
if (info.InColumn && info.Column.FieldName == "Select")
{
if (getCheckedCount() == gridView1.DataRowCount)
UnChekAll();
else
CheckAll();
}
}
}
}
}
评论