TableLayoutPanel fill top-to-bottom instead of left-to-right? - c#

Is it possible to have a System.Windows.Forms.TableLayoutPanel fill a column before moving to the next one? Regardless of how I set the RowCount, ColumnCount, and GrowStyle, the table is always populated like this:
1 2 3
4 5 6
and I want this:
1 3 5
2 4 6
Can a TableLayoutPanel not automatically do this?
Edit - To clarify, I am adding the controls programmatically.

Not much you can do about it. It's just how TableLayoutControlCollection.Add is implemented internally. I see two options:
Use TableLayoutControlCollection.Add(Control, column, row). But that's quite a hassle, because you need to calculate the column/row values each time you add a control.
Create a derived control that overrides Add and implement it as the overload above - but now internally.

Related

UniformGrid Layout

I know that Uniform grid does not support row span, but its somehow possible by overriding a method from what i read on Google, but i could really use an example how can i do this. What i want to achieve is a layout similar to this:
----------
| 1 | 2 |
----------
| 3 |
----------
So basically i want the third children to be spanned over 2 columns. I could use a grid but sadly i don't know any method how can i do this because all the children's are created dynamically and i cannot specify in witch cell the child will be put.
I think that you'd be better off using a Grid control. Although you say that you cannot specify in which cell the child will be put, I would try to fix that problem, rather than to create a new control. If you can fix that issue, then this might help you.
To populate a particular row in code, you can use the Grid.SetRow method and to populate a particular column, you can use the Grid.SetColumn method:
Grid.SetRow(rectangle, 1);
Grid.SetColumn(rectangle, 1);
Likewise, to set a row span, you can use the Grid.SetRowSpan method and to set a column span, you can use the Grid.SetColumnSpan method:
Grid.SetRowSpan(rectangle, 2);
Grid.SetColumnSpan(rectangle, 2);
Please see the Grid.SetRow Method, Grid.SetRowSpan Method , Grid.SetColumn Method and Grid.SetColumnSpan Method pages on MSDN for a full example on this.

Dynamic symmetry grid

I am developing a WPF application.
I have list of buttons (for example).
The list is created on run time, and on design time I don't know how many elements(buttons) are going to be in the list.
I want to order the elements in a symmetric way based on the number of buttons.
For example:
If the list contains 4 buttons, I want the grid to have 2 rows and 2
columns.
If the list contains 6 buttons, I want the grid to have 2 rows and 3
columns.
If the list contains 9 buttons, I want the grid to have 3 rows and 3
columns.
Is there a way to create this kind of grid?
What panel should I be using?
You can use WPF Grid container control. Add RowDefinitions and ColumnDefinitions to the Grid as per your requirement.
Another approach is to use a WrapPanel with a fix ItemHeight and ItemWidth.

how to get 2 lines in datagrid or which control to use in Winforms. so in stead of 1 line use 2 line

I like to have 2 lines (rows) in datagrid, which is together. So normally in a grid you have 1 line per row and every next line/row is independent... But I like to have 2 lines. e.g. 1st row the name,address and 2nd line some amounts, or dates.... so every 2 line is together..
what control do I need to use in Winforms to achieve this... I'm newbie, so I prefer simple solution...
e.g. below I like to use (amount fields should be shown on next line in Datagrid)
DocNumber DocType DueDate SupplierNo SupplierName
Amount VATAmount Total Amount
10000 SA 01-05-2012 1025 Supplier-A
12.25 0.25 12.25
10001 SA 12-12-2014 1095 Supplier-B
42.25 5.25 47.00
10002 SB 31-11-2012 1099 Supplier-C
152.25 55.25 192.00
You could add an extra column to your data (i.e DataTable, for instance) where you specify the row order, and then hide it in your control (a DataGridView for instance).
However this has several drawbacks the first one that comes to mind is that if you click on column headers to sort the grid contents, it will not preserve the order of your rows.
Hope this helps.
Well sounds like to me you need to "group" your data if that's what I'm reading right.
However if this is the case I'm afraid Winforms DGVs do not support this as default as far as my understanding is.
I found this link but havent tried it, looks to be a possible work around/hack for it.
Group Functionality for Winforms Datagridviews
On the plus side there are alot of 3rd party options such as:
Telerik Controls
Or my Personnel favorite :
DevExpress Controls
Beware though even though u can do trials, You may have to pay for publishing, but worth a look!
Hope this helps!

How to set default column size(height,width) of columns of Data Grid View in C#

I have a data table. After creating this table, I use data grid view to show data by simply using the code:
mydataGridView.DataSource = myTable;
By this, dataGridView creates columns and rows according to data from myTable automatically.
However, when I do that, I can't set the width of columns in dataGridView. What should i do to fix this?
I don't use any code for setting size. My dataGridView auto generates the columns and i want to set the size of these auto generated columns.
Well... into the xaml of your DataGrid, you have the ColumnWidth parameter that does just that.
There's a gotcha in this one:
MyGrid.TableStyles(0).GridColumnStyles(2).width = SomeValue;
Pretty sure you always get at least one TableStyle and one ColumnStyle on instantiating the grid.
After that you have to create and add a column style for each column.
Same for Rowstyle if you need it as well.
If you style a grid from VS, set number of columns, only one columnstyle will be defined and it will be used for all columns from 0 onwards.
If you wantedf to make column 3 different then you have to set up columns 0,1 and 2. Then 3 and unless you want columns 4 .. n to be the same as three, a column 4 one to be used from then onwards.
Caught me out as well.
So you'll need code as far as I'm aware, you can make it generic though if you can pass in a list of style properties you want to use.
You can try:
dataGridView.Columns[i].Width = 60;

Setting tabindex in dynamic table vertically then horizontally

I have a page that is currently generating dynamically created textboxes in a table format. The users are requesting that the tab order be changed from horizontal-vertical to vertical-horizontal. I know that you can use the tabindex attribute to control the tab ordering, but I can't for the life of me figure out the right way to get the sequential number properly for the textboxes. I guess this is more of a math question than anything else!
FYI, the textboxes are made while looping two different collections. First collection looped to make the rows, for each row, second collection (which is a property of the first collection objects) is looped to create the columns.
Any help would be greatly appreciated. Thanks!
You will need three variables to get this to work:
Total number of rows
Current row index
Current column index
You can then achieve the vertical tab order by setting the tabindex to:
totalRowCount * currentColumnIndex + currentRowIndex + 1
For a five-row, three-column table the above calculation would render the tab order as:
1 5 10
2 6 11
3 7 12
4 8 13
5 9 14
Updated: Wil, thanks for pointing that out. I've updated the example. I also checked the W3C spec (which I probably should have done first) which clarifies the behavior:
elements that do not support the
tabindex attribute or support it and
assign it a value of "0" are navigated
next. These elements are navigated in
the order they appear in the character
stream.

Categories