网上类似的资料很少,大多是6.0的或者是PC机上的。
首先从派生DataGridColumnStyle子类DataGridCheckBoxColumn,这里注意由于DataGridColumnStyle没有父类的成员属性。因此
必须自己添加。代码如下
public class DataGridCheckBoxColumn : DataGridColumnStyle
{
private DataGrid Parent;
public DataGridCheckBoxColumn(DataGrid parent)
{
Parent = parent;
}
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
object cellValue = this.PropertyDescriptor.GetValue(source.List[rowNum]); //取得该列的值
Image image;
g.FillRectangle(backBrush, bounds);
Rectangle tempSrc;
int x;
int y;
if (cellValue == null || cellValue == DBNull.Value || !Convert.ToBoolean(cellValue))
{
tempSrc = new Rectangle(0, 0, Assay.Properties.Resources._unchecked.Width, Assay.Properties.Resources._checkbox.Height);
image = Assay.Properties.Resources._unchecked; //在项目的资源中添加图片。我添加的是PNG,要是没有你使用开发机连接嵌入式设备,添加个CheckBox的程序运行抓图然后用Photoshop切出他的图片。我就是这么做的。不这样编辑的时候感觉显示与编辑时有差别
}
else
{
tempSrc = new Rectangle(0, 0, Assay.Properties.Resources._checkbox.Width, Assay.Properties.Resources._checkbox.Height);
image = Assay.Properties.Resources._checkbox;
}
x = bounds.Left + (bounds.Width - tempSrc.Width) / 2 - 3; //-3:与编辑的CheckBox距离。试试就知道了
y = bounds.Top + (bounds.Height - tempSrc.Height) / 2;
g.DrawImage(image, x, y);
}
上面是显示
接下来说说编辑(这个资料容易找)
定义变量
private List<int> _lstEdit = new List<int>(); //记录编辑的行号
TextBox txtEdit = new TextBox();
CheckBox chkEdit = new CheckBox();
窗体的Load事件
txtEdit.Font = this.Font;
chkEdit.Font = this.Font;
chkEdit.Text = string.Empty;
chkEdit.Width = 22;
chkEdit.Height = 22;
txtEdit.TextChanged += new EventHandler(txtEdit_TextChanged);
chkEdit.CheckStateChanged += new EventHandler(chkEdit_CheckStateChanged);
this.dataGrid1.Controls.Add(txtEdit);
this.dataGrid1.Controls.Add(chkEdit);
txtEdit.Visible = false;
chkEdit.Visible = false;
private void chkEdit_CheckStateChanged(object sender, EventArgs e)
{
if (this.bindingSource1.Current != null)
{
this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber,this.dataGrid1.CurrentCell.ColumnNumber] = (this.chkEdit.Checked ? 1 : 0);
AddEditIndex(this.dataGrid1.CurrentCell.RowNumber);
}
}
private void txtEdit_TextChanged(object sender, EventArgs e)
{
if (this.bindingSource1.Current != null)
{
this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber, this.dataGrid1.CurrentCell.ColumnNumber] = txtEdit.Text;
AddEditIndex(this.dataGrid1.CurrentCell.RowNumber);
}
}
private void AddEditIndex(int colIndex)
{
bool bfind = false;
for (int i = 0; i < _lstEdit.Count; i++)
{
if (_lstEdit[i] == colIndex)
{
bfind = true;
break;
}
}
if (!bfind)
{
_lstEdit.Add(colIndex);
}
}
private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
DataGridCell currentCell = this.dataGrid1.CurrentCell;
object cellValue = this.dataGrid1[currentCell.RowNumber, currentCell.ColumnNumber];
if (currentCell.ColumnNumber == 5)
{
chkEdit.Visible = false;
if (cellValue == null || cellValue == DBNull.Value)
{
txtEdit.Text = string.Empty;
}
else
{
txtEdit.Text = cellValue.ToString();
}
Rectangle cellPos = this.dataGrid1.GetCellBounds(currentCell.RowNumber, currentCell.ColumnNumber);
txtEdit.Left = cellPos.Left;
txtEdit.Top = cellPos.Top;
txtEdit.Width = cellPos.Width;
txtEdit.Height = cellPos.Height;
txtEdit.SelectAll();
txtEdit.Visible = true;
}
else if(currentCell.ColumnNumber == 7 ||
currentCell.ColumnNumber == 9)
{
txtEdit.Visible = false;
if (cellValue == null || cellValue == DBNull.Value || Convert.ToBoolean(cellValue))
{
chkEdit.Checked = false;
}
else
{
chkEdit.Checked = true;
}
Rectangle cellPos = this.dataGrid1.GetCellBounds(currentCell.RowNumber, currentCell.ColumnNumber);
chkEdit.Left = cellPos.Left + (cellPos.Width - chkEdit.Width) / 2;
chkEdit.Top = cellPos.Top + (cellPos.Height - chkEdit.Height) / 2;
chkEdit.Visible = true;
}
}
这样就能编辑,大部分代码都有了。实现看看!
另外还有个问题,滚动条的问题。
移动滚动条,编辑的控件不移动。
偶然发现了变通的解决办法,通过处理Mouse_Down Mouse_Move Mouse_Up来实现滚动条事件。
假如移动滚动条,编辑控件隐藏即可!