Convert empty cells in a data table to 0 - c#

I have a data table that has 1 column having few cells having null value. How can I convert them to 0 ?
I bind this data table to a data grid view and cells are empty in case of null in the data table.I need to have 0 displayed in the datagridview.

Edit your cell template and set the NullValue to 0

Honestly, I'd clear this up before binding to the datagrid. I'd execute a for loop and just change all the DBNull values to 0. Its quick, easy, understandable code. This has the benefit of actually changing the data to 0, instead of changing how it is viewed to 0, as others have suggested by making tweaks to the dataGrid. Either strategy has merits, just depends on what you want to do.
Try this:
for (int i = 0; i < dataTable.Rows.Count; i++)
{
DataRow row = dataTable.Rows[i];
if (row["ColumnA"] == DBNull.Value)
{
row["ColumnA"] = 0;
}
}

I set the property of the DataGridViewTextBox column .Seems to be working as expected.
amountColumn.DefaultCellStyle.NullValue = "0";

If you fetch data to display from database then you might bind null to 0 using following syntax:
SELECT ISNULL(ColumnName, 0)
FROM TableName;

this works for all rows and columns with null or empty values
For i As Integer = 0 To output.Tables(0).Rows.Count - 1
Dim row As DataRow = output.Tables(0).Rows(i)
For j As Integer = 0 To output.Tables(0).Columns.Count - 1
If row(j) Is DBNull.Value Then
row(j) = 0
End If
If row(j).ToString.Length = 0 Then
row(j) = 0
End If
Next
Next
C#
for (int i = 0; i <= output.Tables[0].Rows.Count - 1; i++)
{
DataRow row = output.Tables[0].Rows[i];
for (int j = 0; j <= output.Tables[0].Columns.Count - 1; j++)
{
if (object.ReferenceEquals(row[j], DBNull.Value))
{
row[j] = 0;
}
if (row[j].ToString().Length == 0)
{
row[j] = 0;
}
}
}

Try this:
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
dt.Rows[i][j] = "0";
}
}
}

Related

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.

C# - How can I check if it is Column 0 in the datatable?

I have a data table that I am creating from an excel file. This works fine. But, I recently had a spec change and need to substitute a value in column 0 of the table. I am having trouble getting my IF statement to fire. It is probably something simple I am overlooking.
Basically, if it is the first column in the row, I want to put a specific value I pulled elsewhere (I am pulling the color of the cell and putting in the RGB value). If it is any other column in the row, continue to read in the data as usual.
Please see the below code:
DataTable tbl = new DataTable();
foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
{
tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
//var cellColor = ws.Cells[firstRowCell].Style.Fill.BackgroundColor.LookupColor();
}
//var startRow = hasHeader ? 2 : 1;
for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
var currentRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
DataRow row = tbl.Rows.Add();
for (var i = 0; i < tbl.Columns.Count; i++)
{
//This is my problem. I am not seeing what needs to be in here.
if (tbl.Columns.????????)
{
row[i] = colorTable[rowNum];
}
else
{
row[i] = currentRow[rowNum, i + 1].Text;
}
}
}
You're in a for loop that starts at your first column to the last
for (var i = 0; i < tbl.Columns.Count; i++)
So the value you can use to know at what column you're at would be the i, you could have something like this
for (var i = 0; i < tbl.Columns.Count; i++)
{
//If you're at the first column
if (i == 0)
{
row[i] = colorTable[rowNum];
}
else
{
row[i] = currentRow[rowNum, i + 1].Text;
}
}

DataGridview Cells are not filled

for (int i = 0; i <= x; i++)
{
for (int k = 0; k <= TimeSub; k++)
{
dataGridView1.Rows.Add(FromDate); // hour
dataGridView1.Rows[k].Cells[1].Value = FromTime;
FromTime = FromTime + 1;
}
FromDate=FromDate.AddDays(1);
}
While I am executing this statement all the rows are filled correctly but the cells are not. Kindly help on this.
You are always looping from the first row in the second loop.
Rows.Add retruns the new row index.
Try this instead:
var rowIndex = dataGridView1.Rows.Add(FromDate); // hour
dataGridView1.Rows[rowIndex].Cells[1].Value = FromTime;

c# winforms DataGridViewRow.Index and Count

What is wrong with this code
for (int i = 1; i < dgv.Rows.Count;)
{
MessageBox.Show(dgv.Rows[i].Index.ToString());
}
I'm getting endless MsgBoxes displaying allways and only value "1"
dgv has six rows.
you forget to place i++
and also i = 0; if i = 1 you will be miss first row of gridview
for (int i = 0; i < dgv.Rows.Count;i++)
{
MessageBox.Show(dgv.Rows[i].Index.ToString());
}

Datatable and Datagridview

I have datatable table like
id name address phoneno
1 abc kfjskl 798798
2 bcd kjdsfl 808909
3 899009
fjsh kjllkjl
5 jfkd
And I am displaying this value in the datagridview by code
dataGridView1.ColumnCount = Table.Columns.Count;
dataGridView1.RowCount = Table.Rows.Count;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
dataGridView1[j, i].Value = Table.Rows[i][j].ToString();
}
}
Now I don't want to display row that has some missing value like If I will do that then the datagridview will look like
1 abc kfjskl 798798
2 bcd kjdsfl 808909
How can I do that ?
This will do what you need (I think):
dataGridView1.ColumnCount = Table.Columns.Count;
for (int i = 0; i < Table.Rows.Count; i++)
{
bool valueMissing = false;
DataGridViewRow row = new DataGridViewRow();
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
if (Table.Rows[i][j].ToString() == "")
{
valueMissing = true;
break;
}
else
{
row.Cells[j].Value = Table.Rows[i][j];
}
}
if (!valueMissing)
{
dataGridView1.Rows.Add(row);
}
}
This may need to be modified to check for null values in Table.Rows[i][i] (in addition to just checking for empty string values).
I think your best choice is to use a DataView between the DataGridView and the DataTable.
The DataView allows you to filter values using and expression without affecting to the original DataTable. To filter all empty columns you can create a view with this expression:
DataView view = new DataView(table);
view.RowFilter = "ISNULL(id, '') <> '' AND ISNULL(name, '') <> '' AND ISNULL(address, '') <> '' AND ISNULL(phoneno, '') <> ''";
The ISNULL function replaces a column value whit the specified string, if the column value is null. So the filter is replacing every NULL value with empty strings, so excluding NULL or Empty columns.
Then you can use this view to assing values to the grid, instead of using the datatable:
dataGridView1.ColumnCount = view.Columns.Count;
dataGridView1.RowCount = view.Rows.Count;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
dataGridView1[j, i].Value = view.Rows[i][j].ToString();
}
}
Also instead of using loops to provide values to the grid, you can simply use databinding. It's much simpler:
dataGridView1.DataSource = view;
Hope it helps!
I'm not sure if I understand, but you could possibly use DataTable.Select to filter out any rows with missing values before you insert them in the datagrid.
Or otherwise, instead of using a for loop for the rows, just use a foreach loop on the rows in the table and add the rows one by one with dataGridView.Rows.Add(newRow).

Categories

Resources