As i see, you are trying to export data from DataGridView to tab-separated file.
Some tips:
1) Define variable, type of:
DataGridViewCell[
^], to use it inside second loop,
2) Value returned by
DataGridViewCell.Value[
^] is the same type, as its source. So, if
DGVCell.Value
is equal to
DBNull.Value
, then it can't be
null[
^].
Quote:
In database applications, a null object is a valid value for a field. This class differentiates between a null value (a null object) and an uninitialized value (the DBNull.Value instance). For example, a table can have records with uninitialized fields. By default, these uninitialized fields have the DBNull value.
More about:
null (C# reference)[
^],
Nullable Types (C#)[
^] and
DBNull Class[
^].
Try to replace:
(dataGridView1.Rows[i].Cells[j].Value != null)
with:
DataGridViewCell cell = this.dataGridView1.Rows[i].Cells[j];
if (cell.Value.ToString != DbNull.Value.ToString)
wr.Write(cell.Value.ToString + "\t");
else
wr.Write("\t");
Have a look at below examples:
http://dotnetask.com/Resource.aspx?Resourceid=644[
^]
http://csharp.net-informations.com/excel/csharp-excel-datagridview.htm[
^]
http://csharp.net-informations.com/datagridview/csharp-datagridview-export-excel.htm[
^]
More you'll find at CP ;) Use
[Search]
textbox in the right-top corner of this site.