I am trying to check if the column value ["CellNumber"]
is emtpy or not with if condition. Even if the column cell value is not empty, it is prompting with message box instead of exit the foreach loop
and updating the data.
private void btnUpdate_Click(object sender, EventArgs e)
{
da = new SqlDataAdapter("select * from Measurement", con);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
foreach (DataGridViewRow row in dgv.Rows)
{
if (Convert.ToString(row.Cells["CellNumber"].Value) == "")
{
MessageBox.Show("Please enter cellnumber", "Missing Information");
return;
}
}
try
{
da.Update(ds, "Measurement");
}
catch (DBConcurrencyException ex)
{
MessageBox.Show(ex.Message);
}
Please check your number of rows in datagridview by doing
DataGridView. If myDataGridView.Rows.Count == 0
If it is equal to 0 then it is empty
您使用字符串“ CellNumber”,难道不是int
吗?
if (Convert.ToString(row.Cells["CellNumber"].Value) == "")
if (string.IsNullOrEmpty(row.Cells["CellNumber"].Value))
{ Your Code }
Try this way
if (row.Cells["CellNumber"].Value == null || string.IsNullOrEmpty(row.Cells["CellNumber"].Value.ToString()))
{
//rest
}
call dgv.CurrentRow.DataGridView.EndEdit();
to save changes in all cells before validating :)
private void btnUpdate_Click(object sender, EventArgs e)
{
dgv.CurrentRow.DataGridView.EndEdit();
dgv.EndEdit();
....
try to use the .EditedFormattedValue
if (string.IsNullOrWhiteSpace(row.Cells["CellNumber"].EditedFormattedValue.ToString()))
{
MessageBox.Show("Please enter cellnumber", "Missing Information");
return;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.