get actual column width - c#

I am building a DataGridView table programmatically and placing it within a GroupBox on a form.
I insert the data, then resize the columns automatically to fit the data. I then want to resize the groupbox to the size of the DataGridView. For each column and row, I get their respective width and height, which technically should be accurate, and update the panel and overall DataGridView sizes.
The problem is that Column.Width always returns 100 pixels regardless of its actual size (see screenshot: actual column width is around 30px, not 100). If I manually enter width = 90 pixels, the resizing is quite accurate!
matrix = new DataGridView();
//modify behaviour
matrix.ColumnHeadersVisible = false;
matrix.AllowUserToResizeColumns = false;
matrix.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
matrix.RowHeadersVisible = false;
matrix.AllowUserToResizeRows = false;
//modify positioning
matrix.Location = new Point(10, 20);
//matrix.Anchor = (AnchorStyles)(AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right);
matrix.Dock = DockStyle.Fill;
//set the size of the matrix
matrix.ColumnCount = col;
matrix.RowCount = row;
//Data now inserted...
matrix.AutoResizeColumns(); //correctly resizes the columns
int height = 0;
foreach (DataGridViewRow row in matrix.Rows)
{
height += row.Height;
}
height += matrix.ColumnHeadersHeight;
int width = 0;
foreach (DataGridViewColumn col in matrix.Columns)
{
width += col.Width;
//PROBLEM: Width always = 100 pixels.
}
width += matrix.RowHeadersWidth;
//width = 90; //override width manually
matrix.Size = new Size(width + 2, height + 2);
panel.Size = new Size(matrix.Width, matrix.Height);
the panel is so large because width isn't 90px but 357 roughly, which is wrong!
EDIT: PARTIAL FIX
I have found a way to get the correct width of a cell:
DataGridView.Rows[0].Cells[0].ContentBounds.Width
//ContentBounds = a rectangle with the exact dimensions of that cell
I can now set the DataGridView the correct size, but only if it's not Docked to Fill. Setting matrix.Dock = DockStyle.Fill prevents the resizing to occur correctly.

I guess that is because you're autosizing your columns in row:
matrix.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
try to change it to:
matrix.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;

I have figured out what the problem is:
DataGridView.Dock = DockStyle.Fill prevented the DataGridView.AutoResizeColumns() method from doing anything.
Rectangle rect = matrix.Rows[0].Cells[0].ContentBounds getting the dimensions of the rectangle containing the cell values didn't update after AutoResizeColumns()
I tried setting the columns manually, but as heq suggested, the AutoSizeColumnsMode must be set to None in order to be able to modify the width.
I adjusted the width of each column manually, to be 10px for each character inside the cell
Final Solution
/* Create the DataGridView */
matrix = new DataGridView();
//modify behaviour
matrix.ColumnHeadersVisible = false;
// matrix.columnhea
matrix.AllowUserToResizeColumns = false;
matrix.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
matrix.RowHeadersVisible = false;
matrix.AllowUserToResizeRows = false;
matrix.ReadOnly = true;
//modify positioning
matrix.Location = new Point(10, 20);
//matrix.Dock = DockStyle.Fill;
matrix.Anchor = (AnchorStyles)(AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right);
//set the size of the matrix
matrix.ColumnCount = cols;
matrix.RowCount = rows;
panel.Controls.Add(matrix);
/* Set the data contents (not shown) */
...
/* Adjust the width of columns */
int width = 0;
int height = 0;
for(int c = 0; c < cols; c++)
{
int largest = 0; //largest number of characters in cell
for(int r = 0; r < rows; r++)
{
int len = matrix.Rows[r].Cells[c].Value.ToString().Length;
if (len > largest)
{
largest = len;
}
}
matrix.Columns[c].Width = largest * 10;
width += largest * 10;
}
/* Get height of table */
foreach (DataGridViewRow row in matrix.Rows)
{
height += row.Height;
}
/* Set the DataGridView Size and Parent Panel size */
//Note: first set the parent panel width, the table is anchored to all of its edges
// as such table wont resize correctly if the panel isn't up to size first
panel.Size = new Size(width + 20, height + 30);
matrix.Size = new Size(width + 3, height + 3); //is it 1px per row and col?
/* Parent Panel settings kept to default, no modifications */

Related

TableLayoutPanel column won't occupy 100% width

I have an issue with TableLayoutPanel. Despite using a single column with 100% width, the control I add keeps the size assigned to it in layout creator.
TableLayoutPanel panel = someTableLayoutPanel;
panel.RowStyles.Clear();
panel.ColumnStyles.Clear();
panel.Controls.Clear();
ColumnStyle columnStyle = new ColumnStyle(SizeType.Percent, 100F);
panel.ColumnStyles.Add(columnStyle);
int count = capabilityListManager.DeviceCount();
panel.RowCount = deviceCount;
panel.ColumnCount = 1;
for (int index = 0; index < count; index++)
{
RowStyle rowStyle = new RowStyle(SizeType.Absolute, 120F);
panel.RowStyles.Add(rowStyle);
SomeCustomControl cell = new SomeCustomControl();
panel.Controls.Add(cell, 0, index);
}
I assumed that would be enough for the control to always be resized to have fixed 120 pixels height and take 100% of width provided by TableLayoutPanel, however the result looks like this:
My custom control is the one with the border, and the TableLayoutPanel has blueish hue - clearly there's space left for the control to occupy.
SomeCustomControl has its AutoSize property set to false. What am I missing?

Windows forms C# how to make the grid lines of a chart square?

I have a chart in windows forms and I want the gridlines to be squared. The gridline is anchored to bottom, top, left and right so it resizes with the screen. How do I make the grid lines always square and make the whole chart resize with the screen?
I have tried setting the width and height to be the same, but it doesn't work since the series names of the chart are on the right.
EDIT 1:
Here is the full uncensored code:
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = max;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = max;
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "0";
chart1.ChartAreas[0].AxisY.LabelStyle.Format = "0";
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisY.Interval = 1;
chart1.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount;
chart1.ChartAreas[0].RecalculateAxesScale();
for (int i = 0; i < points.ToArray().Length; i++)
dt.Rows.Add(pointsArr[i, 0], pointsArr[i, 1]);
chart1.DataSource = dt;
chart1.Series["תחום הפתרונות האפשריים"].BorderWidth = 0;
float[] OptimalPoint = CalculateOptimalPt(convertEq(z), ListArToAr(points));
if (OptimalPoint[0] == 0)
{
for (int i = 0; i < 2; i++)
{
DataPoint dp = new DataPoint();
dp.SetValueXY(i, OptimalPoint[1]);
if (i > 0) dp.Color = Color.Transparent;
chart1.Series["פיתרון אופטימלי"].Points.Add(dp);
}
}
else
chart1.Series["פיתרון אופטימלי"].Points.AddXY(OptimalPoint[0], OptimalPoint[1]);
chart1.Series["פיתרון אופטימלי"].Points[0].MarkerSize = 10;
chart1.Series["תחום הפתרונות האפשריים"].XValueMember = "X_Value";
chart1.Series["תחום הפתרונות האפשריים"].YValueMembers = "Y_Value";
chart1.Series["תחום הפתרונות האפשריים"].ChartType = SeriesChartType.Area;
panel1.Visible = false;
panel2.Visible = true;
You can do it by anchoring the chart only to the Top and Left and calculating and setting the Width and Height yourself when Form size changes.
To do so we get fundamental data of the chart in the form constructor.
private readonly Size _innerMargin = new Size(183, 55); // Estimated
private readonly Size _outerMargin;
private readonly float _aspectRatio;
public Form1()
{
InitializeComponent();
_outerMargin = Size - chart1.Size;
Size innerSize = chart1.Size - _innerMargin;
_aspectRatio = (float)innerSize.Width / innerSize.Height;
}
_innerMargin is the estimated total difference between the chart size and the plot area with the gridlines. I actually got it from a screenshot and measured it in a graphics application.
_outerMargin is the difference of the form size and the chart control size.
This calculation of the initial _aspectRatio assumes the grid lines build perfect squares when the form opens. Instead, you could set this aspect ratio from the known number of squares in X and Y:
_aspectRatio = 16f / 16f; // From your example image.
In the Form_Resize event handler, we then set the new size of the chart. Depending on whether the current aspect ratio (calculated from the theoretical new maximum plot area size) is less than or greater than the original aspect ratio, the height or the width of the chart determines the maximum chart size. The other dimension must be calculated so that the aspect ratio of plot area remains the same.
private void Form1_Resize(object sender, EventArgs e)
{
Size maxChartSize = Size - _outerMargin;
Size innerSize = maxChartSize - _innerMargin;
double currentAspectRatio = (float)innerSize.Width / innerSize.Height;
if (currentAspectRatio < _aspectRatio) {
int chartWidth = Width - _outerMargin.Width;
chart1.Width = chartWidth;
chart1.Height = (int)((chartWidth - _innerMargin.Width) / _aspectRatio + _innerMargin.Height);
} else {
int chartHeight = Height - _outerMargin.Height;
chart1.Height = chartHeight;
chart1.Width = (int)((chartHeight - _innerMargin.Height) * _aspectRatio + _innerMargin.Width);
}
}

Matrix of photos

So I have a code that injects an image into my project through resources Image foodWorld = Resources.orange and I want to make a matrix out of this photo, so it can look like this:
I have this code but I don't know how to draw the matrix. Also, I don't know if this is the right way to draw it or not:
this.Width = 400;
this.Height = 300;
Bitmap b = new Bitmap(this.Width, this.Height);
for(int i = 0; i < this.Height; i++)
{
for(int j = 0; j < this.Width; j ++)
{
//fill the matrix
}
}
I am not too familiar with WinForms, but in WPF, I'd do it this way:
var columns = 15;
var rows = 10;
var imageWidth = 32;
var imageHeight = 32;
var grid = new Grid();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
//Get the image in your project; I'm not sure how this is done in WinForms
var b = new Bitmap(imageWidth, imageHeight);
//Display it
var pictureBox = new PictureBox();
pictureBox.Image = b;
//Set the position
Grid.SetColumn(j, pictureBox);
Grid.SetRow(i, pictureBox);
//Insert into the "matrix"
grid.Children.Add(pictureBox);
}
}
For moving Pacman, repeat the above, but for only one image. Store a reference to the current position and when certain keys are pressed,
Animate it's margin until it appears to be in an adjacent cell (for instance, if each cell is 16 pixels wide and pacman should be in the center of any given cell, animate the right margin by 16 pixels to get it into the cell on the right and so forth).
Once it has moved to another cell, set the new row and column based on the direction in which it last moved.
If there is a fruit at the new position, get the fruit at that position and remove it from the Grid. You can get it by using myGrid.Children[currentRow * totalColumns + currentColumn] assuming currentRow and currentColumn are both zero-based.
Repeat for each cell it must move to.
This does mean the matrix will have a fixed size, but in WPF, there is a Viewbox, which is convenient for these types of scenarios. Also, set the z-index of pacman to be greater than the fruits so it's always on top.

C# Unable to dynamicly auto-size columns evenly in TableLayoutPanel

I have a WinForm that has a TableLayoutPanel control. My code will detect the number of attached monitors on screen, create a column per monitor, and then add a button for each display within each individual column in the TableLayoutControl so I can ensure that no matter how many monitors are attached, the buttons will appear "centered" across the form. One/two monitors renders just fine, however three monitors results in end columns not being evenly distributed.
Here is my code:
int count = Screen.AllScreens.Count();
this.monitorLayoutPanel.ColumnCount = count;
ColumnStyle cs = new ColumnStyle(SizeType.Percent, 100 / count);
this.monitorLayoutPanel.ColumnStyles.Add(cs);
this.monitorLayoutPanel.AutoSize = true;
var buttonSize = new Size(95, 75);
int z = 0;
foreach (var screen in Screen.AllScreens.OrderBy(i => i.Bounds.X))
{
Button monitor = new Button
{
Name = "Monitor" + screen,
AutoSize = true,
Size = buttonSize,
BackgroundImageLayout = ImageLayout.Stretch,
BackgroundImage = Properties.Resources.display_enabled,
TextAlign = ContentAlignment.MiddleCenter,
Font = new Font("Segoe UI", 10, FontStyle.Bold),
ForeColor = Color.White,
BackColor = Color.Transparent,
Text = screen.Bounds.Width + "x" + screen.Bounds.Height,
Anchor = System.Windows.Forms.AnchorStyles.None
};
this.monitorLayoutPanel.Controls.Add(monitor, z, 0);
z++;
monitor.MouseClick += new MouseEventHandler(monitor_Click);
}
I've tried making the buttons smaller, and increased the form size but the last column is always smaller than the first two. I can't understand it!
Clear ColumnStyles first.
this.monitorLayoutPanel.ColumnStyles.Clear();
then:
int count = Screen.AllScreens.Count();
for (int i = 0; i < count; i++)
{
ColumnStyle cs = new ColumnStyle(SizeType.Percent, (float)100 / count);
this.monitorLayoutPanel.ColumnStyles.Add(cs);
}
this.monitorLayoutPanel.AutoSize = true;
...
Reza Aghaei pointed me to this thread How to create a magic square using Windows Forms? which pointed me in the right direction. Updated (and working) code below. :)
int screens = Screen.AllScreens.Count();
this.monitorLayoutPanel.ColumnStyles.Clear();
this.monitorLayoutPanel.ColumnCount = screens;
this.monitorLayoutPanel.AutoSize = true;
int z = 0;
foreach (var screen in Screen.AllScreens.OrderBy(i => i.Bounds.X))
{
var percent = 100f / screens;
this.monitorLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, percent));
Button monitor = new Button
{
Name = "Monitor" + screen,
Size = new Size(95, 75),
BackgroundImageLayout = ImageLayout.Stretch,
BackgroundImage = Properties.Resources.display_enabled,
TextAlign = ContentAlignment.MiddleCenter,
Font = new Font("Segoe UI", 10, FontStyle.Bold),
ForeColor = Color.White,
BackColor = Color.Transparent,
Text = screen.Bounds.Width + "x" + screen.Bounds.Height,
Anchor = System.Windows.Forms.AnchorStyles.None
};
this.monitorLayoutPanel.Controls.Add(monitor, z, 0);
z++;
monitor.MouseClick += new MouseEventHandler(monitor_Click);

