在线等待:自定义DataGridViewColumn控件中自定义属性值无法保存的问题?

cnjack 2011-11-08 08:44:30
我在自定义DataGridViewColumn控件中,定义了属性DecimalPlace,在DataGridView的设计界面中增加此控件列没问题,但在修改DecimalPlace属性值点确定后,再打开计界面查询此属性的值却没有改变(在没点确定前,查看此控件的其它属性后再回来看此属性的值是改变的).
运行环境:WindowsXp(SP3)+ Vs.Net2008企业版
麻烦各位帮忙看一下

我用了三种方法写了三个控件,都是在点击确定后没有保存最新值,具体代码如下:
一、三个控件代码
1.DataGridViewCustom1
using System;
using System.ComponentModel;
using System.Windows.Forms;

public class DataGridViewCustomColumn_1 : DataGridViewColumn
{
private int m_decimalPlace;
/// <summary>
/// 小数位数
/// </summary>
[Description("小数位数"), Browsable(true), Category("财务显示单元格")]
public int DecimalPlace
{
get { return m_decimalPlace != 0 ? m_decimalPlace : 2; }
set
{
if (m_decimalPlace != value)
{
if (value < 0)
throw new Exception("《小数位数》不可以小于 0 !");
else
{
this.m_decimalPlace = value;
}
}
}
}
public DataGridViewCustomColumn_1()
: base(new DataGridViewCustomCell_1())
{
}
}

public class DataGridViewCustomCell_1 : DataGridViewTextBoxCell
{
}

2.DataGridViewCustom2
using System;
using System.ComponentModel;
using System.Windows.Forms;

public class DataGridViewCustomColumn_2 : DataGridViewColumn
{
private int m_decimalPlace;
/// <summary>
/// 小数位数
/// </summary>
[Description("小数位数"), Browsable(true), Category("财务显示单元格")]
public int DecimalPlace
{
get { return m_decimalPlace != 0 ? m_decimalPlace : 2; }
set
{
if (m_decimalPlace != value)
{
if (value < 0)
throw new Exception("《小数位数》不可以小于 0 !");
else
{
this.m_decimalPlace = value;
DataGridViewCustomCell_2 customCell = (DataGridViewCustomCell_2)this.CellTemplate;
customCell.DecimalPlace = value;
}
}
}
}
public DataGridViewCustomColumn_2()
: base(new DataGridViewCustomCell_2())
{
}
}

public class DataGridViewCustomCell_2 : DataGridViewTextBoxCell
{
private int m_decimalPlace;
/// <summary>
/// 小数位数
/// </summary>
public int DecimalPlace
{
get { return m_decimalPlace; }
set
{
if (m_decimalPlace != value)
{
if (value < 0)
throw new Exception("《小数位数》不可以小于 0 !");
else
m_decimalPlace = value;
}
}
}
}

3.DataGridViewCustom3
using System;
using System.ComponentModel;
using System.Windows.Forms;

public class DataGridViewCustomColumn_3 : DataGridViewColumn
{
private int m_decimalPlace;
/// <summary>
/// 小数位数
/// </summary>
[Description("小数位数"), Browsable(true), Category("财务显示单元格")]
public int DecimalPlace
{
get { return m_decimalPlace != 0 ? m_decimalPlace : 2; }
set
{
if (m_decimalPlace != value)
{
if (value < 0)
throw new Exception("《小数位数》不可以小于 0 !");
else
{
this.m_decimalPlace = value;
DataGridViewCustomCell_3 customCell = (DataGridViewCustomCell_3)this.CellTemplate;
customCell.DecimalPlace = value;

if (this.DataGridView != null && this.DataGridView.Rows != null)
{
int rowCount = this.DataGridView.Rows.Count;
for (int x = 0; x < rowCount; x++)
{
foreach (DataGridViewCell dgvCell in this.DataGridView.Rows.SharedRow(x).Cells)
{
if (dgvCell is DataGridViewCustomCell_3)
(dgvCell as DataGridViewCustomCell_3).DecimalPlace = value;
}
}
}
}
}
}
}
public DataGridViewCustomColumn_3()
: base(new DataGridViewCustomCell_3())
{
}
}

public class DataGridViewCustomCell_3 : DataGridViewTextBoxCell
{
private int m_decimalPlace;
/// <summary>
/// 小数位数
/// </summary>
public int DecimalPlace
{
get
{
DataGridViewCustomColumn_3 customColumn = OwningColumn as DataGridViewCustomColumn_3;
if (customColumn != null)
{
if (customColumn.DecimalPlace != 0)
{
return customColumn.DecimalPlace;
}
}
return m_decimalPlace;
}
set
{
if (m_decimalPlace != value)
{
if (value < 0)
throw new Exception("《小数位数》不可以小于 0 !");
else
m_decimalPlace = value;
}
}
}
}
...全文
254 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
DENQH 2011-11-08
  • 打赏
  • 举报
