How can I place a control in a DataGridViewCell for display as well as editing?

I saw How to manage a host in Windows Forms DataGridView boxes , which explains how to place a control to edit a cell in a DataGridView. But how can I place a control to display a cell?

I need to display the file name and button in the same cell. Our user interface designer is a graphic designer, not a programmer, so I need to match the code with what he drew, is it possible - or wise - or not. We use VS2008 and write in C # for .NET 3.5, if that matters.

UPDATE: "net" suggests creating a custom DataGridViewCell that contains the panel as a first step; Has anyone done this?

+2
source share
3 answers

According to your UPDATE, creating a custom DataGridViewCellis how it is done. I did this, and it does not require such modification from the sample code available from MSDN. In my case, I needed a lot of custom controls, so I inherited from DataGridViewTextBoxCelland DataGridViewColumn. I inserted into my class (the one inherited from DataGridViewTextBoxCell) the new custom control that I implemented IDataGridViewEditingControl, and it all just worked.

, PanelDataGridViewCell, MyPanelControl, Panel IDataGridViewEditingControl.

+3

datagridview, TableLayoutPanel . , , , . , . , datagridview, , .

+2

:

1). DataGridViewCell , . , DataGridViewTextBoxCell DataGridViewComboBoxCell.

2). DataGridView, , , .

. Zhi-Xin Ye, :

private void Form_Load(object sender, EventArgs e)
{
    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;

    /*
    * First method : Convert to an existed cell type such ComboBox cell,etc
    */

    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;

    /*
    * Second method : Add control to the host in the cell
    */
    DateTimePicker dtp = new DateTimePicker();
    dtp.Value = DateTime.Now.AddDays(-10);
    //add DateTimePicker into the control collection of the DataGridView
    this.dataGridView1.Controls.Add(dtp);
    //set its location and size to fit the cell
    dtp.Location = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Location;
    dtp.Size = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Size;
}

MSDN: DataGridView

:

Different Controls in DataGridView Column

:

enter image description here

Additional Information: Controls in the same DataGridView column do not display during grid initialization

+1
source

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


All Articles