Alternating row-column headers in worksheet with EPPlus - c#

I am trying to print output from a custom structure into Excel file. What I am trying to do is:
COLUMN NAME
PLAYER
PLAYER
COLUMN NAME
PLAYER
PLAYER
But What I am getting is
COLUMN NAME
COLUMN NAME
PLAYER
PLAYER
CODE:
int i = 1;
foreach (var item in scoresheet.Positions)
{
var column = wb.Cells;
if ((i % 5) == 1)
{
column["A" + i].LoadFromText("Initial Rank");
}
else
{
column["A" + i + 1].LoadFromText(item.Player.FirstName);
}
i++;
}

Looking at your line:
column["A" + i + 1].LoadFromText(item.Player.FirstName);
I assume you want that to be, say, a result of "A2" when i is equal to 1. BUT, what you will actually end up with is "A11" since it will do it as a string concatenation. What you need is:
column["A" + (i + 1)].LoadFromText(item.Player.FirstName);

Related

How to get data out of a foreach and reuse in while and do while in c#?

In the Foreach the data has loop exactly but when the data send to while, It display single character only of each data for example: exact data is: "John" but in my code it display: J then next O, H, N until each character of the exact data is finished. Code example below:
foreach (DataGridViewRow row in DataGrid2.Rows)
{
bool isSelected = Convert.ToBoolean(row.Cells["ChkOkay"].Value);
if (isSelected)
{
value += row.Cells["EnrollName"].Value.ToString();
Console.Writeline("Selected Values " + value);
}
}
while (v < value.Length())
{
do
{
//It display single character only instead whole name.
MessageBox.Show("Name: "value[v] + "Slot: " + u);
foreach (var chari in value[v].ToString())
{
//I re use the data here from 1st foreach
}
v++;
u += 51;
} while (u < 5559);
I did not understand your code, but the correct form is to take a column of DataGrid rows and place it in a list.
You then print the list elements and then check the characters of each list item.
List<string> EnrollNameList = new List<string>();
foreach (DataGridViewRow row in DataGrid2.Rows)
{
bool isSelected = Convert.ToBoolean(row.Cells["ChkOkay"].Value);
if (isSelected)
{
string colValue = row.Cells["EnrollName"].Value.ToString();
EnrollNameList.Add(colValue);
Console.WriteLine("Selected Values " + colValue);
}
}
int v = 0;
while (v < EnrollNameList.Count)
{
//message box show whole EnrollName
MessageBox.Show("Name: " + EnrollNameList[v]);
foreach (var chari in EnrollNameList[v])
{
//get each char in EnrollName
}
v++;
}
strings can be indexed like arrays. Treating a string like an array (and adding an indexer in square brackets) returns the char at that position in the string.. "John"[0] is 'J'. Your value is a single string, not an array of strings.
With this:
value += row.Cells["EnrollName"].Value.ToString();
You're making a string value a longer and longer string. If your grid has 3 rows containing "mark", "luke" and "john", your value ends up as "marklukejohn"
If you then loop over it, you messagebox it out a char at a time
I suspect you want to retain the strings as whole strings (3 of them) in an array (or list).. in which case:
var names = new List<string>();
foreach ....
names.Add(row.Cells["EnrollName"].Value.ToString())
Then you can later loop over the list and pull the names out as complete strings
while (v < names.Count)
{
MessageBox.Show("Name: " + names[v] + "Slot: " + u);
Your code is full of syntax errors and won't compile. Do try to get code working before you post it unless the question is about a compiler error

Minitab Automation and Skipping Columns

My main code is below:
Mtb.Application MtbApp = new Mtb.Application();
MtbApp.UserInterface.Visible = true;
MtbApp.UserInterface.DisplayAlerts = false;
Mtb.Project MtbProj = MtbApp.ActiveProject;
Mtb.Columns MtbColumns;
Mtb.Column MtbColumn1;
Double[] data1;
Hashtable htSingleColumn;
List<double> listSingleColumn;
int i = 1 ;
foreach (DictionaryEntry de in htDataTable)
{
htSingleColumn = (Hashtable)de.Value;
listSingleColumn = (List<double>)htSingleColumn["listSingleData"];
data1 = listSingleColumn.ToArray();
MtbColumns = MtbProj.ActiveWorksheet.Columns;
MtbColumn1 = MtbColumns.Add(null, null, i);
MtbColumn1.SetData(data1);
// strLowlim and strUpplim have no influence on this issue here
strCommand = "Capa C" + i+" 1;" + ((strLowlim == "NA") ? "" : (" Lspec " + strLowlim + ";")) +((strUpplim == "NA") ? "" : (" Uspec " + strUpplim + ";"))+ " Pooled; AMR; UnBiased; OBiased; Toler 6; Within; Percent; CStat.";
// The program is crashing here as a result of the columns not being created sequentially
MtbProj.ExecuteCommand(strCommand);
Mtb.Graph MtbGraph = MtbProj.Commands.Item(i).Outputs.Item(1).Graph;
MtbGraph.SaveAs("C:\\MyGraph" + DateTime.Now.ToString("yyyy-MM-dd HHmmss"), true, Mtb.MtbGraphFileTypes.GFPNGHighColor);
i++;
}
MtbApp.Quit();
When running this code (with the crashing section commented out), I get the following output:
It should look like this:
I am really puzzled about this result. The variable i is right, but what is affecting the column number?
I can't find much information on the Web about Minitab. I just read the start guide here.
This line is the problem.
MtbColumn1 = MtbColumns.Add(null, null, i);
The third parameter Quantity specifies the number of columns to add. On the first iteration of the loop, you add i = 1 column, but on the second iteration of the loop you add i = 2 columns. Each iteration of the loop will add an additional i columns, when what you really want is to add one column each time.
Change the line to:
MtbColumn1 = MtbColumns.Add();

Count of text in RichtextBox (Line and Column)

I'm working on a code editor and I just want to know how to do codes in counting Lines and Columns in richtextbox. Particularly something like this one in actual code editor:
Let's just say count will transfer in a ListBox.
Is there a fast way I can do it?
You can do this :
//This to get lines number.
int index = richTextBox.SelectionStart;
int li = richTextBox.GetLineFromCharIndex(index);
// This to get columns number.
int firstChar = richTextBox.GetFirstCharIndexFromLine(li);
int col = index - firstChar;
Good luck!
This will do it, you just have to call the code inside a timer:
int line = 1 + richTextBox1.GetLineFromCharIndex(richTextBox1.GetFirstCharIndexOfCurrentLine());
int column = 1 + richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexOfCurrentLine();
label1.Text = "line: " + line.ToString() + ", column: " + column.ToString();

Data lost while adding string to listbox

I am cycling through the contents of a two-dimensional array containing the result of a Punnett Square calculation for gene crosses. I need to summarize the result so that the user can readily see the unique instances. I can accomplish this by putting the result into a text box, but when I try and use a ListBox to display the data, part of the information is getting lost, namely a translation of the AaBBCc type data to something that directly relates to the traits that the user initially selected.
This is the main block of code for the operation:
foreach (string strCombination in arrUniqueCombinations)
{
int intUniqueCount = 0;
decimal decPercentage;
foreach (string strCrossResult in arrPunnettSQ)
{
if (strCrossResult == strCombination)
{
intUniqueCount++;
}
}
decPercentage = Convert.ToDecimal((intUniqueCount*100)) / Convert.ToDecimal(intPossibleCombinations);
txtReport.AppendText(strCombination + " appears " + intUniqueCount.ToString() + " times or " + decPercentage.ToString() + "%."+ Environment.NewLine);
lstCrossResult.Items.Add(DecodeGenome(strCombination) + " appears " + intUniqueCount.ToString() + " times or " + decPercentage.ToString() + "%.");
}
For appending the data to the textbox I use this code and it works perfectly:
txtReport.AppendText(DecodeGenome(strCombination) + " appears " + intUniqueCount.ToString() + " times or " + decPercentage.ToString() + "%."+ Environment.NewLine);
Giving the result:
Trait 1 Het.,Trait 3 appears 16 times or 25%.
For adding the result to a list box, this works:
lstCrossResult.Items.Add(strCombination + " appears " + intUniqueCount.ToString() + " times or " + decPercentage.ToString() + "%.");
Giving the result:
AaBBCc appears 16 times or 25%.
But the contents of strCombination is AaBBCc and I need it translated to "Trait 1 Het.,Trait 3", which I accomplish with this bit of code:
private string DecodeGenome(string strGenome)
{
string strTranslation = "";
int intLength = strGenome.Length;
int intCounter = intLength / 2;
string[] arrPairs = new string[intLength / 2];
//Break out trait pairs and load into array
for (int i = 1; i <= intLength; i++)
{
arrPairs[i / 2] = strGenome.Substring((i-1),2);
i++;
}
foreach (string strPair in arrPairs)
{
char chFirstLetter = strPair[0];
char chSecondLetter = strPair[1];
intCounter = intCounter - 1;
if (Char.IsUpper(chFirstLetter))
{
if (!Char.IsUpper(chSecondLetter))
{
if (intCounter > 0)
{
txtReport.AppendText(GetDescription(strPair.Substring(0, 1)) + " Het.,");
}
else
{
txtReport.AppendText(GetDescription(strPair.Substring(0, 1)));
}
}
}
else
{
if (!Char.IsUpper(chSecondLetter))
{
if (intCounter > 0)
{
txtReport.AppendText(GetDescription(strPair.Substring(0, 1)) + ",");
}
else
{
txtReport.AppendText(GetDescription(strPair.Substring(0, 1)));
}
}
}
}
return strTranslation;
}
That has no problem displaying in a text box, but when I try and put it as an item into a list box it turns it into null. Instead of:
"Trait 1 Het.,Trait 3 appears 16 times or 25%."
I get:
" appears 16 times or 25%."
I have tried adding the results to an ArrayList, then populating the listbox after everything is processed, but the result is the same.
Any clues as to why the list box is not accepting the translated AaBBCc information would be greatly appreciated.
strTranslation is never set. Everything is pushed to txtReport.AppendText

Removing entries from a 2-Dimensional Array

I'm doing a university assignment and I'm having some trouble with my Array.
I am building a console based address book application and I need to use a 2D-Array which has the options of adding, removing, or printing the contents of the Array...
I am deleting the current entries by replacing the values with "XXX" -as is required in the brief. I then just hide the "XXX" values when printing to screen.
But once I have 'Deleted' an entry, when it comes to printing the entire Array, anything listed AFTER the entry I tried to delete, does not show up at all, except after adding a new entry.
Could anyone shed some light on why this may be happening?
In your code you're deleting the whole column, once the if statement catch a true condition, e.g. if the address[1, 1] == DeleteName then your array after the for loop will look like:
A00 XXX A02 ...
A10 XXX A13 ...
A20 XXX A23 ...
A30 XXX A33 ...
... ...
So if you just need to delete an entry you will need two loops like this:
for (int i = 0; i < 5; i++)
{
for(int j =0;j < 5; j++)
{
if (Address[i, j] == DeleteName)
{
Address[i, j] = "XXX";
Console.WriteLine("Contact Deleted");
}
}
}
Also, in your case 3 it looks like you're missing the { } after your else statement
else
{
FName = Address[i, 0];
LName = Address[i, 1];
Number = Address[i, 2];
Road = Address[i, 3];
Town = Address[i, 4];
Console.WriteLine(LName +", " + FName + " - "+ Number + ", " + Road+", " + Town);
}

Categories

Resources