【发布时间】:2017-06-12 16:09:53
【问题描述】:
我创建了一个用户控件并为 TextBox 添加了更多功能,我将此用户控件绑定到一个 DataGridViewColumn。但我不知道如何访问用户控件的自定义属性。
我是如何遵循教程https://msdn.microsoft.com/en-us/library/7tas5c80.aspx?PHPSESSID=o1fb21liejulfgrptbmi9dec92
public class IntegerCell : DataGridViewTextBoxCell
{
//Implementations
}
public class IntegerColumn : DataGridViewColumn
{
public IntegerColumn()
:base(new IntegerCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CalendarCell.
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(IntegerCell)))
{
throw new InvalidCastException("Must be a IntegerCell");
}
base.CellTemplate = value;
}
}
[Browsable(true)]
[Category("Custom")]
[Description("")]
[DisplayName("Max. Value")]
public int MaxValue { get; set; }
[Browsable(true)]
[Category("Custom")]
[Description("")]
[DisplayName("Min. Value")]
public int MinValue { get; set; }
}
public partial class IntegerEditingControl : IntegerTextBox, IDataGridViewEditingControl
{
//Implementations
}
public class IntegerTextBox : TextBox
{
[Browsable(true)]
[Category("Custom")]
[Description("")]
[DisplayName("Max. Value")]
public int MaxValue { get; set; }
[Browsable(true)]
[Category("Custom")]
[Description("")]
[DisplayName("Min. Value")]
public int MinValue { get; set; }
}
public partial class Form1 : Form
{
private DataGridView dataGridView1 = new DataGridView();
public Form1()
{
InitializeComponent();
this.dataGridView1.Dock = DockStyle.Fill;
this.Controls.Add(this.dataGridView1);
this.Load += new EventHandler(Form1_Load);
this.Text = "DataGridView column demo";
}
private void Form1_Load(object sender, EventArgs e)
{
IntegerColumn col = new IntegerColumn();
this.dataGridView1.Columns.Add(col);
this.dataGridView1.RowCount = 5;
int i = 0;
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
row.Cells[0].Value = i++;
}
}
}
我想将 DataGridView 自定义列的设置值传递给用户控件属性。
【问题讨论】:
-
一个简短的回答是:覆盖
InitializeEditingControl并使用DataGridView.EditingControl获得编辑控制并设置其值。但是您应该考虑更多关于为DataGridView创建自定义列的事情。
标签: c# winforms datagridview datagridviewcolumn