簡體   English   中英

C#Datagridview在新行上使必填單元格

[英]C# Datagridview make mandatory cell on new row

我正在尋找一種使單元格成為新行上必選單元格的方法。 該行包含7個單元格。 我的問題是,如果用戶輸入新行,他可以跳過非常重要的第一個單元格,該單元格必須包含所需的數字。 我希望如果用戶輸入新行,那么他必須先在該行中輸入所需的單元格,然后才能向第二個單元格中添加更多信息,依此類推。 我嘗試了行驗證並檢查了DBNull等,但沒有任何效果。 最好的解決方案是,如果輸入新行,則第一個單元格跳到編輯模式。 如果輸入數字,則可以編輯以下單元格,否則不能編輯。 如果用戶取消添加,則不會添加當前行。

謝謝任何建議和信息!

我自己找到了一個簡單的解決方案。 只需檢查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;
                }
            }
        }

根據您的描述,您可以使用UserAddedRow事件獲取事件並驗證是否正確填充了新行。

您可以通過Rows屬性獲取DataGridViewRowCollection中的數據

我最近有同樣的問題。 我在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檢查,然后返回布爾結果。

希望有人幫忙。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM