Insert Excel row with normal height - c#

This is my C# code to insert rows into an Excel worksheet:
for (var j = 0; j < dummies.Count; j++)
{
mySheet.Range[myRange].Insert(Excel.XlInsertShiftDirection.xlShiftDown);
}
Nothing too Earth shattering there.
However, if one of the rows below the insert has a different height to the other rows, what seems to happen is that the new row at that position keeps the same height, and so does the row which is shifted down.
So for example, before the code is run, all rows are normal height except 11 which is double the height of the others.
If I insert 10 rows at row 7, the result is that rows 11 and 21 are double height.
Inserting 1 row above makes 11 and 12 double height.
Inserting 2 rows gives the same result for rows 11 and 13, etc.
So the original double-height row always seems to keep its height, even though the height also follows the shifted-down row.
Is there a way I can specify that when I insert a row, the row height should not get left behind?
[edit]
If I insert a row manually it all works fine.
[/edit]

I found out why this is.
In the code above I'm not shifting the entire row down.
This means that the cells that aren't shifting down are keeping their original height, which is affecting the height of all the other cells in the row.
The following works:
for (var j = 0; j < dummies.Count; j++)
{
mySheet.Range[myRange].EntireRow.Insert(Excel.XlInsertShiftDirection.xlShiftDown);
}

Related

Can't read all rows from GUIGridView. Getting blank after 34 rows

I am doing SAP automation, there i am reading data from GUIGridview and adding to a DataTable using a for loop. But after 34 rows every values are empty. Then I tried with setting the current cell using a condition
if (rowindex % 34== 0)
grd.SetCurrentCell(rowindex, grd.FirstVisibleColumn);
After that i am able to get the values of 35, 36th row but again then blank values from there. Total i am having 91 rows.
The backend doesn't send all the grid rows to the frontend, only the ones currently viewed.
You must scroll the grid via the property FirstVisibleRow, and use the other properties RowCount and VisibleRowCount to scroll only when required.
Example:
for (int rowindex = 0; rowindex < grd.RowCount; rowindex = rowindex + 1)
{
// Position at top if first time or scroll to display next page
if ( rowindex % grd.VisibleRowCount == 0 )
{
grd.FirstVisibleRow = rowindex
}
// Process the line
...
}
More information: SAP Library - GuiGridView

Linking the Cells of a spreadsheet like excel to be able to perform formulas c#. Eg: A1 +C8

In Our assignment we were given a task to create a new spreadsheet program with a 26 x 26 grid of textboxes without importing anything and make use of array (in my case I Used array of objects).
I created a 2 d array of objects containing size of grid (27x27). the reason of it being 27 instead of 26 is because the first row and column is to show the ABCDE etc and 12345 etc of the rows and columns.
now the rows indexation I had no problem because it is numeric. however, the letters I solved by creating a string array with alphabet from a to z and entering them through a for loop and it worked.
Now the next step is to be able to link the cells, however, I am a bit stumped, because people told me I have to use Ascii or smthin.
Can anyone help me plz on how to achieve the cell links?
I have been able to past the name of each cell using this code but I fear I just filled the .Text of the cells not the cells name per se:
for (int x = 1; x < TwoDArrayCells.GetLength(0); x++) //nested loop to create the grid of textboxes.
{
for (int y = 1; y < TwoDArrayCells.GetLength(1); y++)
{
Cells CellFields = new Cells(Name = $"{AlphabetRow[x]}{y}", x, y + 3); // called a new cell named CellFields.
CellFields.Text = Name;//Change to location if you wise to take
this.Controls.Add(CellFields);
}
}
The result I would like is to be able to after this be able to link cells. meaning if I insert in a text box A1 + A2 it knows that a1 is the first box in the first row of first column and the a2 is the box in the second row of the first column.
Assuming that Columns can be A-Z only (Capitals), You can extract the column index and row index as follows:
Given a cell, string cell = "A2";
- the column index: int column = cell[0] - 'A' + 1;
- the row index: int row = Convert.ToInt32(cell.Substring(1));
Explanation: for the column, take the first letter, subtract it from 'A' (the subtraction will be performed on the assci codes of the characters) and add 1 since your actual cells are stored starting from index 1. for the row, just parse the rest of the string to int.

how to calculate the total value of a row in a gridview?

I have a dynamically created gridview that displays different pieces and their quantity. I want to get the total of these rows to display the total in the units column. So for example the value in the Unit column on the first row should be 190 (12 + 7 + 136 + 35)
The problem is the number of pieces can be different for each user. The example in the image has 13 pieces, but a different user might only have 5.
The units column will always be in the 9th position. So is there a way to check the value of each column after the unit column, till it reaches the end and then add those values together?
EDIT
main.Columns.Add("Units", typeof(int)).SetOrdinal(8);
foreach (DataRow row in main.Rows)
{
int total = 0;
for (int i = 9; i < main.Columns.Count - 1; i++)
{
total += row[i];
}
row["Units"] = total;
}

C# listview optimum column width

I have a C# ListView and I'd like to autosize the column widths based on the contents of the column.
I know that setting the column width to -1 will size the column at the length of the widest member.
I know that setting the column width to -2 will size the column at the length of the column header.
How do I size the column to be the greater of the two?
I could do something like this:
for (int i = 0; i < listView.Columns.Count; ++i)
{
listView.Columns[i].Width = -1;
int width1 = listView.Columns[i].Width;
listView.Columns[i].Width = -2;
if (width1 > listView.Columns[i].Width)
listView.Columns[i].Width = -1;
}
but it does seem fabulously inefficient.
Does anyone have an answer?
You should try
ListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
even though it says "ColumnHeaderAutoResizeStyle.HeaderSize" it should auto resize to the largest item in the column, whether it's the header or a column member

Windows Forms Top Property Bug

I am trying to populate a container with any number of controls that have the same height and width. I allow this container to be shrunk or grown by the user and the container will organize the controls so that it fits the most controls on one row as possible. Here is the code to organize it:
int row = 0;
int column = 0;
for (int i = 1; i <= controls.Count; i++)
{
controls.Values[i-1].Top = row * controls.Values[0].Height;
controls.Values[i-1].Left = column * controls.Values[0].Width;
if (i % controlsPerRow == 0)
{
// This finishes a row
row++;
column = 0;
}
else
{
column++;
}
}
The problem i run into is that on the first iteration of the loop, I will be multiplying the control height by the row and assigning that value to the control Top property. The first row is 0 and the first height is 165. 0 * 165 = 0, but the Top property contains a magical -20 after assigning the 0.
Anyone have any idea how this can happen?
You're trying to rewrite the FlowLayoutPanel.
Consider using it instead.
Also, it looks like your controls field is a Dictionary<Something, Control>.
Be aware that the iteration order of Dictionary.Values is not guaranteed, meaning that you aren't looping over the controls in the order that they were added to the dictionary.

Categories