Reputation: 576
I am developing a windows application in C#,I have a DataGridView
,currently showing values of a datasource. I want to add a column in which every row will have a table.Now Is there any way to do that dynamically? because the table structure will vary in each row.
thanks in advance.
Edit :- I mean, I want to insert a table in a cell, dynamically.
As u can see Entity Details
column showing this when I tried to add TableLayoutPanel
,in each row dynamically.
Upvotes: 3
Views: 7059
Reputation: 1
You can add a table to the cells control collection using the RowDatabound Event. Something like this example(sorry - different to what you are doing but maybe similar to what you want, also in VB rather than C#)
Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim i As Integer
Dim myTable As Table
Dim myRow As TableRow
Dim mycell As TableCell
If e.Row.RowType = DataControlRowType.DataRow Then
myTable = New Table
myRow = New TableRow
For i = 1 To 3
mycell = New TableCell
mycell.Text = i.ToString
mycell.BorderStyle = BorderStyle.Solid
mycell.BorderWidth = 0.2
mycell.Width = 15
If i = 1 Then mycell.BackColor = Drawing.Color.Beige
If i = 2 Then mycell.BackColor = Drawing.Color.Yellow
If i = 3 Then mycell.BackColor = Drawing.Color.Green
myRow.Cells.Add(mycell)
Next
myTable.Rows.Add(myRow)
e.Row.Cells(12).Controls.Add(myTable)
End If
End Sub
Upvotes: 0
Reputation: 65554
1). Try out MSDN's Mark Ridout's TreeGridView and read his article on using it.
Also see if this CodeProject DataGridView with Hierarchical Data Binding that uses Mark Ridout's TreeGridView is useful.
2). If free controls dont work out have a look at 3rd parties (I am not affiliated with these co's):
Devexpress XtraGrid
Telerik Gridview
Infragistics Grid
VIBlend DataGridView for WinForms
Janus Grid
xceed's Grid
3). I'm pretty sure adding a TablePanelLayout into a Grids cell is not what you want, here is the code so you can see how dodgy it is for yourself:
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;
//add tableLayoutPanel1 into the control collection of the DataGridView
this.dataGridView1.Controls.Add(tableLayoutPanel1);
//resize the row
this.dataGridView1.Rows[1].Height = 100;
//set its location and size to fit the cell
tableLayoutPanel1.Location = this.dataGridView1.GetCellDisplayRectangle(0,1, true).Location;
tableLayoutPanel1.Size = this.dataGridView1.GetCellDisplayRectangle(0, 1, true).Size;
Upvotes: 1
Reputation: 4904
Adding a column with a table within it in a DataGridView
is not possible according to me..
Best alternate to your question is to create a dynamic table by self and repeat rows to show your data and within that row you can off course put a cell with a table within it.
Upvotes: 0
Reputation: 5815
There is this article : How to: Host Controls in Windows Forms DataGridView Cells
at MSDN
The DataGridView control provides several column types, enabling your users to enter and edit values in a variety of ways. If these column types do not meet your data-entry needs, however, you can create your own column types with cells that host controls of your choosing.
To do this, you must define classes that derive from DataGridViewColumn and DataGridViewCell.
You must also define a class that derives from Control and implements the IDataGridViewEditingControl interface.
So i would recommend , first Make your Table as independent control with properties or method then use that control to be displayed for each row - when you are doing databind ,let each row set data for your TableControl.
An alternative approach could be this (assuming you are not editing in grid) - you can create a single row like usercontrol ( create a usercontrol where all textbox or label are laid around like datagridrow style them so that it looks like a single row) and then you add them in a panel with scrollbar .
Upvotes: 0
Reputation: 673
I don't think this is (easily) possible with the controls provided out-of-the-box by Microsoft.
If you have the budget, perhaps check out Telerik's controls.
http://www.telerik.com/products/winforms/gridview.aspx
They have a gridview control as part of their .NET WinForm controls that looks like it will do what you want it to.
Otherwise, is it possible to change your architecture and do an ASP.NET web-based approach to solving this problem?
Hope that helps. Good luck.
Upvotes: 0