简体   繁体   English

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

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

i'm searching a way to make an cell an mandatory cell on a new row. 我正在寻找一种使单元格成为新行上必选单元格的方法。 The row consists of 7 cells. 该行包含7个单元格。 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. 我尝试了行验证并检查了DBNull等,但没有任何效果。 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. 只需检查CellBeginEdit是否在新行中填充了第一个单元格-否则其他单元格将无法编辑,因此不会添加任何新行。 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. 根据您的描述,您可以使用UserAddedRow事件获取事件并验证是否正确填充了新行。

You can grab the data in a DataGridViewRowCollection through the Rows Property 您可以通过Rows属性获取DataGridViewRowCollection中的数据

I recently had this same issue. 我最近有同样的问题。 I have a DGV in the middle of the a Windows Form that takes Emergency Contact (EC) information. 我在Windows表单的中间有一个DGV,该表单接受紧急联系人(EC)信息。 It allows users to enter multiple ECs. 它允许用户输入多个EC。 The Name, Phone, and Email of each EC must be validated on each row at the time of entry. 每个EC的名称,电话和电子邮件在输入时必须在每一行上进行验证。

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. 我背面有代码,可以根据我认为有效的姓名,电话号码和电子邮件进行regedit检查,然后返回布尔结果。

Hope this helps, someone. 希望有人帮忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM