当前位置:编程学习 > C#/ASP.NET >>

自定义 dataviewcolumn 遇到的一些问题

     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

       --------------------编程问答-------------------- C#提供一个默认的无参数构造函数,当实现了另外一个有一个参数的构造函数时候,还想保留这个无参数的构造函数。这样应该写2个构造函数,一旦你实现了一个构造函数,C#就不会再提供默认的构造函数了,所以需要手动实现那个无参数构造函数。 --------------------编程问答--------------------
引用 1 楼 kid_wang 的回复:
C#提供一个默认的无参数构造函数,当实现了另外一个有一个参数的构造函数时候,还想保留这个无参数的构造函数。这样应该写2个构造函数,一旦你实现了一个构造函数,C#就不会再提供默认的构造函数了,所以需要手动实现那个无参数构造函数。

这个多知道 ,你没仔细看我的问题吧, 我是显示调用用了有参构造函数,就不会去掉其他非静态的构造函数。
可是为什么却还报错:没有为该对象定义无参数的构造函数
dataGridView1.Columns.Add(new DataGridViewTextButtonColumn(dataGridView1))

            public DataGridViewTextButtonColumn(DataGridView dgv)
            {
                this.CellTemplate = new DataGridViewTextButtonCell(dgv);
                 
            } --------------------编程问答-------------------- 关键点是 问题2 ,
再我 dataGridView1.Columns.Add(new DataGridViewTextButtonColumn(dataGridView1)后,
为什么在DataGridViewTextButtonCell : DataGridViewTextBoxCell  模板类里用
this.DataGridView 的值是NULL


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

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


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




--------------------编程问答-------------------- 用了比较麻烦的方法解决了 问题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));
                }
            }
        }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,