win8 网站模板,好看的模板,怎么创建属于自己的网站,怎么手动安装网站程序实现原因 因为现有的DataGridView控件无法支持在单元格DataGridViewCell上显示多个甚至是多种类型的控件
实现效果 效果1 效果2 实现代码
核心代码实现
using System;
using System.Drawing;
using System.Windows.Forms;namespace DataGridViewCellExtLib
{/// summary…实现原因 因为现有的DataGridView控件无法支持在单元格DataGridViewCell上显示多个甚至是多种类型的控件
实现效果 效果1 效果2 实现代码
核心代码实现
using System;
using System.Drawing;
using System.Windows.Forms;namespace DataGridViewCellExtLib
{/// summary/// DataGridViewCell扩展类/// /summarypublic static class DataGridViewCellExtension{/// summary/// 添加Panel控件到DataGridView/// /summary/// param namecellDataGridViewCell对象/param/// param namedataGridViewDataGridView对象/param/// returns返回已指定好大小位置添加到DataGridView控件的Panel对象/returns/// exception crefArgumentNullException/// 如果cell为Null抛出ArgumentNullException异常/// 如果dataGridView为Null抛出ArgumentNullException异常/// /exceptionpublic static Panel AddPanelControl(this DataGridViewCell cell, DataGridView dataGridView){if (cell null){throw new ArgumentNullException(nameof(cell));}if (dataGridView null){throw new ArgumentNullException(nameof(dataGridView));}var panel new Panel{//默认Panel颜色为透明BackColor Color.Transparent};//添加控件到DataGridViewdataGridView.Controls.Add(panel);//获取单元格的矩形范围Rectangle rectangle dataGridView.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, false);//设置Panel的大小panel.Size new Size(){Width rectangle.Width,Height rectangle.Height};//显示Panelpanel.Visible true;//设置Panel位置panel.Location new Point(rectangle.Left, rectangle.Top);//将Panel和DataGridViewCell绑定在一起panel.Tag new TagPlusDataGridViewCell{Binding cell};cell.Tag new TagPlusPanel{Binding panel};return panel;}/// summary/// 从DataGridView中移除Panel控件/// /summary/// param namecellDataGridViewCell对象/param/// param namedataGridViewDataGridView对象/param/// exception crefArgumentNullException/// 如果cell为Null抛出ArgumentNullException异常/// 如果dataGridView为Null抛出ArgumentNullException异常/// /exceptionpublic static void RemovePanelControl(this DataGridViewCell cell, DataGridView dataGridView){if (cell null){throw new ArgumentNullException(nameof(cell));}if (dataGridView null){throw new ArgumentNullException(nameof(dataGridView));}var panel ((TagPlusPanel)cell.Tag).Binding;//移除Panel控件dataGridView.Controls.Remove(panel);panel.Dispose();}/// summary/// 重新在DataGridView控件中设置Panel位置/// /summary/// param namecellDataGridViewCell对象/param/// param namedataGridViewDataGridView对象/param/// exception crefArgumentNullException/// 如果cell为Null抛出ArgumentNullException异常/// 如果dataGridView为Null抛出ArgumentNullException异常/// /exceptionpublic static void ResetPanelControlLocation(this DataGridViewCell cell, DataGridView dataGridView){if (cell null){throw new ArgumentNullException(nameof(cell));}if (dataGridView null){throw new ArgumentNullException(nameof(dataGridView));}var tagPlus (TagPlusPanel)cell.Tag;var panel tagPlus.Binding;//获取单元格的矩形范围Rectangle rectangle dataGridView.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, false);//设置Panel位置panel.Location new Point(rectangle.Left, rectangle.Top);}}/// summary/// Tag扩展类/// /summary/// typeparam nameT需要绑定的数据类型/typeparampublic class TagPlusT{/// summary/// 需要绑定的元素/// /summarypublic T Binding { get; set; }/// summary/// 将Control.Tag属性保留到当前属性上/// /summarypublic object Tag { get; set; }}
}程序代码地址下载
https://gitee.com/GandalfGao/cell-with-control.git
设计思路
1. 为什么以DataGridViewCell的扩展类方式实现
从界面的实现角度来看控件是添加到单元格中的实际却不是这样的添加显示的控件实际和DataGridViewCell一点关系都没有它们也没有父级和子级的关系添加的控件只和DataGridView控件有父级和子级的关系。因此我想设计一个这样的扩展方法使其开发起来就好像是直接对DataGridViewCell操作一样 比如添加控件虽然实际是往DataGridView控件上做添加但是开发者在用扩展方法时就仿佛是在对DataGridViewCell做添加一样这样的话在感官上显得更加直接
2. 为什么添加到DataGridView的都是只是一个Panel控件 a. 是为了化繁为简 我需要设计一个root的控件用这个控件来承接多个甚至是多个不同种类的子控件。这样的设计的好处有两点 第一对于DataGridViewCell来说只需要面对这一个root控件即可它不需要再关注root控件里面究竟设计了什么样的控件它们的位置是在哪里。它只需要对一个控件做好负责那就是这个root控件只要设计好这个root的控件大小和其位置就可以了大小是单元格的大小位置是单元格的位置 第二设计这个root控件之后子控件的位置就是相对root控件位置来设置了不需要再相对DataGridView控件位置来设置。 b. 为什么选择Panel做为这个root控件 因为子控件在Panel控件中布局更加简单和自由
3. TagPlus类是做什么用的 简单来说Control类中带有Tag属性TagPlus可以理解为是Tag属性的扩展版 那为什么需要这个类来做为Tag的扩展呢 这就得提到我其中的一个设计思路。由于DatGridViewCell控件和Panel控件之间没有关联关系而是DataGridView控件和Panel有关联关系且是一对多的关系。所以我就想通过某种方式将DataGridViewCell控件和Panel控件绑定在一起让它们之间有一定关联关系且是一对一的关联关系首先想到的就是用Control的Tag属性但是担心Tag属性被占用后如果想要添加别的数据到Tag上那就无法赋值了所以我加了TagPlus类在提供绑定控件属性的同时且也保留了Tag属性。绑定之后呢DataGridViewCell和Panel就有了关联关系DataGridViewCell就成了Panel逻辑上的“父控件”Panel成了DataGridViewCell逻辑上的“子控件”这样也更好的可以通过DataGridViewCell去访问到Panel控件通过Panel控件更好的访问到DataGridViewCell控件。
结尾
如果您觉得我还写的不错请给我一点点的鼓励您的鼓励就是我坚持的动力当然啦一切要量力而行不要勉强哦