datagridview直接编辑验证时候取值使用EditedFormattedValue取值,如果使用value取值的话,则对其修改的时候value保持修改以前的数据信息,从而导致一些判断之类的东西出现不正确!
/// <summary>
/// 行数显错误时候验证
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">e</param>
private void DgvGoodsInfo_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
if (DgvGoodsInfo.Rows[e.RowIndex].IsNewRow == true)
{
return;
}
else if (this.CellValid(DgvGoodsInfo, e.RowIndex) == true)
{
e.Cancel = false;
return;
}
else
{
e.Cancel = true;
return;
}
}
private bool CellValid(DataGridView dgv, int RowIndex)
{
//// 名称
if (dgv.Rows[RowIndex].Cells[this.ColumGoodsName.Name].ReadOnly)
{
return false;
}
if (dgv.Rows[RowIndex].IsNewRow == true)
{
return true;
}
//// TODO:编辑期间取值使用EditedFormattedValue进行判断
if (dgv[this.ColumGoodsName.Name, RowIndex].EditedFormattedValue == null || string.IsNullOrEmpty(dgv[this.ColumGoodsName.Name, RowIndex].EditedFormattedValue.ToString().Trim()))
{
WDMessageBox.ShowTips("清单货品名称不能为空");
dgv[this.ColumGoodsName.Name, RowIndex].ErrorText = "清单货品名称不能为空";
return false;
}
else
{
if (dgv[this.ColumGoodsName.Name, RowIndex].EditedFormattedValue.ToString().Trim().GetLength() > 50)
{
WDMessageBox.ShowTips("清单货品名称输入不能大于50个字符,一个汉字代表两个字符");
dgv[this.ColumGoodsName.Name, RowIndex].ErrorText = "清单货品名称输入不能大于50个字符,一个汉字代表两个字符";
return false;
}
if (this.bindlist != null && this.bindlist.Count > 0)
{
try
{
List<ExpOrderDetailModel> model = this.bindlist.Where(editmodel => editmodel.GoodsName.Equals(dgv[this.ColumGoodsName.Name, RowIndex].EditedFormattedValue.ToString().Trim(), StringComparison.CurrentCultureIgnoreCase)).ToList();
if (model.Count > 1)
{
WDMessageBox.ShowTips("清单货品名称必须唯一,如需添加,请修改货品件数");
dgv[this.ColumGoodsName.Name, RowIndex].ErrorText = "清单货品名称必须唯一,如需添加,请修改货品件数";
return false;
}
}
catch (Exception ex)
{
LoggingService.Error(ex.ToString());
}
}
}
}
/// <summary>
/// 单击单元格时触发-----删除操作
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">e</param>
private void DgvGoodsInfo_CellClick(object sender, DataGridViewCellEventArgs e)
{
var dgv = sender as DataGridView;
var columnName = dgv.Columns[e.ColumnIndex].Name;
if (columnName == this.DeleteColumn.Name)
{
// dgv.RowValidating-=new DataGridViewCellCancelEventHandler(DgvGoodsInfo_RowValidating);
if (this.DgvGoodsInfo.CurrentRow.IsNewRow == false)
{
this.DgvGoodsInfo.EndEdit();
//// 新增情况下删除订单清单
if (string.IsNullOrEmpty(this.OrderID))
{
this.DgvGoodsInfo.Rows.RemoveAt(e.RowIndex);
}
//// 修改情况下删除订单清单
else
{
//this.DgvGoodsInfo.EndEdit();
if (dgv.Rows[e.RowIndex].Cells[this.MID.Name].Value == null)
{
this.DgvGoodsInfo.Rows.RemoveAt(e.RowIndex);
this.DgvGoodsInfo.DataSource = this.bindlist;
this.DgvGoodsInfo.EndEdit();
}
else
{
string strmid = dgv.Rows[e.RowIndex].Cells[this.MID.Name].Value.ToString();
//// 检测清单是否已经被提货
this.CheckGoodsInfoDel(strmid);
}
}
}
}
}
注意还需要在保存按钮的时候继续进行验证