Print? For (int I = 0; I <iRows-1; I ++)
{
For (int j = 1, k = 0; j <iCols; j ++)
{
If (dgvStudent. Columns [j]. Visible)
{
DimArray [I + 1, k] = dgvStudent. Rows [I]. Cells [j]. Value. ToString ();
K ++;
}
}
}
For (int I = 0; I <iRows-1; I ++)
{
For (int j = 1, k = 0; j <iCols; j ++)
{
If (dgvStudent. Columns [j]. Visible)
{
DimArray [I + 1, k] = dgvStudent. Rows [I]. Cells [j]. Value. ToString ();
K ++;
}
}
} [Csharp] view plaincopyprint? // Public System. Windows. Forms. DataGridView dgvStudent; and dimArray is the cell in Excel, that is
// Among them, public System. Windows. Forms. DataGridView dgvStudent; and dimArray are cells in Excel, that is, [csharp] view plaincopyprint? // Ws. get_Range (ws. Cells [1, 1], ws. Cells [iRows, iCols]). Value2 = dimArray;
// Ws. get_Range (ws. Cells [1, 1], ws. Cells [iRows, iCols]). Value2 = dimArray; [csharp] view plaincopyprint? // Ws is Microsoft. Office. Interop. Excel. Worksheet
// Ws is Microsoft. Office. Interop. Excel. Worksheet
This is the code used to import an Excel file from the Datagridview, but the error "the object reference is not set to the instance of the object" occurs. I thought it was incorrect to add a column manually, so I set the function dgvStudent. columns. add (column name string variable, column header name string variable); changed
[Csharp]
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn ();
Column. HeaderText = sh;
Column. Name = quesId;
Column. ReadOnly = false;
DgvStudent. Columns. Add (column );
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn ();
Column. HeaderText = sh;
Column. Name = quesId;
Column. ReadOnly = false;
DgvStudent. Columns. Add (column); DataGridViewColumn column = new DataGridViewColumn () is not required. The CellType is empty.
However, the error "the object reference is not set to the instance of the object" still exists, so it is not an error to manually add columns. In this case, locate the relationship between the Datagridview cell and Excell,
[Csharp]
For (int I = 0; I <iRows-1; I ++)
{
For (int j = 1, k = 0; j <iCols; j ++)
{
If (dgvStudent. Columns [j]. Visible)
{
If (dgvStudent. Rows [I]. Cells [j]. ValueType = typeof (string ))
{
DimArray [I + 1, k] = dgvStudent. Rows [I]. Cells [j]. Value. ToString ();
K ++;
}
Else // very important, because when the cell of the DataGridView is null
{
If (dgvStudent [j, I]. Value = null)
{
DgvStudent [j, I]. Value = string. Empty;
}
DimArray [I + 1, k] = dgvStudent [j, I]. Value. ToString ();
K ++;
}
}
}
}
For (int I = 0; I <iRows-1; I ++)
{
For (int j = 1, k = 0; j <iCols; j ++)
{
If (dgvStudent. Columns [j]. Visible)
{
If (dgvStudent. Rows [I]. Cells [j]. ValueType = typeof (string ))
{
DimArray [I + 1, k] = dgvStudent. Rows [I]. Cells [j]. Value. ToString ();
K ++;
}
Else // very important, because when the cell of the DataGridView is null
{
If (dgvStudent [j, I]. Value = null)
{
DgvStudent [j, I]. Value = string. Empty;
}
DimArray [I + 1, k] = dgvStudent [j, I]. Value. ToString ();
K ++;
}
}
}
} Now, I know that the cell value of the DataGridView is null and cannot be converted to a string.