我正在寻找一种方法,使细胞成为新行的必需细胞。 该行由7个单元组成。我遇到的问题是,如果用户输入新行, 他可以跳过必须包含所需数字的非常重要的第一个单元格。我要那个 如果用户输入新行,他必须首先输入该行中的所需单元格,然后才能向第二个单元格添加更多信息,依此类推。我已经尝试了行验证并检查了DBNull等等,但没有任何效果。最好的解决方案是,如果输入新行,则第一个单元格跳转到编辑模式。如果输入数字,则可以编辑以下单元格,否则不能编辑。如果用户取消添加,则不会添加当前行。
任何建议和信息的Thx!
答案 0 :(得分:1)
我自己找到了一个简单的解决方案。如果第一个单元格已在新行上填充,请检查CellBeginEdit - 否则无法编辑其他单元格,因此不会添加新行。 愿其他人也需要它。非常感谢你的帮助!
private void dgvUsers_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (e.ColumnIndex != 0)
{
if ((e.RowIndex == dgvUsers.NewRowIndex) && (dgvUsers[0, e.RowIndex].Value == null))
{
e.Cancel = true;
}
}
}
答案 1 :(得分:0)
根据您所描述的内容,您可以使用UserAddedRow event获取事件并验证新行是否已正确填充。
您可以通过Rows Property
获取DataGridViewRowCollection中的数据答案 2 :(得分:0)
我最近有同样的问题。我在Windows窗体中间有一个DGV,它接收紧急联系(EC)信息。它允许用户输入多个EC。每个EC的名称,电话和电子邮件必须在输入时在每一行上进行验证。
我这样做了:
private void CheckECName(DataGridViewCellValidatingEventArgs newValue)
{
StringBuilder _errMsg = new StringBuilder();
string _cellMsg = "";
string _eCon_Name = "";
DataGridViewCell cell = this.dgEmergencyContact.Rows[newValue.RowIndex].Cells[newValue.ColumnIndex];
if (!String.IsNullOrEmpty(newValue.FormattedValue.ToString()))
{
_eCon_Name = newValue.FormattedValue.ToString();
if (!((_eCon_Name.Trim()).Length == 0))
{
// Match the regular expression pattern against a text string.
// Only alphabetic and space characters
//Match m = r.Match(wholeName);
//if (!m.Success)
if (TestInput.IsFullName(_eCon_Name))
{
string _cellLabel = "Emergency Conact Name";
_cellMsg = "Emergency Contact name";
NotifyUserAndContinue(_cellLabel, _cellMsg, newValue);
return;
}
else
{
_cellMsg = "Emergency Contact name";
_errMsg.Append("The Emergency Contact Name: " + _eCon_Name + " is entered incorrectly.");
_errMsg.AppendLine("Acceptable format: First Last");
_errMsg.AppendLine("\n\tFirst MI Last");
_errMsg.AppendLine("\n\tFirst Middle Last");
_errMsg.AppendLine("\nNo commas or periods, please.");
NotifyUserAndForceRedo(_cellMsg, _errMsg.ToString(), newValue);
return;
}
}
}
}
private void CheckECEmail(DataGridViewCellValidatingEventArgs newValue)
{
StringBuilder _errMsg = new StringBuilder();
string _cellMsg = "";
string techEmail = newValue.FormattedValue.ToString().Trim();
DataGridViewCell cell = this.dgEmergencyContact.Rows[newValue.RowIndex].Cells[newValue.ColumnIndex];
if (!(techEmail.Length == 0))
{
// Send the contents of the Personal Email to the tester class
if (TestInput.IsEmail(techEmail))
{
string _cellLabel = "Emergency Conact Email";
_cellMsg = "Emergency Contact email address";
NotifyUserAndContinue(_cellLabel, _cellMsg, newValue);
return;
}
else
{
_cellMsg = "Emergency Contact email address";
_errMsg.Append("An invalid email address has been entered.");
_errMsg.AppendLine("Format email@server.type (jim@place.com)");
NotifyUserAndForceRedo(_cellMsg, _errMsg.ToString(), newValue);
return;
}
}
}
private void CheckECPhone(DataGridViewCellValidatingEventArgs newValue)
{
StringBuilder _errMsg = new StringBuilder();
string _cellMsg = "";
string techPhone = newValue.FormattedValue.ToString().Trim();
DataGridViewCell cell = this.dgEmergencyContact.Rows[newValue.RowIndex].Cells[newValue.ColumnIndex];
if (!(techPhone.Length == 0))
{
// Send the contents of the Personal Phone to the tester class
if (TestInput.IsPhone(techPhone))
{
string _cellLabel = "Emergency Conact Phone";
_cellMsg = "Emergency Contact phone number";
NotifyUserAndContinue(_cellLabel, _cellMsg, newValue);
return;
}
else
{
_cellMsg = "Emergency Contact phone number";
_errMsg.Append("An invalid phone number has been entered.");
_errMsg.AppendLine("Acceptable formats: 8606782345");
_errMsg.AppendLine("\t860-678-2345");
_errMsg.AppendLine("\t(860) 678-2345");
_errMsg.AppendLine("\t(860) 678 - 2345");
NotifyUserAndForceRedo(_cellMsg, _errMsg.ToString(), newValue);
return;
}
}
}
private void NotifyUserAndForceRedo(string cellMessage, string errorMessage, DataGridViewCellValidatingEventArgs newValue)
{
DataGridViewCell cell = this.dgEmergencyContact.Rows[newValue.RowIndex].Cells[newValue.ColumnIndex];
MessageBox.Show(errorMessage);
dgEmergencyContact.Rows[cell.RowIndex].ErrorText = String.Format("{0} is entered incorrectly", cellMessage);
cell.ErrorText = String.Format("{0} is entered incorrectly", cellMessage);
try
{
dgEmergencyContact.EditingControl.BackColor = Color.OrangeRed;
}
catch (Exception)
{
}
newValue.Cancel = true;
}
private void NotifyUserAndContinue(string cellLabel, string cellMessage, DataGridViewCellValidatingEventArgs newValue)
{
DataGridViewCell cell = this.dgEmergencyContact.Rows[newValue.RowIndex].Cells[newValue.ColumnIndex];
string _goodMsg = String.Format("A valid {0} has been entered.", cellMessage);
AutoClosingMessageBox.Show(cellMessage, cellLabel, 2500);
cell.Style.BackColor = Color.White;
cell.ErrorText = "";
dgEmergencyContact.Rows[cell.RowIndex].ErrorText = "";
}

这会在用户标签或输入时验证每个单元格。我在背面做了代码,根据我认为的有效名称,电话号码和电子邮件进行注册检查,然后返回bool结果。
希望这有帮助,有人。