TableLayoutPanel's Control Columns properties - c#

I've noticed that every control added to the TableLayoutPanel is given "Column" and "Row" properties.
How can I get access to these properties through code?
thanks:)

These properties only exist in the Properties Window, magic provided by the IExtenderProvider interface. They don't exist at runtime. Extended properties are:
ColumnSpan. Runtime: GetColumnSpan() and SetColumnSpan()
RowSpan. Runtime: GetRowSpan() and SetRowSpan()
Row. Runtime: GetRow() and SetRow()
Cell. Runtime: GetCellPosition() and SetCellPosition()
Column. Runtime: GetColumn() and SetColumn()
Obviously TLP was highly optimized to be used from the designer. It's kinda of a pain at runtime.

Although the properties designer shows the row and column as properties of the added control thay are set programatically using a method on the table layout panel itself (SetColumn(control, index) and SetRow(control, index)).
This pattern of behaviour is similar the tool tip component and the error component.

Go here.
This properties are added by means of "extending properties", something that other controls like ToolTip uses.

// Create TableLayoutPanel
TableLayoutPanel tlp = new TableLayoutPanel();
// Set the BorderStyle to Inset
tlp.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset;
// Grid has two columns
tlp.ColumnCount = 2;
// Grid has two rows
tlp.RowCount = 2;
// If grid is full add extra cells by adding column
tlp.GrowStyle = TableLayoutPanelGrowStyle.AddColumns;
// Padding (pixels)within each cell (left, top, right, bottom)
tlp.Padding = new Padding(1, 1, 4, 5);
// Add TableLayoutPanel to the Forms controls
this.Controls.Add(tlp);
for more check this
http://en.csharp-online.net/TableLayoutPanel

Related

GridView: forecolor of column header

This Grid column property i can set from the designer, but not from the C # code !!! Why? Header.BackColor is working fine. I can also set any other color for the GridView elements except this.
If you are doing this at runtime you need to set the GridColumn.AppearanceHeader.Options.UserForeColor property to true as well as set the actual color. When you do this via the designer, the property is automatically set to true.
gridView1.Columns["FieldName"].AppearanceHeader.ForeColor = Color.Red;
gridView1.Columns["FieldName"].AppearanceHeader.Options.UseForeColor = true;
Ref: How do I set the color of a grid column header panel?
The header's BackColor property is ignored in the Skin paint style.
Add the following line to your code to address this issue:
gridControl1.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Flat;
gridControl1.LookAndFeel.UseDefaultLookAndFeel = false; // <<<<<<<<
gridView1.Appearance.HeaderPanel.Options.UseForeColor = true;
gridView1.Appearance.HeaderPanel.ForeColor = System.Drawing.Color.Red;
Reference these for detailed information:
Gridview - How to change a column's header background color
Why Appearance settings of DevExpress controls are not taken into account?
Devexpress column header color (each column different color), winform c#
The DevExpress skinning mechanism overrides the Appearance object settings. You have a couple options if you need a different background color for the column header:
Disable skinning on the GridControl by setting the GridControl.LookAndFeel.UseDefaultLookAndFeel property to False and the GridControl.LookAndFeel.Style property to "Flat".
Handle the GridView's CustomDrawColumnHeader and fill the column header with a different color.

How to make a ListView-Control behave like a ListBox-Control?

Because I would like to color every single element differently, I decided for the ListView instead of the ListBox, which can only colour all elements at once.
That means it should, so to speak, have only one column and insert the elements among each other, comparable to the command listBox.Items.Insert(0, "Item").
Which properties do I need to change in order to achieve that?
I already tried setting the View Property to View.List but as soon as there are too many elements in a row, it continues to insert the elements into a second row that I didn't even create and also can't find when I'm looking at Edit Columns...
You can set the View to Details and set the HeaderStyle to None, and then by adding a column and setting its size to -1 force the column to use the same width as ListView:
this.listView1.View = View.Details;
this.listView1.HeaderStyle = ColumnHeaderStyle.None;
this.listView1.FullRowSelect = true;
this.listView1.Columns.Add("", -2);
this.listView1.Items.Add("Something");
this.listView1.Items.Add("Something else").BackColor = Color.Red;

Align components in Groupbox in c#

I am having a group box of width 900px and height 250px. and have to place 20 around radio buttons in it in a 4 rows * 5 columns tabular format. But at present its coming in 20 rows * 1 column format.
And i have to do this using groupbox.
I'll be glad for the answers thank you .
Unfortunately, this is not imporssible, as Group control does not support overflow style child management. You have two options (maybe more):
Create your custom group box and implement the "row overflow" logic yourself;
You might embed a child TableLayoutPanel or ListBox in your group box, and add child items there;
for the TableLayoutPanel option, you might try below snippet to see if it matches your requriment:
TableLayoutPanel Table = new TableLayoutPanel();
Table.AutoSize = true;
Table.RowCount = 4;
Table.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
Table.GrowStyle = System.Windows.Forms.TableLayoutPanelGrowStyle.AddCols;
this.Controls.Add(Table);
for the listbox, you can try this RadioListBox, and also set:
listBox1.MultiColumn = true;
And also you need adjust the listbox height so that exactly 4 rows are there.
If WPF is an option, this will be trivial as you can do this with XAML, and implementation your own ItemTemplate is just a breeze.

