Add Multiple control type in DataGridView in winforms c#

Tejas GC 6 Reputation points
2020-11-30T14:30:48.53+00:00

Dear Friends,

I want to display the DataGridView like below in winforms c#

43772-q1.png

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,868 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,573 questions
0 comments No comments
{count} vote

4 answers

Sort by: Most helpful
  1. Željko Perić 11 Reputation points
    2020-11-30T18:13:51.103+00:00

    Hello,
    Here are links to some useful articles :

    https://video2.skills-academy.com/en-us/dotnet/api/system.windows.forms.datagridview?view=net-5.0

    https://video2.skills-academy.com/en-us/dotnet/api/system.windows.forms.datagridviewcell?view=net-5.0

    After analyzing above articles here is some code sample that solves the problem :

     void Show_Table_Click(object sender, EventArgs e)  
     {  
        DataGridView Table = new DataGridView();  
        Controls.Add(Table);  
      
        Table.Columns.Add("Column1","Name");  
        Table.Columns.Add("Column2","Address");  
      
        Table.Rows.Add("TextBoxText","TextBoxText");  
        Table.Rows.Add("TextBoxText","TextBoxText");  
        Table.Rows.Add("TextBoxText","TextBoxText");  
        Table.Rows.Add("TextBoxText","TextBoxText");  
        //  
        // Change DataGridViewTextBoxCell to DataGridViewButtonCell  
        //  
        DataGridViewButtonCell ButtonCell = new DataGridViewButtonCell();  
        Table[1, 3] = ButtonCell;  
        Table[1, 3].Value = "Button Text";  
      
      
        Table.ColumnHeadersVisible = false;  
        Table.RowHeadersVisible    = false;  
        Table.AllowUserToAddRows   = false;  
        Table.Height = 91;  
        Table.Width  = 203;  
        Table.AllowUserToResizeColumns = false;  
        Table.AllowUserToResizeRows    = false;  
      
        Table.Location = new System.Drawing.Point(50, 28);  
      
        Refresh();  
     }  
    

    Here is the result :

    44090-sample.png

    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,291 Reputation points
    2020-11-30T20:23:40.343+00:00

    Hello @Tejas GC

    The following may be helpful. Source code is in the following GitHub repository and a Microsoft TechNet Wiki article Windows DataGridView with inline edit and remove buttons.

    The code relies on the following custom column DataGridViewDisableButtonColumn in the repository mentioned above and here is implementing the button column.

    What it does not do "as is" is do two different types e.g. TextBox and Button.

    43783-d1.png

    0 comments No comments

  3. Rajanikant Hawaldar 91 Reputation points
    2020-12-01T14:00:30.84+00:00

    Hello @Tejas GC , Try below sample.

    DataTable dt = new DataTable();  
    dt.Columns.Add("ID");  
    dt.Columns.Add("name");  
    for (int j = 0; j < 5; j++)  
    {  
         dt.Rows.Add("");  
    }  
    this.dataGridView1.DataSource = dt;  
    this.dataGridView1.Columns[1].Width = 200;  
      
    DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell();  
    ComboBoxCell.Items.AddRange(new string[] { "aaa", "bbb", "ccc" });  
    this.dataGridView1[1, 0] = ComboBoxCell;  
    this.dataGridView1[1, 0].Value = "bbb";  
      
    DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();  
    this.dataGridView1[1, 1] = TextBoxCell;  
    this.dataGridView1[1, 1].Value = "some text";  
      
    DataGridViewCheckBoxCell CheckBoxCell = new DataGridViewCheckBoxCell();  
    CheckBoxCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;  
    this.dataGridView1[1, 2] = CheckBoxCell;  
    this.dataGridView1[1, 2].Value = true;  
    
    0 comments No comments

  4. Daniel Zhang-MSFT 9,621 Reputation points
    2020-12-04T02:26:54.193+00:00

    Hi TejasGC-1360,
    The detailed method has been provided above that casting the DataGridViewCell to a specific cell type that exists.
    And you can also create Button and textBox controls and add these to the DataGridView control collection.
    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;  
      
        // Add Button control to the host in the cell.  
        Button bt = new Button();  
        bt.Text = "Button Text";  
        //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, 3, true).Location;  
        bt.Size = this.dataGridView1.GetCellDisplayRectangle(0, 3, true).Size;  
      
        // Add TextBox control to the host in the cell.  
        TextBox tb = new TextBox();  
        tb.Text = "TextBox Text";  
        this.dataGridView1.Controls.Add(tb);  
        //set its location and size to fit the cell  
        tb.Location = this.dataGridView1.GetCellDisplayRectangle(0, 0, true).Location;  
        tb.Size = this.dataGridView1.GetCellDisplayRectangle(0, 0, true).Size;  
      
        TextBox tb2 = new TextBox();  
        tb2.Text = "TextBox Text";  
        this.dataGridView1.Controls.Add(tb2);  
        //set its location and size to fit the cell  
        tb2.Location = this.dataGridView1.GetCellDisplayRectangle(0, 1, true).Location;  
        tb2.Size = this.dataGridView1.GetCellDisplayRectangle(0, 1, true).Size;  
      
        TextBox tb3 = new TextBox();  
        tb3.Text = "TextBox Text";  
        this.dataGridView1.Controls.Add(tb3);  
        //set its location and size to fit the cell  
        tb3.Location = this.dataGridView1.GetCellDisplayRectangle(0, 2, true).Location;  
        tb3.Size = this.dataGridView1.GetCellDisplayRectangle(0, 2, true).Size;  
    }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments