私は1つのusercontrolを作成し、TextBoxの関数を追加しました。このusercontrolをDataGridViewColumnにバインドします。しかし、ユーザーがコントロールするカスタムプロパティにどのようにアクセスできるかわかりません。私はチュートリアルhttps://msdn.microsoft.com/en-us/library/7tas5c80.aspx?PHPSESSID=o1fb21liejulfgrptbmi9dec92カスタムDataGridViewColumnのカスタムEditingControlのプロパティを設定する方法
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'のカスタム列を作成することについてもっと多くのことを考慮する必要があります。 –