Hi TejasGC,
You need to create a control and add it to the DataGridView control collection. Then set its position and size to fit the cell as the host.
You can also cast the DataGridViewCell to a specific cell type that exists.
Here is a simple code example you can refer to.
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Test");
for (int j = 0; j < 8; j++)
{
dt.Rows.Add("");
}
this.dataGridView1.DataSource = dt;
this.dataGridView1.Columns[0].Width = 200;
//First method: Add control to the host in the cell.
Button bt = new Button();
bt.Text = "button";
//add button into the control collection of the DataGridView
this.dataGridView1.Controls.Add(bt);
//set its location and size to fit the cell
bt.Location = this.dataGridView1.GetCellDisplayRectangle(0, 1, true).Location;
bt.Size = this.dataGridView1.GetCellDisplayRectangle(0, 1, true).Size;
// Second method: Convert to an existed cell type such TextBox cell.
DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();
this.dataGridView1[0, 2] = TextBoxCell;
this.dataGridView1[0, 2].Value = "some text";
}
Best Regards,
Daniel Zhang
"Visual c#" forum will be migrating to a new home on
Microsoft Q&A ! We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on
Microsoft Q&A ! For more information, please refer to the
sticky post.