I have sequence for reading EXCEL sheet from left to right
for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
if (row == null) continue;
if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
for (int j = row.FirstCellNum; j < cellCount; j++)
{
if (row.GetCell(j) != null)
{
if (!string.IsNullOrEmpty(row.GetCell(j).ToString()) &&
!string.IsNullOrWhiteSpace(row.GetCell(j).ToString()))
{
rowList.Add(row.GetCell(j).ToString());
}
}
}
}
And I am getting all the text from 1st sheet, but I need to read each column from top to bottom.
Here's what I tried but it's ending up getting 1 word loop from random cell...
for (int i = headerRow.FirstCellNum; i < headerRow.LastCellNum; i++)
{
IRow row = sheet.GetRow(i);
if (row == null) continue;
if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
for (int j = sheet.FirstRowNum; j <= sheet.LastRowNum; j++)
{
if (row.GetCell(i) != null)
{
if (!string.IsNullOrEmpty(row.GetCell(i).ToString()) &&
!string.IsNullOrWhiteSpace(row.GetCell(i).ToString()))
{
rowList.Add(row.GetCell(i).ToString());
}
}
}
}
When you read each column from top to bottom you call row.GetCell(i) in the second for loop instead of row.GetCell(j).
for (int i = headerRow.FirstCellNum; i < headerRow.LastCellNum; i++)
{
IRow row = sheet.GetRow(i);
if (row == null) continue;
if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
for (int j = sheet.FirstRowNum; j <= sheet.LastRowNum; j++)
{
if (row.GetCell(j) != null)
{
if (!string.IsNullOrEmpty(row.GetCell(j).ToString()) &&
!string.IsNullOrWhiteSpace(row.GetCell(j).ToString()))
{
rowList.Add(row.GetCell(j).ToString());
}
}
}
}
Related
working on a personal project to self develop with c#
i'm currently working on a console app using c# for space invaders - i made the decision to use a 2d array in order to store my game.
my current class for invaders is below, i can "summon" the invaders, move them from left to right, then drop a row and move right to left but they don't iterate back through in the other direction.
public void SummonMovingInvaders(int row, int col, Map[,] spaceInvaders)
{
var checkGameOver = new ConsoleInterface();
while (checkGameOver.GameOverCheck(spaceInvaders) == false)
{
row = 1;
while (spaceInvaders[row, col].Filled == false)
{
//Set up original invaders
for (int i = 1; i < 3; i++)
{
spaceInvaders[1, i].Type = CellType.Invader;
}
for (row = 1; row < 10; row++)
{
for (col = 0; col < 4; col++)
{
for (int j = 3; j > 0; j--)
{
if (j == 0)
{
spaceInvaders[row, col + j].Type = CellType.Empty;
}
else
{
spaceInvaders[row, col + j].Type = spaceInvaders[row, col + j - 1].Type;
}
}
Thread.Sleep(500);
}
//Clear out the row
for (int i = 1; i < spaceInvaders.GetLength(1); i++)
{
spaceInvaders[row, i].Type = CellType.Empty;
}
row++;
for (col = 6; col >= 0; col--)
{
for (int j = 0; j < 2; j++)
{
if (col > 0)
spaceInvaders[row, col + j - 1].Type = CellType.Invader;
else
spaceInvaders[row, col + j].Type = CellType.Invader;
}
Thread.Sleep(500);
for (int j = 1; j <= 2; j++)
{
if (col > 1)
{
spaceInvaders[row, col].Type = CellType.Empty;
spaceInvaders[row, col - 2].Type = spaceInvaders[row, col - 1].Type;
}
else
{
break;
}
}
}
col = 0;
}
}
}
}
I'm trying to compare the rows and columns with 2 datagridview. The first column s1 in (DGV1) has found the duplicate values in s1 (DGV2). The second column s2 in (DGV1) is mismatched with the second column s2 in (DGV2). What's wrong with the code?
for (int i = 0; i < dataGridView1.RowCount ; i++)
{
for (int j = 0; j < dataGridView2.RowCount; j++)
{
if ( dataGridView1.Rows[i].Cells[0].Value.ToString() ==
dataGridView2.Rows[j].Cells[0].Value.ToString())
{
dataGridView1.Rows[i].Cells[0].Style.BackColor =
Color.Yellow;
dataGridView2.Rows[j].Cells[0].Style.BackColor =
Color.YellowGreen;
}
}
}
enter image description here
try this
for (int i = 0; i < dataGridView1.RowCount ; i++)
{
for (int j = 0; j < dataGridView2.RowCount; j++)
{
if ( (dataGridView1.Rows[i].Cells[0].Value.ToString() ==
dataGridView2.Rows[j].Cells[0].Value.ToString()) &&
(dataGridView1.Rows[i].Cells[1].Value.ToString() ==
dataGridView2.Rows[j].Cells[1].Value.ToString()) )
{
dataGridView1.Rows[i].Cells[0].Style.BackColor =
Color.Yellow;
dataGridView1.Rows[i].Cells[1].Style.BackColor =
Color.Yellow;
dataGridView2.Rows[j].Cells[0].Style.BackColor =
Color.YellowGreen;
dataGridView2.Rows[j].Cells[1].Style.BackColor =
Color.YellowGreen;
}
}
}
foreach (DataGridViewRow row1 in table1.Rows) //LOOP ROWS TABLE 1
{
foreach (DataGridViewCell cell1 in row1.Cells) //LOOP COLUMNS TABLE 1
{
foreach (DataGridViewRow row2 in table2.Rows) //LOOP ROWS TABLE 2
{
foreach (DataGridViewCell cell2 in row2.Cells) //LOOP COLUMNS TABLE 2
{
if (cell1.Value != null && cell2.Value != null&& cell2.Value.ToString() == cell1.Value.ToString())
{
cell1.Style.BackColor = Color.Yellow;
cell2.Style.BackColor = Color.YellowGreen;
}
}
}
}
}
Hey Marcel16, this should solve your problem:
Datagridview calculate Amount each cell Differently based on data from database if addition then addition should be done if subtraction then subtraction to be done.
i have created this method but it affects all cell with answer. only the above cell should be calculated.
public void datagridviewone()
{
int addvalue = 0, subractvalue = 0;
ds = cs.Select("select ChargesProperty from FrieghtGridview");
list.Clear();
addition.Clear();
subtraction.Clear();
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
if(ds.Tables[0].Rows[j][0].ToString() == "Answer")
{
list.Add(j);
}
if(ds.Tables[0].Rows[j][0].ToString() == "Plus")
{
addition.Add(j);
}
if(ds.Tables[0].Rows[j][0].ToString() == "Minus")
{
subtraction.Add(j);
}
}
if (list.Count > 0)
{
for(int j = 0; j < list.Count; j++)
{
if(addition.Count > 0)
{
for(int adval = 0; adval <= addition.Count-1; adval++)
{
if
(dataGridView1.Rows[addition[adval]].Cells[3].Value.ToString()
!= "")
{
addvalue +=
Convert.ToInt32(dataGridView1.Rows[addition[adval]].Cells[3].Value);
}
}
}
if (subtraction.Count > 0)
{
for (int adval = 0; adval <= subtraction.Count - 1;
adval++)
{
if
(dataGridView1.Rows[subtraction[adval]].Cells[3].Value.ToString() != "")
{
subractvalue +=
Convert.ToInt32(dataGridView1.Rows[subtraction[adval]].Cells[3].Value);
}
}
}
var finalans = addvalue - subractvalue;
dataGridView1.Rows[list[0]].Cells[3].Value = finalans;
addvalue = 0;
subractvalue = 0;
finalans = 0;
}
}
}
I need to empty datarow from certain cell index, where previous condition has been met.
if (tabControl1.SelectedIndex == 1)
{
for (int i = 0; i < dtSecondTab.Rows.Count; i++)
{
if (dtSecondTab.Rows[i]["Month"].ToString() != "" && dtSecondTab.Rows[i]["Month"].ToString() != Convert.ToInt32(cbMonth.Text).ToString())
{
//here I need to clear datarow from cell index 6 to the end of the row
}
}
}
for(int cellIndex = 6; cellIndex < dtSecondTab.Columns.Count; cellIndex++)
{
dtSecondTab.Rows[i][cellIndex] = DBNull.Value;
}
I have this repeating code , and I'm unsure of how I can make it in only 1 method.
public int isWonVertical()
{
for (int i = 0; i < columns; i++)
{
resetCounter();
for (int j = 0; j < rows; j++)
{
if (raster[j, i] == 1) counterPlayer1++;
else counterPlayer1 = 0;
if (raster[j, i] == 2) counterPlayer2++;
else counterPlayer2 = 0;
if (counterPlayer1 == tokenStreak) return 1;
if (counterPlayer2 == tokenStreak) return 2;
}
}
return 0;
}//isWonVertical
public int isWonHorizontal()
{
for (int i = 0; i < rows; i++)
{
resetCounter();
for (int j = 0; j < columns; j++)
{
if (raster[i, j] == 1) counterPlayer1++;
else counterPlayer1 = 0;
if (raster[i, j] == 2) counterPlayer2++;
else counterPlayer2 = 0;
if (counterPlayer1 == tokenStreak) return 1;
if (counterPlayer2 == tokenStreak) return 2;
}
}
return 0;
}//isWonHorizontal
The returns and resetCounter() I can all put in 1 method. But how do I make sure the for loops are different for vertical/horizontal. I assume it's with giving parameters with, and then checking wether I gave 'vertical' or 'horizontal' as a paramter. But i'm unsure how to make this actually work.
Thank you.
public int isWon(DirectionEnum enum)
{
int counter1 = enum == DirectionEnum.IsVertical ? columns : rows;
int counter2 = enum == DirectionEnum.IsHorizontal ? columns: rows;
for (int i = 0; i < counter1 ; i++)
{
resetCounter();
for (int j = 0; j < counter2; j++)
{
if (raster[i, j] == 1) counterPlayer1++;
else counterPlayer1 = 0;
if (raster[i, j] == 2) counterPlayer2++;
else counterPlayer2 = 0;
if (counterPlayer1 == tokenStreak) return 1;
if (counterPlayer2 == tokenStreak) return 2;
}
}
return 0;
}
How about his, two parameters, one for the inner array, one for the outer. Then your client (calling code) needs to decide what to use as inner or outer, either rows or columns
public int isWon(outerArray, innerArray)
{
for (int i = 0; i < outerArray; i++)
{
resetCounter();
for (int j = 0; j < innerArray; j++)
{
if (raster[i, j] == 1) counterPlayer1++;
else counterPlayer1 = 0;
if (raster[i, j] == 2) counterPlayer2++;
else counterPlayer2 = 0;
if (counterPlayer1 == tokenStreak) return 1;
if (counterPlayer2 == tokenStreak) return 2;
}
}
return 0;
}