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] = "";
}
}
}
}
Related
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;
}
That piece of code check only first row in datagrid. I need to find all 0 rows in datagrid. I have many models and this need to be called on cellclick to grid than show to another. any help ?
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in dgvNalog.SelectedCells)
{
for (int i = 0; i < dgvNalog.RowCount; i++)
{
rowIsEmpty = true;
if (Convert.ToDouble(row.Cells["Kolicina"].Value) == 0)
{
rowIsEmpty = false;
dgvNalog.Rows[i].Cells["Kolicina"].Style.BackColor = Color.Red;
break;
}
}
}
}
You need to iterate each row in dgvNalog.Rows collection and check for the zero value like this:
for (int i = 0; i < dgvNalog.RowCount; i++)
{
var row = dgvNalog.Rows[i];
rowIsEmpty = true;
if (Convert.ToDouble(row.Cells["Kolicina"].Value) == 0)
{
rowIsEmpty = false;
dgvNalog.Rows[i].Cells["Kolicina"].Style.BackColor = Color.Red;
// break; -> comment this to allow other rows to be processed
}
}
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;
}
}
}
im trying to loop through a dataset and where ever there is a 0 i want to change that to a null value or blank so far i have this
foreach (DataRow drRow in ds.Tables[0].Rows)
{
//loop through cells in that row
{
//if the value is 0 change to null or blank
}
}
can any one help me out of this?
Sure
foreach (DataRow drRow in ds.Tables[0].Rows)
{
for(int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
int rowValue;
if (int.TryParse(drRow[i].ToString(), out rowValue))
{
if (rowValue == 0)
{
drRow[i] = null;
}
}
}
}
I really believed that nobody still works with DataSets :(
Something like this might help:
foreach (DataRow rows in table.Rows)
{
object value = null;
var cells = rows.ItemArray;
for (int i = 0; i < cells.Length; i++)
{
value = cells[i];
if (value != null && value.GetType() == typeof(int))
{
if ((int)value == 0)
{
cells[i] = null;
}
}
}
rows.ItemArray = cells;
}
You could do
DataTable dt = new DataTable();
.....
.....
DataRowCollection drColl = dt.Rows;
for(int j=0; j<drColl.Count;j++)
{
DataRow drRow = drColl[j];
object[] itemArr = null;
itemArr = drRow.ItemArray;
//loop through cells in that row
for(int i=0; i<itemArr.Length; i++)
{
//if the value is 0 change to null or blank
if (itemArr[i].ToString() == "0")
drRow[i] = "";
}
}
.NET 2.0
Trying to find if I am on the first row so I can do a comparison
foreach(DataRow row in tbl.Rows) {
if (row<something> == "first row") { continue; }
foreach(DataColumn col in tbl.Columns) {
if (something == "first column) { continue; }
....
But it is escaping me.
Quick 'n' dirty says you can throw an int counter in there.
int rowCounter=0;
foreach(DataRow row in tbl.Rows)
{
rowCounter++;
if (rowCounter==1) { continue; }
...
//do the same for a columnCounter to get your first-column first-row
}
You can use Linq to do that:
foreach(DataRow row in tbl.Rows.Cast<DataRow>().Skip(1)) {
foreach(DataColumn col in tbl.Columns.Cast<DataColumn>().Skip(1)) {
or you can compare current Row/Column with first row or column
foreach(DataRow row in tbl.Rows) {
if (tbl.Rows[0] == "first row") { continue; }
foreach(DataColumn col in tbl.Columns) {
if (tbl.Columns[0] == "first column) { continue; }
or you can use indexer to access it (this way is faster way to access rows)
for(int rowIndex = 0; rowIndex < tbl.Rows.Count; rowIndex++)
{
if(rowIndex == 0) return;
var row = tbl.Rows[rowIndex];
}
Wont a better way be using
bool isFirstReached = false;
foreach (DataRow datarow in datatable.Rows)
{
foreach (DataColumn datacolumn in datarow.Table.Columns)
{
if (!isFirstReached)
{
isFirstReached = true;
continue;
}
}
}
unless you prefer for rather than foreach
for (int i = 0; i < datatable.Rows.Count; i++)
{
for (int j = 0; j < datatable.Columns.Count; j++)
{
if (j == 0 && i == 0)
{
continue;
}
}
}
Could you use a counter like so?
var tbl = new DataTable();
int row = -1;
int column = -1;
foreach (DataRow row in tbl.Rows)
{
row++;
if (row == 0)
{
continue;
}
foreach (DataColumn col in tbl.Columns)
{
column++;
if (column == 0)
{
continue;
}
}
}
Actually the easiest and safest way I found that someone posted on here, but removed it was:
foreach (DataRow row in tbl.Rows) {
if (tbl.Rows.IndexOf(row) < 1)
continue;
foreach (DataColumn col in tbl.Columns) {
if (tbl.Columns.IndexOf(col) < 1)
continue;