locked
Adding multiple, different controls to DataGridView in Winforms C# RRS feed

  • Question

  • Dear Friends,

    In DataGridView of winforms  second Column for some row i want to have Button control and on the same column for different row i want TextBox control.

    Could any one please help me to resolve this problem.

    Friday, November 27, 2020 11:00 AM

All replies

  • Hello,

    Can you provide code to what you have so far?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Check out:  the new Microsoft Q&A forums

    Friday, November 27, 2020 1:38 PM
  • 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.

    Monday, November 30, 2020 7:05 AM
  • Hi TejasGC,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly. 
    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.

    Friday, December 4, 2020 9:16 AM