Data is automatically deleted after Winform DataGridView adds data

Source: Internet
Author: User

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 ,.

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.