There is a DataTable and there is a loop

 foreach (DataRow row in table.Rows) { int a = 0; int b = 0; int.TryParse(row["a"].ToString(), out a); int.TryParse(row["b"].ToString(),out b); row["a"] = sign + signfk; } 

row["a"] type int

Accordingly, it turns out that the column is of type int and integer values ​​are stored in it.

Is it possible to make it so that when row["a"] = 0 , it was possible to convert the cell to string and set the value for row["a"] = "н/a" .

I understand that it is possible to work with lists and it will be easier. But since there are many actions with one table, there is no point in changing already.

The report is output from the source table in Excel.

  • one
    How not to try, if an int column is written to it, the string will fail. - MaximK
  • maybe somehow you can remove the cell and replace the string box - Mikhail
  • @ Michael you can do so - komra23
  • Write then NULL there something - Senior Pomidor
  • @May_be Like in my example, replace the cell with a string. I still do not understand DataTable with cells - Michael

1 answer 1

In DataTable you need to store the value that should be: 0.
And the string "n / a" must be shown when displaying data to the user.

I do not know how the data is presented to you to the user. Suppose this is done using a DataGridView . Then you can assign the desired format row for a specific column.

 DataTable table = new DataTable(); table.Columns.Add("a", typeof(int)); table.Columns.Add("b", typeof(int)); table.Rows.Add(1, 11); table.Rows.Add(2, 22); table.Rows.Add(0, 33); var dataGridView = new DataGridView { Parent = this, Dock = DockStyle.Top }; dataGridView.DataSource = table; dataGridView.Columns[0].DefaultCellStyle.Format = "0;-0;н/a"; 

The data is entered into the DataTable and bound to the grid. The last line of code for the column with index 0 is set to the format "0;-0;н/a" - positive and negative values ​​are displayed as they are, and instead of 0 - н/а .

  • Yes this is a good answer for datagridview. My conclusion goes immediately to exel. - Michael
  • @Michail - I suppose you can specify formatting in Excel in the same way. - Alexander Petrov