question

TejasGC-1360 avatar image
1 Vote"
TejasGC-1360 asked DanielZhang-MSFT answered

Add Multiple control type in DataGridView in winforms c#

Dear Friends,

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

43772-q1.png


dotnet-csharpwindows-forms
q1.png (11.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

periczeljkosmederevo avatar image
1 Vote"
periczeljkosmederevo answered periczeljkosmederevo rolled back

Hello,
Here are links to some useful articles :

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview?view=net-5.0

https://docs.microsoft.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




sample.png (2.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

Hello @TejasGC-1360

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



d1.png (21.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RajanikantHawaldar-9680 avatar image
0 Votes"
RajanikantHawaldar-9680 answered RajanikantHawaldar-9680 edited

Hello @TejasGC-1360, 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;
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.