using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing;
using WindowsControlLibrary1;
namespace WindowsApplication1
{
public class MyDataGridViewCell :
//System.Windows.Forms.DataGridViewTextBoxCell
System.Windows.Forms.DataGridViewCell
{
public MyDataGridViewCell(): base(){}
public override Type FormattedValueType
{
get{ return typeof(MyUserControl); }
}
public MyUserControl MyControl
{
get { return this.DataGridView.EditingControl as MyUserControl; }
}
public override Type EditType
{
get{ return typeof(MyUserControl); }
}
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
value = MyControl;
return value;
}
protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);
graphics.FillRectangle(cellBackground, cellBounds);
cellBackground.Dispose();
if (MyControl != null)
{
foreach (Control c in MyControl.Controls)
{
Rectangle rt = cellBounds;
rt.X += c.Location.X;
rt.Y += c.Location.Y;
rt.Width = c.Width;
rt.Height = c.Height;
if (c is TextBox)
TextBoxRenderer.DrawTextBox(graphics, rt, c.Text, c.Font, TextBoxState.Normal);
if (c is Button)
ButtonRenderer.DrawButton(graphics, rt, c.Text, c.Font, false, PushButtonState.Normal);
if (c is Label)
TextRenderer.DrawText(graphics, c.Text, c.Font, rt.Location, c.ForeColor);
}
}
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}
public override Type ValueType
{
get { return typeof(MyUserControl); }
}
}
}