回复
看了半天,还是不懂LZ要这个做什么
sdl2005lyx 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 cnjack 的回复:]

sdl2005lyx,我是需要此属性来来确定画线用的颜色
[/Quote]

是下划线,还是。。。。?
cnjack 2011-11-08
  • 打赏
  • 举报
回复
sdl2005lyx,我是需要此属性来来确定画线用的颜色
xiaoyu821120 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 nanqi0506 的回复:]
C# code
using System;
using System.ComponentModel;
using System.Windows.Forms;

public class DataGridViewCustomColumn_1 : DataGridViewColumn
{
public override object Clone()
{
……
[/Quote]
新添加的属性要在Clone方法中重新赋值,不然会丢失。
sdl2005lyx 2011-11-08
  • 打赏
  • 举报
回复
看了你的代码,感觉你这种需求,没有必要用自定义列,用CellFormatting事件就可以解决。。。
神棍 2011-11-08
  • 打赏
  • 举报
回复
为什么我刚刚发的不见了.
神棍 2011-11-08
  • 打赏
  • 举报
回复
using System;
using System.ComponentModel;
using System.Windows.Forms;

public class DataGridViewCustomColumn_1 : DataGridViewColumn
{
public override object Clone()
{
DataGridViewCustomColumn_1 col = (DataGridViewCustomColumn_1)base.Clone();
col.DecimalPlace = this.DecimalPlace;

return col;
}

private int m_decimalPlace;
/// <summary>
/// 小数位数
/// </summary>
[Description("小数位数"), Browsable(true), Category("财务显示单元格")]
public int DecimalPlace
{
get
{
return m_decimalPlace != 0 ? m_decimalPlace : 2;
}
set
{
if (m_decimalPlace != value)
{
if (value < 0)
throw new Exception("《小数位数》不可以小于 0 !");
else
{
this.m_decimalPlace = value;
}
}
}
}
public DataGridViewCustomColumn_1()
: base(new DataGridViewCustomCell_1())
{
}
}

public class DataGridViewCustomCell_1 : DataGridViewTextBoxCell
{
}
cnjack 2011-11-08
  • 打赏
  • 举报
回复
DecimalPlace的默认值 为2,当我在设计界面中修改DecimalPlace的值为5并点击确定后,我再次打开设计界面中查看DecimalPlace的值时,发现其值还是2,而不是我之前更改的最新值5
DENQH 2011-11-08
  • 打赏
  • 举报
回复
designer.文件是垃圾,不要管,只说出你加三个column的最终实现的目的就可以了
cnjack 2011-11-08
  • 打赏
  • 举报
回复
Sory,test.designer.cs文件传错了.
namespace WindowsApplication37
{
partial class test
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.CustomColumn1 = new DataGridViewCustomColumn_1();
this.CustomColumn2 = new DataGridViewCustomColumn_2();
this.CustomColumn3 = new DataGridViewCustomColumn_3();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.CustomColumn1,
this.CustomColumn2,
this.CustomColumn3});
this.dataGridView1.Location = new System.Drawing.Point(68, 26);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 23;
this.dataGridView1.Size = new System.Drawing.Size(598, 329);
this.dataGridView1.TabIndex = 0;
//
// CustomColumn1
//
this.CustomColumn1.DecimalPlace = 2;
this.CustomColumn1.HeaderText = "CustomColumn1";
this.CustomColumn1.Name = "CustomColumn1";
//
// CustomColumn2
//
this.CustomColumn2.DecimalPlace = 2;
this.CustomColumn2.HeaderText = "CustomColumn2";
this.CustomColumn2.Name = "CustomColumn2";
//
// CustomColumn3
//
this.CustomColumn3.DecimalPlace = 2;
this.CustomColumn3.HeaderText = "CustomColumn3";
this.CustomColumn3.Name = "CustomColumn3";
//
// test
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(754, 380);
this.Controls.Add(this.dataGridView1);
this.Name = "test";
this.Text = "test";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.DataGridView dataGridView1;
private DataGridViewCustomColumn_1 CustomColumn1;
private DataGridViewCustomColumn_2 CustomColumn2;
private DataGridViewCustomColumn_3 CustomColumn3;
}
}
风骑士之怒 2011-11-08
  • 打赏
  • 举报
回复
FinanceTextBoxColumn呢,在哪里
DENQH 2011-11-08
  • 打赏
  • 举报
回复
写哪么多就是为了一个小数位?
cnjack 2011-11-08
  • 打赏
  • 举报
回复
二、测试窗口代码
1.test.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication37
{
public partial class test : Form
{
public test()
{
InitializeComponent();
}
}
}

2.test.designer.cs
namespace WindowsApplication37
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column1 = new WindowsApplication37.FinanceTextBoxColumn();
this.Column2 = new System.Windows.Forms.DataGridViewComboBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
dataGridViewCellStyle1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.dataGridView1.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column3,
this.Column1,
this.Column2});
dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
this.dataGridView1.DefaultCellStyle = dataGridViewCellStyle2;
this.dataGridView1.Location = new System.Drawing.Point(58, 62);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 23;
this.dataGridView1.Size = new System.Drawing.Size(566, 336);
this.dataGridView1.TabIndex = 0;
//
// Column3
//
this.Column3.HeaderText = "Column3";
this.Column3.Name = "Column3";
//
// Column1
//
this.Column1.DecimalPlace = 2;
this.Column1.HeaderText = "Column1";
this.Column1.LineBold = 0F;
this.Column1.Name = "Column1";
this.Column1.NormalColor = System.Drawing.Color.Gray;
this.Column1.RedColor = System.Drawing.Color.Red;
this.Column1.SpecialColor = System.Drawing.Color.Blue;
//
// Column2
//
this.Column2.HeaderText = "Column2";
this.Column2.Name = "Column2";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(664, 504);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion

private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
private FinanceTextBoxColumn Column1;
private System.Windows.Forms.DataGridViewComboBoxColumn Column2;
}
}
cnjack 2011-11-08
  • 打赏
  • 举报
回复
财务表格似的,画分隔线:个位线,千位线和普通位置线的颜色。
本文档不准备面面俱到地介绍DataGridView,而是着眼于深入地介绍一些技术点的高级特性。 本文档按逻辑分为5个章节,首先是结构和特性的概览,其次是内置的列/单元格类型的介绍,再次是数据操作相关的内容,然后是主要特性的综述,最后是最佳实践。 大部分章节含有一个“Q & A”部分,来回答该章节相关的一些常见问题。注意,某些问题会由于知识点的关联性重复出现在多个章节。这些问题、答案及其附带的示例代码都包含在本文档的附录部分。 一、DataGridView技术点的高级特性。 11 1 何为DataGridView 11 1.1 DataGridView和DataGrid 之间的区别 11 1.2 DataGridView的亮点 12 2 DataGridView的结构 13 2.1 结构元素(Architecture Elements) 13 2.2 单元格和组(Cells and Bands) 13 2.3 DataGridView的单元格 (DataGridViewCell) 13 2.3.1 DataGridViewCell的工作机制 14 2.3.2 常见问题 15 2.4 DataGridView的列(DataGridViewColumn) 16 2.5 DataGridView的编辑控件(Editing Controls) 16 2.6 DataGridViewRow 17 2.6.1 常见问题 17 3 列/单元格类型揭密(column/cell types) 17 3.1 DataGridViewTextBoxColumn 18 3.2 DataGridViewCheckBoxColumn 19 3.3 DataGridViewImageColumn 19 3.4 DataGridViewButtonColumn 19 3.5 DataGridViewComboBoxColumn 20 3.5.1 DataError事件和ComboBox列 20 3.5.2 常见问题 20 3.6 DataGridViewLinkColumn 21 4 操作数据(Working with Data) 21 4.1 数据输入和验证的相关事件 21 4.1.1 数据验证相关事件的顺序 21 4.1.2 验证数据 22 4.1.3 在新行的数据输入(Data Entry in the New Row) 22 4.1.3.1 显示新行 22 4.1.3.2 为生成的新行添加默认 22 4.1.3.4 在新行输入数据 23 4.1.3.5 自定义新行的可视化效果 23 4.1.3.6 新行的排序 24 4.1.3.7 关于新行,还要注意: 24 4.1.3.8 Virtual Mode下的新行 24 4.2 关于Null 24 4.2.1 NullValue属性 24 4.2.2 DataSourceNullValue属性 25 4.3 DataError事件 25 4.4 数据绑定模式(Databound modes) 26 4.4.1 非绑定模式(Unbound Mode) 26 4.4.2 绑定模式(Bound Mode) 26 4.4.2.1 有效的数据源 27 4.4.3 虚拟模式 27 4.4.4 混合模式 – 绑定与非绑定模式 27 4.4.5 常见问题 28 5 特性综览(Overview of features) 28 5.1 样式(Styling) 28 5.1.1 The DataGridViewCellStyle Class 29 5.1.2 Using DataGridViewCellStyle Objects 29 5.1.3 Style Inheritance 30 5.1.4 Setting Styles Dynamically 34 5.2 Custom painting 35 5.2.1 Paint Parts 35 5.3.1 在Windows窗体DataGridView控件调整大小选项 39 5.3.2 Resizing with the Mouse用鼠标调整大小 42 5.3.3 Automatic Sizing自动调整大小 43 5.3.4 Programmatic Resizing编程调整大小 45 5.3.5 Customizing Content-based Sizing Behavior自定义基于内容的调整大小行为 46 5.3.6 Content-based Sizing Options基于内容的调整大小选项 47 5.4 Selection modes选择模式 47 5.4.1 Programmatic Selection编程选择 49 5.5 滚动(Scrolling) 49 5.5.1 Scroll event Scroll事件 49 5.5.2 Scroll bars滚动条 50 5.5.3 Scrolling Properties滚动属性 50 5.6 Sorting排序 50 5.6.1 Programmatic Sorting编程排序 52 5.6.2 Custom Sorting自定义排序 53 5.6.3 Common questions and scenarios常见问题及案例 54 5.7 Border styles边框样式 55 5.7.1 Standard Border Styles标准边框样式 55 5.7.2 Advanced Border Styles高级边框风格 56 5.8 Enter-Edit modes输入,编辑模式 57 5.9 Clipboard copy modes剪贴板拷贝模式 58 5.10 Frozen columns/rows冻结的列/行 60 5.11 Implementing Custom cells and editing controls/cells实现自定义和编辑控制单元格/单元格 60 5.11.1 IDataGridViewEditingControl 接口 61 5.11.2 IDataGridViewEditingCell 接口 61 5.12 Virtual mode虚拟模式 61 5.12.1 Bound Mode and Virtual Mode绑定模式和虚拟模式 62 5.12.2 Supplementing Bound Mode补充绑定模式 62 5.12.3 Common questions and scenarios常见问题及案例 62 5.12.4 Replacing Bound Mode更换绑定模式 63 5.12.5 Virtual-Mode Events虚拟模式事件 63 5.12.6 Best Practices in Virtual Mode在虚拟模式下的最佳实践 66 5.13 容量(Capacity) 66 6 最佳实践(Best Practices) 67 6.1 Using Cell Styles Efficiently使用高效单元格样式 67 6.2 Using Shortcut Menus Efficiently使用高效快捷菜单 68 6.3 Using Automatic Resizing Efficiently使用自动调整大小高效 69 6.4 Using the Selected Cells, Rows, and Columns Collections Efficiently高效使用选定的单元格,行和列的集合 69 6.5 Using Shared Rows 使用共享行 70 6.6 Preventing Rows from Becoming Unshared 防止行成为非共享 72 附录 A – FAQ 75 1. 如何使指定的单元格不可编辑? 75 2. 如何让一个单元格不可用(disable)? 75 3. 如何避免用户将焦点设置到指定的单元格? 77 4. 如何使所有单元格总是显示控件(不论它是否处于编辑状态)? 77 5. Why does the cell text show up with “square” characters where they should be new lines(TODO,未能实现该效果)? 78 6. 如何在单元格内同时显示图标和文本? 78 7. 如何隐藏一列? 80 8. 如何避免用户对列排序? 81 9. 如何针对多个列排序? 81 9.1 将数据绑定到DataGridView时 81 9.2 Unbound DataGridView 取消绑定 82 9.2.1 Custom Sorting Using the SortCompare Event 使用排序结束时间实现用户自定义排序 82 9.2.2 Custom Sorting Using the IComparer Interface使用IComparer接口实现自定义排序 84 10. 如何为编辑控件添加事件处理函数? 86 11. 应在何时移除编辑控件的事件处理函数? 87 12. 如何处理ComboBox列控件的SelectIndexChanged事件? 87 13. 如何通过拖放调整行的顺序? 87 14. 如何调整最后一列的宽度使其占据网格的剩余客户区? 89 15. 如何让TextBox类型的单元格支持换行? 89 16. 如何使Image列不显示任何图像(字段为null时)? 90 17. 如何能够在ComboBox类型的单元格输入数据? 90 18. How do I have a combo box column display a sub set of data based upon the value of a different combo box column(TODO)? 91 19. 如何在用户编辑控件的时候(而不是在验证时)就显示错误图标? 92 20. 如何同时显示绑定数据和非绑定数据? 94 21. How do I show data that comes from two tables(TODO)?如何显示来自两个数据源的数据? 96 22. 如何显示主从表? 97 23. 如何在同一DataGridView显示主从表? 99 24. 如何避免用户对列排序? 99 25. 如何在点击工具栏按钮的时候将数据提交到数据库? 99 26. 如何在用户删除记录时显示确认对话框? 99

110,533

社区成员

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

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

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