How to delete datarow cells from certain index? - c#

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;
}

Related

C# NPOI read EXCEL cells from top to bottom

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());
}
}
}
}

check if there are duplicate values in 2 datagridview

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:

Getting datagridview cell index in C#

How to get Cell index after comparing values, for example i have this
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[value].Value.ToString() == radTextBox1.Text)
{
//If the value matches how to get the row index of that
}
}
This might do the job:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == radTextBox1.Text)
{
dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
}
}
If you are wanting to find the value in any cell then you need to use a nested loop.
dataGridView1.ClearSelection();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int c = 0; c < dataGridView1.Columns.Count; c++)
{
if (dataGridView1.Rows[i].Cells[c].Value.ToString() == radtextBox1.Text)
{
dataGridView1.Rows[i].Selected = true;
}
}
}
This will move through each row while checking all the columns for a value and will hilight any row where a cell has that value. Note that this code is case sensitive so "City" <> "city" but that is easily fixed using .ToUpper or .ToLower as needed.
One other thing to add, this code also is based off a DGV that has AllowUserToAddRows set to false. If you need the edit row, you either need to -1 from the count in the rows loops or check to ensure that the current row is false for .IsNewRow.
You found the Row you're looking for.
It's i variable in your code.
var requiredRowIndex = -1;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[value].Value.ToString() == radTextBox1.Text)
{
requiredRowIndex = i;
break;
}
}
if (requiredRowIndex != -1)
{
// It was found.
}
else
{
// It was not found.
}
You dont show us what is the value? It's actualy index of Cell you're looking for.

Trying to get column counts having data using openxm(C#)

I am trying to count header columns only having data in excel sheet using openxml(c#).
For example if the data is present in A1,D1,F1, then the count should be 3 and not 6 (i.e A to F).
Please suggest how it can be done.
To know if a cell contains a value check if CellValue is not null. The following should work for you:
Row row1 = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>().FirstOrDefault();
int counter = 0;
foreach (Cell cell in row1.Elements<Cell>())
{
if (cell.CellValue != null)
{
Console.WriteLine(cell.CellReference);
counter++;
}
}
I did something like this and it working fine for me.
foreach (Cell cell in row.Descendants<Cell>())
{
if (cell.CellValue != null)
{
var NumberOfCol = datatable.Columns.Count;
DataRow tempRow = datatable.NewRow();
for (int k = 0; k < NumberOfCol; k++)
{
if (tempRow[k] != null)
{
tempRow[k] = row.Count() > k ? GetValue(spreadsheet, row.Descendants<Cell>().ElementAt(k)) : "";
}
else
{
tempRow[k] = "";
}
}
}
}

Reading data from DataGridView in C#

How can I read data from DataGridView in C#? I want to read the data appear in Table. How do I navigate through lines?
something like
for (int rows = 0; rows < dataGrid.Rows.Count; rows++)
{
for (int col= 0; col < dataGrid.Rows[rows].Cells.Count; col++)
{
string value = dataGrid.Rows[rows].Cells[col].Value.ToString();
}
}
example without using index
foreach (DataGridViewRow row in dataGrid.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
string value = cell.Value.ToString();
}
}
If you wish, you can also use the column names instead of column numbers.
For example, if you want to read data from DataGridView on the 4. row and the "Name" column.
It provides me a better understanding for which variable I am dealing with.
dataGridView.Rows[4].Cells["Name"].Value.ToString();
Hope it helps.
string[,] myGridData = new string[dataGridView1.Rows.Count,3];
int i = 0;
foreach(DataRow row in dataGridView1.Rows)
{
myGridData[i][0] = row.Cells[0].Value.ToString();
myGridData[i][1] = row.Cells[1].Value.ToString();
myGridData[i][2] = row.Cells[2].Value.ToString();
i++;
}
Hope this helps....
Code Example : Reading data from DataGridView and storing it in an array
int[,] n = new int[3, 19];
for (int i = 0; i < (StartDataView.Rows.Count - 1); i++)
{
for (int j = 0; j < StartDataView.Columns.Count; j++)
{
if(this.StartDataView.Rows[i].Cells[j].Value.ToString() != string.Empty)
{
try
{
n[i, j] = int.Parse(this.StartDataView.Rows[i].Cells[j].Value.ToString());
}
catch (Exception Ee)
{ //get exception of "null"
MessageBox.Show(Ee.ToString());
}
}
}
}
private void HighLightGridRows()
{
Debugger.Launch();
for (int i = 0; i < dtgvAppSettings.Rows.Count; i++)
{
String key = dtgvAppSettings.Rows[i].Cells["Key"].Value.ToString();
if (key.ToLower().Contains("applicationpath") == true)
{
dtgvAppSettings.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
}
}
}

Categories

Resources