Find the Coordinates of a cell top left ? Working with EPPlus

I am trying to map a image into a excel spread sheet and i need to find what the coordinates of the current cell are?
My code at the moment loops though each cell till it finds a matching tag in the excel where it knows this is the cell to place the picture.
I have the cell i just have no idea how to obtain the cells top and left properties?
foreach (ExcelRangeBase cell in range1)
{
}
Please help!
Thanks in advance.
int row = cellAddress.Start.Row;
int column = cellAddress.Start.Column;
if (mCellsResult.Count() > 0)
{
var mCells = mCellsResult.First();
//if the cell and the merged cell do not share a common start address then skip this cell as it's already been covered by a previous item
if (mCells.Start.Address != cellAddress.Start.Address)
continue;
if (mCells.Start.Column != mCells.End.Column)
{
colSpan += mCells.End.Column - mCells.Start.Column;
}
if (mCells.Start.Row != mCells.End.Row)
{
rowSpan += mCells.End.Row - mCells.Start.Row;
}
}
double height = 0, width = 0;
for (int h = 0; h < rowSpan; h++)
{
height += xlWorkSeet1[k].Row(row + h).Height;
}
for (int w = 0; w < colSpan; w++)
{
width += xlWorkSeet1[k].Column(column + w).Width;
}
double pointToPixel = 0.75;
height /= pointToPixel;
width /= 0.1423;
picture = xlWorkSeet1[k].Drawings.AddPicture(System.Guid.NewGuid().ToString() + row.ToString() + column.ToString(), image);
picture.From.Column = column - 1;
picture.From.Row = row - 1;
picture.SetSize((int)width, (int)height);
Cell.Top and Cell.Left would be nice, but they doesn't exist in EPPlus.
I was inspired by Pomster and made this function:
/// <summary>Get top, left, width and height of a given cell.</summary>
Rectangle GetCellTopLeftCoordinates(ExcelWorksheet worksheet, ExcelRangeBase cell)
{
double top = 0, left = 0, width = 0, height = 0;
//Get top and left position:
for (int i = 1; i < cell.Start.Row; i++)
top += worksheet.Row(i).Height;
for (int i = 1; i < cell.Start.Column; i++)
left += worksheet.Column(i).Width;
//Get width and height:
if (cell.Merge) //Then the cell are merged with others:
{
foreach (string address in worksheet.MergedCells) //Loop through all merged cells:
if (Intersect(worksheet.Cells[address], cell)) //Then we have found the particular, merged range:
{
ExcelRange range = worksheet.SelectedRange[address];
for (int i = range.Start.Row; i <= range.End.Row; i++)
height += worksheet.Row(i).Height;
for (int i = range.Start.Column; i <= range.End.Column; i++)
width += worksheet.Column(i).Width;
break;
}
}
else //No merges - just get dimensions:
{
width = worksheet.Column(cell.Start.Column).Width;
height = worksheet.Row(cell.Start.Row).Height;
}
//Convert point to pixels:
top /= 0.75;
left /= 0.1423;
height /= 0.75;
width /= 0.1423;
return new Rectangle((int)left, (int)top, (int)width, (int)height);
}
bool Intersect(ExcelRange range, ExcelRangeBase cell)
{
foreach (ExcelRangeBase item in range)
if (item.Address == cell.Address)
return true;
return false;
}
Try cell.top and cell.left, they exist in the excel object model.
How to get the upper-left corner of an excel range:
using Excel = Microsoft.Office.Interop.Excel;
...
// get upper left corner of range defined by a RangeStr like "B2:D4"
Excel.Range UpperLeftCell = (Excel.Range)wsheet.get_Range(RangeStr).Cells[1,1];
where wsheet is a Worksheet object.

Categories