using System;
//DataGridコントロールには動的にスクロールを変化させるメソッドがないため、
//継承して新しいコントロールの作成
//It succeeds and DataGrid control is created.

public class CustomDataGrid : System.Windows.Forms.DataGrid 
{
	private System.Windows.Forms.Control mControl;

	public CustomDataGrid(){
		mControl=this;
		this.Scroll += new EventHandler(this.CustomDataGrid_Scroll);
		this.Paint += new System.Windows.Forms.PaintEventHandler(this.CustomDataGrid_Paint);
	}


	public bool headerLine = false;

	//DataGridの先頭に表示される行を設定する
	//The line displayed at the head of DataGrid is set up.
	public void SetTopRow(int rowNum){
		GridVScrolled(this, new System.Windows.Forms.ScrollEventArgs(System.Windows.Forms.ScrollEventType.LargeIncrement, rowNum));
	}

	//DataGridの先頭に表示される行を取得する
	public int GetTopRow(){
		return VertScrollBar.Value;
	}

	//横向きのスクロールを設定する
	//Sideways scrolling is set up.
	public void HScrollSet(int rowNum){
		GridHScrolled(this, new System.Windows.Forms.ScrollEventArgs(System.Windows.Forms.ScrollEventType.LargeIncrement, rowNum));
	}
	//DataGridの横スクロールを取得します
	//Horizontal scrolling of DataGrid is acquired.
	public System.Windows.Forms.ScrollBar getHScroll(){
		return HorizScrollBar;
	}
	//DataGrid内のセルでタブキーを押された時に、ほかのコントロールにフォーカスを移動します
	//When a tab key is pushed in the cell in DataGrid, a focus is moved to other control.


	public System.Windows.Forms.Control NextControl{
		get{
			return mControl;
		}
		set{
			mControl = value;
		}
	}

	const int WM_KEYDOWN  = 0x100;
	protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData){
		if( msg.Msg == WM_KEYDOWN ){
			if (keyData == System.Windows.Forms.Keys.Tab){
				mControl.Focus();
				return true;
			}else{
				return base.ProcessCmdKey(ref msg,keyData);
			}
		}
	return base.ProcessCmdKey(ref msg,keyData);
	}

	//ヘッダーに線を引くことができないため自分で描画します。
	private void CustomDataGrid_Paint(Object sender,System.Windows.Forms.PaintEventArgs e){
		if(headerLine){
			int height = this.Font.Height + 5;
			int width = 0 - getHScroll().Value;
			int i  = 0;

			for(i = 0;i<=this.Controls.Count - 3;i++){
				width += this.TableStyles[0].GridColumnStyles[i].Width;

				//縦線ラインを引きます
				e.Graphics.DrawLine(new System.Drawing.Pen(this.TableStyles[0].GridLineColor),new System.Drawing.PointF(width - 1, 0),new System.Drawing.PointF(width - 1, height));
			}
			//横線ラインを引きます
			e.Graphics.DrawLine(new System.Drawing.Pen(this.TableStyles[0].GridLineColor),new System.Drawing.PointF(0, height),new System.Drawing.PointF(width - 1, height));
		}
	}

	private void CustomDataGrid_Scroll(Object sender,System.EventArgs e){
		this.Refresh();
	}

	protected override void OnMouseWheel(System.Windows.Forms.MouseEventArgs e){
		base.OnMouseWheel(e);
		base.Focus();
	}
}



public class ColorDataGridTextBoxColumn : System.Windows.Forms.DataGridTextBoxColumn
{
	//Paintメソッドをオーバーライドする
	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(base.DataGridTableStyle.DataGrid.CurrentCell.RowNumber != rowNum){
			foreBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
			backBrush = new System.Drawing.SolidBrush(System.Windows.Forms.Form.DefaultBackColor);
		}
		base.Paint(g, bounds, source, rowNum,backBrush, foreBrush, alignToRight);
	}
}


▲トップページ