Recently, when data is added to the DataGridView, the data is automatically deleted when the row focus is lost.
DataGridView data comes from DataTable
The problem persists.
/// <Summary>
/// Add a new row to the DataGridView
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void dgv_NewRowNeeded (object sender, DataGridViewRowEventArgs e)
{
If (dgvContact. Focused)
{
E. Row. Cells ["ContactSex"]. Value = "male ";
E. Row. Cells ["ContactDuties"]. Value = CommonHelp. GetTypeName (1 );
E. Row. Cells ["ContactName"]. Value = "enter a name ";
// E. Row. Cells ["Contact_CustomerId"]. Value = GetCustomerId;
}
Else if (dgvWeb. Focused)
{
E. Row. Cells ["WebServiceType"]. Value = CommonHelp. GetTypeName (4 );
E. Row. Cells ["WebService"]. Value = CommonHelp. GetTypeName (2 );
E. Row. Cells ["WebStatus"]. Value = CommonHelp. GetTypeName (5 );
E. Row. cells ["webprice"]. value = 0;
E. Row. cells ["costprice"]. value = 0;
E. Row. cells ["webstartdate"]. value = datetime. Now. Date. tostring ("yyyy-mm-dd ");
E. Row. cells ["webenddate"]. value = datetime. Now. addyears (1). tostring ("yyyy-mm-dd ");
E. Row. cells ["webaddress"]. value = "Enter the address ";
// E. Row. cells ["web_customerid"]. value = getcustomerid;
}
}
After a morning's troubleshooting, I finally found the cause, where the above Code commented
When a datagridview is added, the datagridview row is verified based on the bound data type. Because the contact_customerid and web_customerid columns are not set during addition, their default values are null, the bound data type is int, so the verification fails.
However, the data will still be added to the bound able, but it will not be displayed in the datagridview ,.