嵌套DataGridVIew类实现 [英] Nested DataGridVIew Class Implementation

查看:70
本文介绍了嵌套DataGridVIew类实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我从这篇原帖中得到了代码:
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/0a63a483-5b15-40d6-afb4 -8add6b4f244f

I got the code from this original post: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/0a63a483-5b15-40d6-afb4-8add6b4f244f,

我已经翻译成c #self:

and i had translated into c# myself:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
namespace ProductionSupportLibrary
{
    public class NestedDgvColumn : DataGridViewColumn
    {
        public NestedDgvColumn()
            : base(new NestedDgvCell())
        {
        }
        public override DataGridViewCell CellTemplate
        {
            get { return base.CellTemplate; }
            set
            {
                if ((value != null) && !value.GetType().IsAssignableFrom(typeof(NestedDgvCell)))
                {
                    throw new InvalidCastException("Must be a NestedDgvCell");
                }
                base.CellTemplate = value;
            }
        }
    }
    public class NestedDgvCell : DataGridViewCell
    {
        private DataGridView dgv = new DataGridView();
        private void SetupDGVToDraw()
        {
            dgv.BackgroundColor = SystemColors.Control;
            dgv.Size = new Size(400, 100);
            dgv.AllowUserToAddRows = false;
            dgv.RowHeadersVisible = false;
            dgv.ColumnHeadersVisible = false;
            dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgv.BorderStyle = BorderStyle.None;
            dgv.AutoGenerateColumns = false;
            if ((Value) is DataTable)
            {
                DataTable table = (DataTable)Value;
                dgv.Columns.Clear();
                DataGridViewTextBoxColumn dgvColumn = default(DataGridViewTextBoxColumn);
                foreach (DataColumn column in table.Columns)
                {
                    dgvColumn = new DataGridViewTextBoxColumn();
                    dgvColumn.Name = column.ColumnName;
                    dgvColumn.HeaderText = column.ColumnName;
                    dgvColumn.DefaultCellStyle.Format = "C2";
                    dgvColumn.ValueType = typeof(decimal);
                    dgv.Columns.Add(dgvColumn);
                }
                foreach (DataRow datarow in ((DataTable)Value).Rows)
                {
                    dgv.Rows.Add(datarow.ItemArray);
                }
            }
        }
        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.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds);
            SetupDGVToDraw();
            Bitmap abbreviation = new Bitmap(cellBounds.Width, cellBounds.Height);
            dgv.DrawToBitmap(abbreviation, new Rectangle(0, 0, cellBounds.Width, cellBounds.Height));
            graphics.DrawImage(abbreviation, cellBounds,
                               new Rectangle(0, 0, abbreviation.Width, abbreviation.Height),
                               GraphicsUnit.Pixel);
        }
        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue,
                                                      DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
        }
        public override Type EditType
        {
            get { return typeof(DgvEditingControl); }
        }
        public override System.Type ValueType
        {
            get { return typeof(object); }
            set { }
        }
    }
    public class DgvEditingControl : DataGridView, IDataGridViewEditingControl
    {
        private DataGridView dataGridViewControl;
        private bool valueIsChanged = false;
        private int rowIndexNum;
        public object EditingControlFormattedValue
        {
            get { return this.RowCount; }
            set { }
        }
        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return this.RowCount;
        }
        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            this.ForeColor = dataGridViewCellStyle.ForeColor;
            this.BackgroundColor = dataGridViewCellStyle.BackColor;
        }
        public int EditingControlRowIndex
        {
            get { return rowIndexNum; }
            set { rowIndexNum = value; }
        }
        
        public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
        {
            switch (key & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                case Keys.Enter:
                case Keys.Escape:
                case Keys.Tab:
                    return true;
                default:
                    return false;
            }
        }
        public void PrepareEditingControlForEdit(bool selectAll)
        {
        }
        public bool RepositionEditingControlOnValueChange
        {
            get { return false; }
        }
        public DataGridView EditingControlDataGridView
        {
            get { return dataGridViewControl; }
            set { dataGridViewControl = value; }
        }
        public bool EditingControlValueChanged
        {
            get { return valueIsChanged; }
            set { valueIsChanged = value; }
        }
        public Cursor EditingControlCursor
        {
            get { return base.Cursor; }
        }
        Cursor IDataGridViewEditingControl.EditingPanelCursor
        {
            get { return EditingControlCursor; }
        }
    }
}

我想知道的是如何从Windows窗体中实现这个自定义的嵌套DataGridView类应用程序,换句话说,我该如何使用它。

What i would like to know is how can I implement this customised nested DataGridView class from a windows form application, in other words, how can I use it.

非常感谢。

推荐答案

没人有碰到这件事?


这篇关于嵌套DataGridVIew类实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