XAML Creating GridView from Code Behind

I am practicing to learn XAML, but syntax is too verbose at my first impression. I am trying to create a gridView from codebehind like below:
var gv = new GridView();
gv.Name = "itemGridView";
gv.ItemsSource = sampleDataGroups;
gv.SetValue(AutomationProperties.AutomationIdProperty, "ItemGridView");
gv.SetValue(AutomationProperties.NameProperty, "Grouped Items");
gv.Padding = new Thickness(116, 137, 40, 46);
Grid.SetRowSpan(gv, 2);
While I can set my gridView' padding like a property, why I am using a static method of grid class to set its rowSpan ?
What is the reason behind that decision ?
Note: I guess I can write an extension method to do that but I am curious why it is not made at the beginning.
The reason is - a GridView (or pretty much every other control except the Grid itself) doesn't have a RowSpan property. Neither do these controls have the Row, Column and ColumnSpan properties.
Don't believe me? Check out the MSDN documentation for GridView. ;)
These are properties which you can use when you put something inside a Grid. Note that in XAML, you also set these properties up in a slightly different fashion: Grid.Row="1" as opposed to just Padding="2".
You can also set up the Grid properties as you did with AutomationProperties. The Grid.SetRowSpan is just a shorthand.
Because setrow and setrowspan are attached properties which are available to gridview's parent GRID which decides the measuring and the layout of the children and hence are not directly available to the children (gridview).

DataGridView , Adjusting width and height to DataTable

I am binding a DataTable to GridView. It does not adjust to height and width of the DataTable. How can I the strech the width of the grid that i shows all the columns and height shrink if the rows are few.
just go to the properties of your datagrid =>
and then at the section Layout=>
AutoSizeColumnsMode set it to Fill...
To autosize the columns to fit the data (width-wise) and then autosize the form to fit the gridview (width-wise), use the following code:
foreach (DataGridViewColumn column in dataGridView1.Columns)
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
this.Width = dataGridView1.Width + 100;
where dataGridView1 is the name of the Datagridview in this example, and this is refering to the instance of the form. The 100 is a constant for how many pixels wider you want the form than the datagridview. (note: you may want some width checking to make sure you form and data gridview aren't wider than the user's screen)
To autosize the datagridview to fit the rows (height-wise) and then autosize the form to fit the gridview (height-wise), use the following code:
int totalRowHeight = dataGridView1.ColumnHeadersHeight;
foreach (DataGridViewRow row in dataGridView1.Rows)
totalRowHeight += row.Height;
dataGridView1.Height = totalRowHeight;
this.Height = dataGridView1.Height + 100;
where dataGridView1 is the name of the Datagridview in this example, and this is refering to the instance of the form. The 100 is a constant for how many pixels higher you want the form than the datagridview. (note: you may want some height checking to make sure you form and data gridview aren't taller than the user's screen)
First, your form would have to be big enough for the DataGridView to expand and show all of the columns. In the screenshot, it doesn't look wide enough. You can set the size of the datagridview manually, but it is rare that you know the exact width of each column at design time because you don't know how much will be in each column at run time. For example, your Invoicedescription column could have strings that vary in length.
What I typically do in cases like yours is to put a splitter control on the form. The text box at the top and the Import Invoices button would go in the top panel of the splitter control, and the DataGridView would go in the bottom panel. Then, set the Dock property of the DataGridView to fill the bottom panel of the splitter control. This way, when the user resizes the form the DataGridView will grow/shrink with it.
You can further control the way the columns appear by setting the DataGridView.AutoResizeRows property.
Just giving this as an option, I'm not aware of any way to have the grid automatically resize to the data it is displaying. You may be able to calculate the height/width of the rows and then manually resize the grid in code, but I would make sure I really needed that requirement first.
This works assuming that all the rows have the same height, even when the first header row has different height (for example long headers are wrapped)
If List.RowCount = 0 Then
List.Height = 0
ElseIf List.RowCount = 1 Then
List.Height = List.ColumnHeadersHeight
Else
List.Height = List.ColumnHeadersHeight + List.Rows(1).Height * (List.RowCount + 1)
End If
Set the datagridviewautosizecolumn to "All"
Then, in the form that contains the datagridview, assuming you populate the data grid view in the load event handler or some other event handler as desired wherever the data is loaded (must be after data is loaded) type: (VB)
Me.Width = DataGridView1.Width + 50 ' Number of pixels wider than the datagridview you want the form to be.
datagridview1 is the name of your datagridview control.
Me refers to the form that contains the datagridview control.
You may need to either maximize the window or anchor it to the top and left to ensure fitment depending on how large of a dataset is being populated.
I know this is probably already answered but just in case I post here what helped me to autosize the whole datagrid view by AllCells:
After your Datasource load, paste the following
dataGridView1.AutoResizeColumns();
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
i used this in IronPyhton to fit the table
1: LayoutPanel.AutoSize = True
2: dataGridView.AutoSize = True
3: self.AutoSize = True

Categories

Resources