簡體   English   中英

如何設置自定義DataGridViewColumn的自定義EditingControl的屬性

[英]How to set properties of custom EditingControl of a custom DataGridViewColumn

我創建了一個用戶控件,並為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自定義列的設置值傳遞給usercontrol屬性。

要解決此問題,您需要對代碼進行多次修復,但作為一般性回答,應在自定義單元格的InitializeEditingControl方法中配置編輯控件。 您應該重寫InitializeEditingControl以獲得對編輯控件的訪問權限,並能夠設置其屬性。

public override void InitializeEditingControl(int rowIndex, object 
    initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
    base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
    YourEditingControl c = DataGridView.EditingControl as YourEditingControl;
    //Set c.Properties here
}

這不是您的代碼所需的唯一修復。 鏈接的文章只是一個簡單的示例,您還應該考慮其他幾點:

  1. 您的自定義單元格應具有與自定義列相同的屬性。 此外,編輯控件應具有相同的屬性。
  2. 在自定義列的屬性獲取器中,應該從CellTemplate獲取屬性值。
  3. 在自定義列的屬性設置器中,將屬性值設置為CellTemplate ,並使用for循環為列的所有單元格設置屬性值,因為用戶希望列設置適用於所有單元格。
  4. 在您的自定義單元格中,您需要覆蓋Clone並確保執行自定義單元格屬性的深層副本。 但是由於將屬性值存儲在單元格模板中,因此不需要覆蓋“ Clone以用於自定義列。 克隆對於設計者很重要。
  5. 在自定義單元格中,應在InitializeEditingControl使用自定義單元格的屬性設置編輯控件。 還保留對編輯控件的引用,並在單元格屬性的設置器中更新編輯控件的屬性,因為如果列的屬性發生更改,用戶希望看到編輯控件上的更改。
  6. 在您的自定義編輯控件中,您需要處理通過更改值引發的事件(在本例中為OnTextChanged ),然后使用this.EditingControlDataGridView.NotifyCurrentCellDirty(true);通知DataGridView單元已被弄臟this.EditingControlDataGridView.NotifyCurrentCellDirty(true);

您可以看到我在與ComboBox列相關的源代碼中提到的規則:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM