干脆我将代码copy过来:namespace DataGridSelectColumn { using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private ColumnSelectionDataGrid dataGrid1; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // DataTable MyTable = new DataTable("MyTable"); MyTable.Columns.AddRange(new DataColumn[] {new DataColumn("Col1"), new DataColumn("Col2"), new DataColumn("Col3"), new DataColumn("Col4"), new DataColumn("Col5"), new DataColumn("Col6")}); MyTable.Rows.Add(new object[] {"Blah11", "Blah12","Blah13", "Blah14","Blah15", "Blah15"}); MyTable.Rows.Add(new object[] {"Blah21", "Blah22","Blah23", "Blah24","Blah25", "Blah25"}); MyTable.Rows.Add(new object[] {"Blah31", "Blah32","Blah33", "Blah34","Blah35", "Blah35"}); dataGrid1.DataSource = MyTable; //add a tablestyle with default columnstyles CurrencyManager cm = (CurrencyManager) this.BindingContext[dataGrid1.DataSource]; DataGridTableStyle dgts = new DataGridTableStyle(cm);
//clear out the default colstyles so we can add our derived ones dgts.GridColumnStyles.Clear(); for(int i = 0; i < MyTable.Columns.Count; i++) { DataGridSelectionColumnStyle colStyle = new DataGridSelectionColumnStyle(i, this.dataGrid1); colStyle.MappingName = MyTable.Columns[i].ColumnName; colStyle.HeaderText = MyTable.Columns[i].ColumnName; dgts.GridColumnStyles.Add(colStyle); } dataGrid1.TableStyles.Add(dgts); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (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.dataGrid1 = new ColumnSelectionDataGrid(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(32, 32); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(344, 184); this.dataGrid1.TabIndex = 0; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(416, 261); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.dataGrid1}); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] public static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) {
} } public class ColumnSelectionDataGrid : DataGrid { private ArrayList _colSelections; public ColumnSelectionDataGrid() { this.AllowSorting = false; _colSelections = new ArrayList();
} private void ClearRowSelections() { for(int i = 0; i < this.ListManager.Count; ++i) if(this.IsSelected(i)) this.UnSelect(i); }// instead of using a derived columnstyle to handle drawing the selection, // you could just override the grid Paint method and draw the selection here. // But doing so, would require drawing the text as well as the selection // rectangle. Below just draws the rectangle. You could use a alpha blend selection // color to avoid having to draw the text in this case (just letting the text bleed thru). // protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe) // { // Console.WriteLine("OnPaint"); // base.OnPaint(pe); // foreach(int i in ColSelections) // { // Rectangle top = this.GetCellBounds(0, i); // Rectangle bottom = this.GetCellBounds(this.ListManager.Count, i); // // top = Rectangle.Union(top, bottom); // top.Intersect(pe.ClipRectangle); // // pe.Graphics.FillRectangle(new SolidBrush(this.SelectionBackColor), top); // // } // // } } public class DataGridSelectionColumnStyle : DataGridTextBoxColumn { private ArrayList _Selections; private Color _selectionForeColor; private Color _selectionBackColor; private int _column; public DataGridSelectionColumnStyle(int column, ColumnSelectionDataGrid grid) { _Selections = grid.ColSelections; _selectionForeColor = grid.SelectionForeColor; _selectionBackColor = grid.SelectionBackColor; _column = column; } protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight) { if(this._Selections.Contains(this._column)) { backBrush = new SolidBrush(this._selectionBackColor); foreBrush = new SolidBrush(this._selectionForeColor); } base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight); } } }
不能再指条明路??
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data; /// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private ColumnSelectionDataGrid dataGrid1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
// DataTable MyTable = new DataTable("MyTable");
MyTable.Columns.AddRange(new DataColumn[] {new DataColumn("Col1"),
new DataColumn("Col2"),
new DataColumn("Col3"),
new DataColumn("Col4"),
new DataColumn("Col5"),
new DataColumn("Col6")});
MyTable.Rows.Add(new object[] {"Blah11", "Blah12","Blah13", "Blah14","Blah15", "Blah15"});
MyTable.Rows.Add(new object[] {"Blah21", "Blah22","Blah23", "Blah24","Blah25", "Blah25"});
MyTable.Rows.Add(new object[] {"Blah31", "Blah32","Blah33", "Blah34","Blah35", "Blah35"});
dataGrid1.DataSource = MyTable; //add a tablestyle with default columnstyles
CurrencyManager cm = (CurrencyManager)
this.BindingContext[dataGrid1.DataSource];
DataGridTableStyle dgts = new DataGridTableStyle(cm);
//clear out the default colstyles so we can add our derived ones
dgts.GridColumnStyles.Clear();
for(int i = 0; i < MyTable.Columns.Count; i++)
{
DataGridSelectionColumnStyle colStyle = new DataGridSelectionColumnStyle(i, this.dataGrid1);
colStyle.MappingName = MyTable.Columns[i].ColumnName;
colStyle.HeaderText = MyTable.Columns[i].ColumnName;
dgts.GridColumnStyles.Add(colStyle);
} dataGrid1.TableStyles.Add(dgts);
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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.dataGrid1 = new ColumnSelectionDataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(32, 32);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(344, 184);
this.dataGrid1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(416, 261);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.dataGrid1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
}
} public class ColumnSelectionDataGrid : DataGrid
{
private ArrayList _colSelections; public ColumnSelectionDataGrid()
{
this.AllowSorting = false;
_colSelections = new ArrayList();
} public ArrayList ColSelections
{
get{return _colSelections;}
}
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{ Point p1 = new Point(e.X, e.Y);
bool shift = (Control.ModifierKeys & Keys.Shift) == Keys.Shift;
bool ctrl = (Control.ModifierKeys & Keys.Control) == Keys.Control;
HitTestInfo hti = this.HitTest(p1);
if( hti.Type == HitTestType.RowHeader && !shift && !ctrl)
{
this.ColSelections.Clear();
this.Invalidate();
}
else if( hti.Type == HitTestType.ColumnHeader)
{
if(shift && this.ColSelections.Count > 0)
{
int last = (int) this.ColSelections[this.ColSelections.Count - 1];
if(hti.Column > last)
for(int i = last + 1; i <= hti.Column; ++i)
this.ColSelections.Add(i);
else if(hti.Column < last)
for(int i = last + 1; i >= hti.Column; --i)
this.ColSelections.Add(i);
}
else if(ctrl)
{
if(this.ColSelections.Contains(hti.Column))
this.ColSelections.Remove(hti.Column);
else
this.ColSelections.Add(hti.Column); }
else
{
this.ColSelections.Clear();
ClearRowSelections();
this.ColSelections.Add(hti.Column);
}
this.Invalidate();
return;
}
else if(this.ColSelections.Count > 0 && !ctrl && !shift)
{
this.ColSelections.Clear();
this.Invalidate();
}
base.OnMouseDown(e); if(!shift && !ctrl && this.IsSelected(this.CurrentRowIndex))
{
if (this.ColSelections.Count > 0)
{
this.ColSelections.Clear();
this.Invalidate();
}
}
} private void ClearRowSelections()
{
for(int i = 0; i < this.ListManager.Count; ++i)
if(this.IsSelected(i))
this.UnSelect(i);
}// instead of using a derived columnstyle to handle drawing the selection,
// you could just override the grid Paint method and draw the selection here.
// But doing so, would require drawing the text as well as the selection
// rectangle. Below just draws the rectangle. You could use a alpha blend selection
// color to avoid having to draw the text in this case (just letting the text bleed thru).
// protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
// {
// Console.WriteLine("OnPaint");
// base.OnPaint(pe);
// foreach(int i in ColSelections)
// {
// Rectangle top = this.GetCellBounds(0, i);
// Rectangle bottom = this.GetCellBounds(this.ListManager.Count, i);
//
// top = Rectangle.Union(top, bottom);
// top.Intersect(pe.ClipRectangle);
//
// pe.Graphics.FillRectangle(new SolidBrush(this.SelectionBackColor), top);
//
// }
//
// }
} public class DataGridSelectionColumnStyle : DataGridTextBoxColumn
{
private ArrayList _Selections;
private Color _selectionForeColor;
private Color _selectionBackColor;
private int _column; public DataGridSelectionColumnStyle(int column, ColumnSelectionDataGrid grid)
{
_Selections = grid.ColSelections;
_selectionForeColor = grid.SelectionForeColor;
_selectionBackColor = grid.SelectionBackColor;
_column = column;
}
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
{
if(this._Selections.Contains(this._column))
{
backBrush = new SolidBrush(this._selectionBackColor);
foreBrush = new SolidBrush(this._selectionForeColor);
} base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
}
}
}