Controls in the same DataGridView column do not display when mesh initialization

GOOD BAD

Different Controls in DataGridView Columnenter image description here

I have different controls in a DataGridView column. When I add controls at the same time, I initialize the grid whose controls are displayed as text fields (BAD). If I add controls after the DataGridView has been initialized, the controls render correctly (GOOD).

public Form1()
{
        InitializeComponent();
        ControlsInGridViewColumn(); //<- renders controls as textboxes
}

private void button1_Click(object sender, EventArgs e)
{
        ControlsInGridViewColumn();  //<- does correctly render controls
}

private void ControlsInGridViewColumn()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("name");
    for (int j = 0; j < 10; j++)
    {
        dt.Rows.Add("");
    }
    this.dataGridView1.DataSource = dt;
    this.dataGridView1.Columns[0].Width = 200;

    DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell();
    ComboBoxCell.Items.AddRange(new string[] { "aaa","bbb","ccc" });
    this.dataGridView1[0, 0] = ComboBoxCell;
    this.dataGridView1[0, 0].Value = "bbb";

    DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();
    this.dataGridView1[0, 1] = TextBoxCell;
    this.dataGridView1[0, 1].Value = "some text";

    DataGridViewCheckBoxCell CheckBoxCell = new DataGridViewCheckBoxCell();
    CheckBoxCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    this.dataGridView1[0, 2] = CheckBoxCell;
    this.dataGridView1[0, 2].Value = true;
}

How to ensure the correct display of controls during initialization?

(I am dynamically adding DataGridViews).

UPDATE: Jakob Seleznev is responsible for working with forms, but not for the user controls that I need ... Here's how to reproduce it:

In the shape of:

private void button3_Click(object sender, EventArgs e)
{
    this.Controls.Add(new userCtrl());
}

User control:

public partial class userCtrl : UserControl
{
    public userCtrl()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        ControlsInGridViewColumn();
        base.OnLoad(e);
    }

    public void ControlsInGridViewColumn()
    {
        //same code as above
    }

}
+3
1

:

    protected override void OnLoad(EventArgs e)
    {
        ControlsInGridViewColumn();  //<- does correctly render controls
        base.OnLoad(e);
    }
+3

Source: https://habr.com/ru/post/1528993/


All Articles