سؤال

i'm searching a way to make an cell an mandatory cell on a new row. The row consists of 7 cells. The Problem i have is, that if User enters the new row, he can skip the very important first cell which have to contain a required number. I want that if user enters the new row that he has to enter first the required cell in that row, before he can add more informations to the second cell and so on. I've tried row validating and checked for DBNull and so on, but nothing works. The best solution would be, that if an new row is entered, the first cell jumps to edit mode. If number is entered, the following cells can be edited, else not. If user cancels adding, the current row isn't added.

Thx for any kind of suggestions and info!

هل كانت مفيدة؟

المحلول

I've found a simple solution by myself. Just check in CellBeginEdit if the first cell has been filled on the new row - else other cells can not be edited and so no new row is been added. May someone else can need it too. Thank you very much for your help!

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;
                }
            }
        }

نصائح أخرى

Based on what you have described you could use the UserAddedRow event to grab the event and verify that the new row is populated correctly.

You can grab the data in a DataGridViewRowCollection through the Rows Property

I recently had this same issue. I have a DGV in the middle of the a Windows Form that takes Emergency Contact (EC) information. It allows users to enter multiple ECs. The Name, Phone, and Email of each EC must be validated on each row at the time of entry.

I did this:

        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 = "";
        }

This validates each cell as the user tabs or enters. I have code on the back doing regedit checks against what I consider a valid name, phone number, and email, and they return bool results.

Hope this helps, someone.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top