2012-09-26 31 views
0

我正在尋找一種方法來使單元格成爲新行上的強制單元格。 該行由7個單元組成。我的問題是,如果用戶輸入新行, ,他可以跳過必須包含必需數字的非常重要的第一個單元格。我想要 如果用戶輸入新的行,他必須首先輸入該行所需的單元格,然後才能向第二個單元格添加更多信息,等等。我試過行驗證和檢查DBNull等,但沒有任何作品。最好的解決方案是,如果輸入新行,第一個單元跳轉到編輯模式。如果輸入數字,則可以編輯以下單元格,否則不可以。如果用戶取消添加,則不會添加當前行。C#Datagridview使新行上的強制單元格

Thx對於任何類型的建議和信息!

+0

你可以告訴我們不起作用的代碼嗎? –

+0

我想這... ​​ – FreewareFire

+0

目前我已經在這裏結束了這幾乎是我要找的 私人無效dgvList_RowValidating(對象發件人,DataGridViewCellCancelEventArgs E) { //MessageBox.Show(dgvList [2,e.RowIndex] .Value.ToString()); (dgvList [2,e.RowIndex] .Value == null) { dgvList.CurrentCell = dgvList [2,e.RowIndex]; dgvList.BeginEdit(true); dgvList.AllowUserToAddRows = false;其他 dgvList.AllowUserToAddRows = true; } – FreewareFire

回答

1

搶在DataGridViewRowCollection的數據,我發現我自己一個簡單的解決方案。只需檢查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; 
       } 
      } 
     } 
0

根據您所描述的內容,您可以使用UserAddedRow event來抓取事件並驗證新行是否正確填充。

您可以通過Rows Property

+0

這裏的問題是,我想停止添加新的行,直到最後一個不正確。如果我使用UserAddedRow,該行也將被添加如果第一列沒有填寫 – FreewareFire

+0

如果新行不正確,那麼你可以刪除該行,試着查看[this](http://msdn.microsoft.com/zh-cn/library/default.aspx)。COM/EN-US /庫/ 7ehy30d4%28V = vs.110%29.aspx) – user1424311

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 protected] ([email protected])"); 
 

 
        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檢查,他們返回布爾結果。

希望這有助於某人。

相關問題