When the DataGridView is imported into an Excel file, the following solution is displayed: "the instance of the object has not been referenced ."

Source: Internet
Author: User

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.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.