0

我正在寻找一种使单元格成为新行上的强制单元格的方法。该行由 7 个单元格组成。我遇到的问题是,如果用户输入新行,他可以跳过必须包含所需数字的非常重要的第一个单元格。我希望如果用户输入新行,他必须首先在该行中输入所需的单元格,然后才能向第二个单元格添加更多信息,依此类推。我尝试过行验证并检查 DBNull 等,但没有任何效果。最好的解决方案是,如果输入新行,第一个单元格会跳转到编辑模式。如果输入数字,则可以编辑以下单元格,否则不能。如果用户取消添加,则不会添加当前行。

感谢您提供任何建议和信息!

4

3 回答 3

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;
                }
            }
        }
于 2012-09-27T14:01:20.290 回答
0

根据您所描述的内容,您可以使用UserAddedRow 事件来获取事件并验证新行是否已正确填充。

您可以通过Rows 属性获取 DataGridViewRowCollection 中的数据

于 2012-09-26T16:52:45.067 回答
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 = "";
        }

这会在用户选择或输入时验证每个单元格。我在背面有代码对我认为有效的姓名、电话号码和电子邮件进行 regedit 检查,它们返回布尔结果。

希望这会有所帮助,有人。

于 2016-07-08T13:30:19.100 回答