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] = "";
}
}
Related
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] = "";
}
}
}
}
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 two datatables, each with 1 column:
dtTempCM
dtOldTempCM
In both tables, the only column name is Column1
I also have 2 datagridviews in the form that are each bound to a datatable:
dgvCurrentCM.DataSource = dtTempCM;
dgvOldCM.DataSource = dtOldTempCM;
How can I compare each row, and highlight it in one (or both) of the datagridviews if they don't match? So far, I have this:
foreach (DataRow row1 in dtTempCM.Rows)
{
foreach (DataRow row2 in dtOldTempCM.Rows)
{
var array1 = row1.ItemArray;
var array2 = row2.ItemArray;
if (array1.SequenceEqual(array2))
{
//change row/cell color in dgvCurrentCM to red
//change row/cell color in dgvOldCM to red
}
}
}
Any ideas? Thank you!
EDIT: I tried this too, but it changes the color of every cell since it compares every row in dgvOldCM to a single row in dgvCurrentCM:
foreach (DataGridViewRow row1 in dgvCurrentCM.Rows)
{
foreach (DataGridViewRow row2 in dgvOldCM.Rows)
{
if (row1.Cells[0].Value != row2.Cells[0].Value)
{
row1.DefaultCellStyle.ForeColor = Color.Red;
row2.DefaultCellStyle.ForeColor = Color.Red;
}
}
}
I would iterate on one of the gridview, comparing to the other like this:
for (int i = 0; i < dgvCurrentCM.RowCount; i++)
{
if (dgvCurrentCM.Rows[i].Cells[0].Value != null)
{
if ((int)dgvCurrentCM.Rows[i].Cells[0].Value != (int)dgvOldCM.Rows[i].Cells[0].Value)
{
dgvCurrentCM.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
dgvOldCM.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
}
}
}
You can generate a Intersection of two Datatables, and then mark the rows:
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt1.Columns.Add("Col");
dt2.Columns.Add("Col");
for (int i = 0; i < 10; i++)
{
DataRow dr = dt1.NewRow();
dr["Col"] = i.ToString();
dt1.Rows.Add(dr);
}
for (int i = 5; i < 15; i++)
{
DataRow dr = dt2.NewRow();
dr["Col"] = i.ToString();
dt2.Rows.Add(dr);
}
var result = dt1.AsEnumerable().Intersect(dt2.AsEnumerable(), DataRowComparer.Default);
dataGridView1.DataSource = dt1;
dataGridView2.DataSource = dt2;
for (int i = 0; i < dataGridView1.RowCount -1; i++)
{
DataRow currRow = ((DataRowView)dataGridView1.Rows[i].DataBoundItem).Row;
if (result.Contains(currRow))
dataGridView1.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
}
if (array1.SequenceEqual(array2))
{
}
else
{
//Do your action here!
}
I apologize for this newbie question, but I'm looking for a simple solution.
I want to write a function that will return a datatable.
Like this:
public static DataTable DataTableCommaReplce(DataTable dt){..}
The function will check each data in DataTable.
If data contained one or more commas, the function will make that data in double quote.
For Example:
you,me⇒"you,me"
What's the best way to write this function?
Can any body help me?
I had solved with this code, but I want more simple solution.
If possible, I want no looping.
public static DataTable DataTableCommaReplce(DataTable dt)
{
int col = dt.Columns.Count;
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < col; i++)
{
if (dr[i].ToString().IndexOf(",") > 0)
{
dr[i] = "\"" + dr[i].ToString() + "\"";
}
}
}
return dt;
}
This should work:
public static DataTable DataTableCommaReplce(DataTable dt) {
foreach (DataRow row in dt.Rows) {
foreach (DataColumn col in dt.Columns) {
string s = row[col] as string;
if (s != null) {
if (s.Contains(',')) {
row[col] = string.Format("\"{0}\"", s);
}
}
}
}
return dt;
}
Try the Following Code part. Hope it will help.
DataTable Tb = new DataTable();
for (int i = 0; i < Tb.Columns.Count; i++)
{
for (int j = 0; j < Tb.Rows.Count; j++)
{
if (Tb.Rows[j][i] != DBNull.Value)
{
if (Tb.Rows[j][i].ToString().IndexOf(',') != -1)
{
Tb.Rows[j][i] = "\"" + Tb.Rows[j][i].ToString() + "\"";
}
}
}
}
.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;