I create some control in codebehind and would like to set its size dynamically.
I can assign numerical values as well as System.Windows.GridLength.Auto, but there is no equivalent to "*".
Is that because the "*" from XAML gets translated into code when the WPF gets parsed?
To give this some detail: There is a grid with three rows. I want the top and bottom row to take all the space they can, while the middle row remains auto-sized.
var gridLength = new GridLength(1, GridUnitType.Star);
More info.
Related
I would like to know how you would change the height of a cell without affecting the whole row. I'm making a timetable form and I need to change the height of a cell depending on the amount of time the event takes.
I'm making this in a clr project and using C++
dataGridView1->Rows[0]->Cells[0]->Style->BackColor = Color::Red;
sets the background colour of the first cell on the first row to red.
Change dataGridView1 to whatever your datagridview is called and set the row and column values as you need them.
You will probably also have to add: - using namespace System::Drawing;
You have to use Style.BackColor instead
foreach (DataGridViewCell cell in dataGridView1.Rows[0].Cells)
cell.Style.BackColor = Color.Red;
I have a customized datagrid control that dynamically creates columns depending on various circumstances.
Originally I was using fixed column sizes as such.
DataGridTextColumn column = new DataGridTextColumn();
column.Width = new DataGridLength(entity.DisplaySize);
This is in a foreach loop. However, we decided that we need to ensure that we never have horizontal scrolling, so I change it to the following so that the columns always fit inside the datagrid and they maintain their size proportionally to each other.
DataGridTextColumn column = new DataGridTextColumn();
column.Width = new DataGridLength(entity.DisplaySize, DataGridLengthUnitType.Star);
However, now when the datagrid draw, or is redrawn, there is flicker. It looks like it is initially creating the datagrid with all columns at the minimum size of 20 and then once all columns are created it expands them to fill the area. This behavior was not present with the previous method of providing precise column widths.
I have played with various virtualization settings to no avail.
Anyone have any ideas please?
How I can move control from one row of Grid to another without remove the control from the first row and then add it to the second row???
Thanks
Assuming you want to do this at run-time, in the code-behind, you can try:
// Assuming your control is not in row 1
myControl.SetValue(Grid.RowProperty, 1)
Why? To get this done easily, you have to change the row that it is in.
Another way is to use math and do calculation of the height of grid, row and control and position it by margin. - very messy code.
I currently have a Textbox on a Windows Forms, and I need to dynamically add a PictureBox box control at the right of the Textbox.
I create the PictureBox programmatically and I when setting the location of the PictureBox, i'm setting like this:
pBox.Location = new Point(tbControl.Location.X + ctrl.Width, ctrl.Location.Y);
So i'm setting the picture box to be located at the X location of the textbox PLUS the width of the textbox. However, since the textbox has an anchor property set to right, its width increases to fill the space between itself and the form border.
Problem is, that even though the textbox's width is visually bigger than the actual value of Textbox.Width. the Width property is not taking into account the extra width of being anchored.
I already tried properties like Textbox.Bounds.Width, Textbox.ClientSize.Width, Textbox.DisplayRectangle.Width, etc. with no luck. All of those properties return the original Width of the control without taking into account the resized width due to the Anchor property.
Does anyone know how I can determine the real size of the textbox? Thank you
The Width property always tracks the current width of a control, whether it is anchored or not. However, the TextBox is going to grow when you make the container larger and that will make it overlap the PictureBox. You have to anchor the PB to the right as well.
These should be returning the adjusted size. Either you are referring to the wrong textbox, or you are doing the query before the size has actually changed.
I ve created columns of my datagrid using this,
private void Receive_Load(object sender, System.EventArgs e)
{
DataGridView1.Columns.Add("Sender",typeof(string));
DataGridView1.Columns.Add("Time",typeof(string));
DataGridView1.Columns.Add("Message",typeof(string));
}
How can i dynamically assign a column width to a winforms datagrid?
I think you are searching for something line
DataGridView1.Columns["ColumnName"].Width = 75;
I Hope it help you.
In addition, you can set the AutoSizeMode of the column to obtain different behaviours automatically. For example, if you set it to ColumnHeader, then the cell width will be set to the best fit for showing the header text. You can get more info in this Link.
Here's a suggestion:
If you know the length of your fields you can multiply their length by a constant value (max character width for example) to produce a dynamic width.