自定义 dataviewcolumn 遇到的一些问题

yiweidianzi 2011-04-09 01:45:07
1,自定义的DataGridViewCell一定要有无参构造函数吗,如果没有在dataGridView1.Columns.Add(new DataGridViewTextButtonColumn(dataGridView1));添加自定义列时 报错:没有为该对象定义无参数的构造函数。
这个是为什么?
2,DataGridViewTextBoxCell和datagridview怎么建立关联,不是在dataGridView1.Columns.Add(new DataGridViewTextButtonColumn(dataGridView1)) 就会自动把dataGridView1和面板关联吗????但是我在DataGridViewTextButtonCell 中使用 this.dataGridView1的值等于 null的

代码:

 #region
public class DataGridViewTextButtonColumn : DataGridViewTextBoxColumn
{
public DataGridViewTextButtonColumn(DataGridView dgv)
{
this.CellTemplate = new DataGridViewTextButtonCell(dgv);

}
}

public class DataGridViewTextButtonCell : DataGridViewTextBoxCell
{
private Bitmap _buttonFace;
private Bitmap _buttonFacePressed;
public event DataGridCellButtonClickEventHandler CellButtonClicked;

DataGridView dgv;
public DataGridViewTextButtonCell()
{
}

public DataGridViewTextButtonCell(DataGridView dgv)
{
try
{
this.dgv = dgv;
System.IO.Stream strm = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication2.buttonface.bmp");
_buttonFace = new Bitmap(@"D:\My Documents\Downloads\DataGridButton\buttonface.bmp");
strm = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication2.buttonfacepressed.bmp");
_buttonFacePressed = new Bitmap(strm);
}
catch { }

}
//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)
protected override void Paint(
Graphics graphics,
Rectangle clipBounds,
Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates cellState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
//base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

//graphics.DrawImage(_img, cellBounds.Location.X + 5, cellBounds.Location.Y + 3, _img.Width, _img.Height);
//(int x, int y, int width, int height)


//Rectangle rert = new Rectangle(cellBounds.X + cellBounds.Width - 15, cellBounds.Y - 2, 10, cellBounds.Height - 2);
//graphics.FillRectangle(new SolidBrush(Color.Red), rert);


//base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
if (this.dgv == null)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
return;
}

if(dgv.CurrentCell.ColumnIndex<=-1) return;
DataGridViewColumn dgvCol = dgv.Columns[dgv.CurrentCell.ColumnIndex];
if (!(dgvCol is DataGridViewTextButtonColumn)) return;
//bool current = parent.IsSelected(rowNum) ||
// (parent.CurrentRowIndex == rowNum &&
// parent.CurrentCell.ColumnNumber == this._columnNum);

//Color BackColor = current ? parent.SelectionBackColor : parent.BackColor;
//Color ForeColor = current ? parent.SelectionForeColor : parent.ForeColor;

Color BackColor = dgv.BackgroundColor;
Color ForeColor = dgv.ForeColor;
//clear the cell
graphics.FillRectangle(new SolidBrush(BackColor), cellBounds);

//draw the value
//string s = this.GetColumnValueAtRow(source, rowNum).ToString();//parent[rowNum, 0].ToString() + ((parent[rowNum, 1].ToString())+ " ").Substring(0,2);
string s = dgv.CurrentCell.Value.ToString();
//Font font = new Font("Arial", 8.25f);
//g.DrawString(s, font, new SolidBrush(Color.Black), bounds);

graphics.DrawString(s, dgv.Font, new SolidBrush(ForeColor), cellBounds);

//draw the button
//Bitmap bm = _pressedRow == rowNum ? this._buttonFacePressed : this._buttonFace;
Bitmap bm = _buttonFace;
graphics.DrawImage(bm, cellBounds.Right - bm.Width, cellBounds.Y);
//font.Dispose();
}
public void HandleMouseUp(object sender, MouseEventArgs e)
{


DataGridView.HitTestInfo hit = this.dgv.HitTest(e.X, e.Y);
//DataGridView.HitTestInfo hit = this.dgv.HitTest(e.X, e.Y);
if (hit.Type != DataGridViewHitTestType.Cell) return;
//if(hit.RowIndex<=-1) return;
//DataGridViewColumn col = this.DataGridView.Columns[hit.ColumnIndex];
if (!(this.dgv.Columns[hit.ColumnIndex] is DataGridViewTextButtonColumn)) return;
Rectangle rect = this.dgv.GetCellDisplayRectangle(hit.ColumnIndex, hit.RowIndex, true);

using (Graphics g = Graphics.FromHwnd(this.dgv.Handle))
{
g.DrawImage(this._buttonFace, rect.Right - this._buttonFace.Width, rect.Y);
if (CellButtonClicked != null)
CellButtonClicked(this, new DataGridCellButtonClickEventArgs(hit.RowIndex, hit.ColumnIndex));
}
}
}
#endregion

...全文
212 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yiweidianzi 2011-04-09
  • 打赏
  • 举报
回复
用了比较麻烦的方法解决了 问题2,显然不是很合理,但是目前没时间去研究,求知道的大侠帮忙

下面解决问题的别扭代码:

public class DataGridViewTextButtonColumn : DataGridViewTextBoxColumn
{
public DataGridViewTextButtonColumn(DataGridView dgv)
{
DataGridViewTextButtonCell dgvtb = new DataGridViewTextButtonCell();
DataGridViewTextButtonCell.dgv = dgv;
this.CellTemplate = dgvtb;
}
}

public class DataGridViewTextButtonCell : DataGridViewTextBoxCell
{
private Bitmap _buttonFace;
private Bitmap _buttonFacePressed;
public event DataGridCellButtonClickEventHandler CellButtonClicked;

public static DataGridView dgv;
public DataGridViewTextButtonCell()
{
try
{
System.IO.Stream strm = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication2.buttonface.bmp");
_buttonFace = new Bitmap(@"D:\My Documents\Downloads\DataGridButton\buttonface.bmp");
strm = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication2.buttonfacepressed.bmp");
_buttonFacePressed = new Bitmap(strm);
}
catch { }
}

//public DataGridViewTextButtonCell(DataGridView dgv)
//{
// try
// {

// this.dgv = dgv;
// System.IO.Stream strm = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication2.buttonface.bmp");
// _buttonFace = new Bitmap(@"D:\My Documents\Downloads\DataGridButton\buttonface.bmp");
// strm = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication2.buttonfacepressed.bmp");
// _buttonFacePressed = new Bitmap(strm);
// }
// catch { }

//}
//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)
protected override void Paint(
Graphics graphics,
Rectangle clipBounds,
Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates cellState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
//base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

//graphics.DrawImage(_img, cellBounds.Location.X + 5, cellBounds.Location.Y + 3, _img.Width, _img.Height);
//(int x, int y, int width, int height)


//Rectangle rert = new Rectangle(cellBounds.X + cellBounds.Width - 15, cellBounds.Y - 2, 10, cellBounds.Height - 2);
//graphics.FillRectangle(new SolidBrush(Color.Red), rert);


//base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
if (dgv == null||dgv.CurrentCell==null)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
return;
}
if(dgv.CurrentCell.ColumnIndex<=-1) return;
DataGridViewColumn dgvCol = dgv.Columns[dgv.CurrentCell.ColumnIndex];
if (!(dgvCol is DataGridViewTextButtonColumn)) return;
//bool current = parent.IsSelected(rowNum) ||
// (parent.CurrentRowIndex == rowNum &&
// parent.CurrentCell.ColumnNumber == this._columnNum);

//Color BackColor = current ? parent.SelectionBackColor : parent.BackColor;
//Color ForeColor = current ? parent.SelectionForeColor : parent.ForeColor;

Color BackColor = dgv.BackgroundColor;
Color ForeColor = dgv.ForeColor;
//clear the cell
graphics.FillRectangle(new SolidBrush(BackColor), cellBounds);

//draw the value
//string s = this.GetColumnValueAtRow(source, rowNum).ToString();//parent[rowNum, 0].ToString() + ((parent[rowNum, 1].ToString())+ " ").Substring(0,2);
string s=null;
if (dgv.CurrentCell.Value == null)
s = "";
else
s = dgv.CurrentCell.Value.ToString();
//Font font = new Font("Arial", 8.25f);
//g.DrawString(s, font, new SolidBrush(Color.Black), bounds);

graphics.DrawString(s, dgv.Font, new SolidBrush(ForeColor), cellBounds);

//draw the button
//Bitmap bm = _pressedRow == rowNum ? this._buttonFacePressed : this._buttonFace;
Bitmap bm = _buttonFace;
graphics.DrawImage(bm, cellBounds.Right - bm.Width, cellBounds.Y);
//font.Dispose();
}
public void HandleMouseUp(object sender, MouseEventArgs e)
{


DataGridView.HitTestInfo hit = dgv.HitTest(e.X, e.Y);
//DataGridView.HitTestInfo hit = this.dgv.HitTest(e.X, e.Y);
if (hit.Type != DataGridViewHitTestType.Cell) return;
//if(hit.RowIndex<=-1) return;
//DataGridViewColumn col = this.DataGridView.Columns[hit.ColumnIndex];
if (!(dgv.Columns[hit.ColumnIndex] is DataGridViewTextButtonColumn)) return;
Rectangle rect = DataGridViewTextButtonCell.dgv.GetCellDisplayRectangle(hit.ColumnIndex, hit.RowIndex, true);

using (Graphics g = Graphics.FromHwnd(dgv.Handle))
{
g.DrawImage(this._buttonFace, rect.Right - this._buttonFace.Width, rect.Y);
if (CellButtonClicked != null)
CellButtonClicked(this, new DataGridCellButtonClickEventArgs(hit.RowIndex, hit.ColumnIndex));
}
}
}
yiweidianzi 2011-04-09
  • 打赏
  • 举报
回复
关键点是 问题2 ,
再我 dataGridView1.Columns.Add(new DataGridViewTextButtonColumn(dataGridView1)后,
为什么在DataGridViewTextButtonCell : DataGridViewTextBoxCell 模板类里用
this.DataGridView 的值是NULL


再dataGridView1.Columns。add(DataGridViewTextButtonColumn)列的时候不就是创建了 DataGridViewTextButtonCell 和DataGridView 关联了吗

如果不是,那怎么设置它们的关联 ??????


就是因为问题2,所以我必须去重载构造函数显示出入dataGridView1给模板类DataGridViewTextButtonCell ,而不能通过this.DataGridView获取其关联的DataGridView




yiweidianzi 2011-04-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kid_wang 的回复:]

C#提供一个默认的无参数构造函数,当实现了另外一个有一个参数的构造函数时候,还想保留这个无参数的构造函数。这样应该写2个构造函数,一旦你实现了一个构造函数,C#就不会再提供默认的构造函数了,所以需要手动实现那个无参数构造函数。
[/Quote]
这个多知道 ,你没仔细看我的问题吧, 我是显示调用用了有参构造函数,就不会去掉其他非静态的构造函数。
可是为什么却还报错:没有为该对象定义无参数的构造函数
dataGridView1.Columns.Add(new DataGridViewTextButtonColumn(dataGridView1))

public DataGridViewTextButtonColumn(DataGridView dgv)
{
this.CellTemplate = new DataGridViewTextButtonCell(dgv);

}
kid_wang 2011-04-09
  • 打赏
  • 举报
回复
C#提供一个默认的无参数构造函数,当实现了另外一个有一个参数的构造函数时候,还想保留这个无参数的构造函数。这样应该写2个构造函数,一旦你实现了一个构造函数,C#就不会再提供默认的构造函数了,所以需要手动实现那个无参数构造函数。
源码下载地址: https://pan.quark.cn/s/a4b39357ea24 【运算单元构造实验报告】运算单元是计算机硬件系统中的关键构成部分,主要承担执行算术运算和逻辑运算的任务。在本次实验中,我们着重探讨了带有累加器的运算单元的设计,涵盖了溢出识别、有符号数值与无符号数值运算的差异性,以及采用补码方式进行的加法与减法运算的实现机制。 一、实验目标 1. 掌握运算单元的基本构造,理解带有累加器的运算单元的具体实现途径。 2. 学习并领会溢出检测的机制,能够设计并构建溢出检测电路,用以判定运算结果是否超出了数据类型的表示范畴。 3. 明辨有符号数值和无符号数值运算的不同特性,把握它们在运算过程中各自的处理方法。 4. 熟练掌握基于补码方式的加法与减法运算的执行,理解补码形式下的溢出判定准则。 5. 熟悉运算单元内部的数据传输路线,明晰数据在运算过程中的流转路径。 6. 设计一个能够支持有符号数值与无符号数值运算、补码加法/减法运算以及有符号数值溢出检测的运算单元电路。 二、实验仪器 采用JZYL—Ⅱ型计算机组成原理实验装置,配备2片74181运算单元芯片作为算术逻辑单元(ALU),2片74LS373用作八位D型锁存器,并辅以一些基础门电路和多路选择器来完成电路设计。 三、实验内容 1. 运用片74181构建一个8位运算单元,负责处理数据的高4位与低4位。 2. 设计并实现溢出检测电路,确保在有符号数值与无符号数值的加法运算中均能准确识别溢出状况。 3. 通过74LS373增加累加器功能,使运算结果得以保存。 4. 将所有设计整合,利用多路选择器来支持有符号数值与无符号数值的加法/减法运算。 四、实验电路 1. 8位运算单元由2片74181构成,通过控制...

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