2012-04-03 47 views
4

我在我的WinForm應用程序在C#3.5一個DataGridView。防止在當前行被填充之前在DataGridView中出現新行?

AllowUserToAddNewRow屬性設置爲true。當用戶在DataGridView中鍵入任何文本時,另一個新行將自動添加到DataGridView。我不想添加這個新的行,直到在當前行上執行了一些檢查,並且所有必要的信息都被填充在那裏。

例子:我有一個空行一個DataGridView: DataGridView with one blank row

當我開始鍵入一個新行補充說,這是太快:

我想的是,新行僅在用戶輸入數量後才能添加數量:

回答

0

我認爲事件CellClick可以檢查您是哪一列,然後添加一個新行,如:DataGridView1.Rows.Add()

+0

它不練琴用戶定位鼠標,可能是他們通過鍵盤接口 – 2012-04-03 06:56:01

4

設置AllowUserToAddNewRow =假 現在開始添加一個空白行的數據源的如。如果你只是之前

dataGridView1.DataSource = DT; 

結合在DataGridView到一個名爲DT數據表,然後像做

DT.Rows.Add(DT.NewRow()); 

這是有一個空白行開始,這樣的第一條記錄,可以輸入。 然後處理該事件dataGridView1.CellEndEdit,在這種情況下寫這樣的:

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
    { 
     if (e.ColumnIndex == 1)//The index of your Quantity Column 
     { 
      int qty = (int)DT.Rows[e.RowIndex][e.ColumnIndex]; 
      if (qty > 0)//Your logic if required 
      { 
       DT.Rows.Add(DT.NewRow());      
      } 
     } 
    } 
+0

有沒有事件打壓行添加命令 – 2012-04-03 11:12:47

+0

抱歉無法理解你...你什麼意思取消行補充的嗎?如果你的意思是不應該在DataGrid中添加新行,那麼只需設置AllowUserToAddNewRow = false,如上面第一行中提到的 – 2012-04-04 09:55:30

+0

我希望Row應該有條件地添加 – 2012-04-05 07:31:33

0

這是它是如何實現的。

一)您可以使用

String.IsNullOrWhiteSpace(GridPGLog.Rows[e.RowIndex].Cells[0].value.toString()) 
using (or) Cells[0] || cells[1] || cell[2] || .. 

如果發現任何錯誤,將焦點設置錯誤單元格並強制用戶輸入數據檢查RowLeave事件當前行的內容。

DataGridViewRow rowToSelect = this.dgvJobList.CurrentRow; 
rowToSelect.Selected = true; 
rowToSelect.Cells[0].Selected = true; 
this.dgvJobList.CurrentCell = rowToSelect.Cells[0]; 

B),或者你可以放置一個保存按鈕,並檢查使用foreach循環

+0

上面提到的這個可以通過e.Cancel = true在RowValidating事件上很容易實現,但是如果當前行未填充,我不希望添加新行。 – 2012-04-03 11:10:16

+0

如果你執行它,用戶將無法集中在行外添加新行 – arvind 2012-04-03 13:43:21

+0

但這不是我正在尋找的答案 – 2012-04-03 13:51:38

0

我知道這是舊的所有新添加的行。 最簡單的方法是,取消選中「啓用添加」,從設計視圖

enter image description here

+0

這是迄今爲止最簡單的方法解決在不需要時出現的額外空行。 – 2017-04-24 22:29:32

1

基本上它的一些事件和啓用/禁用AllowUserToAddRow屬性的簡單打法:

public Form1() 
     { 
      InitializeComponent(); 
      //creating a test DataTable and adding an empty row 
      DataTable dt = new DataTable(); 
      dt.Columns.Add("Column1"); 
      dt.Columns.Add("Column2"); 
      dt.Rows.Add(dt.NewRow()); 

      //binding to the gridview 
      dataGridView1.DataSource = dt; 

      //Set the property AllowUserToAddRows to false will prevent a new empty row 
      dataGridView1.AllowUserToAddRows = false; 
     } 

現在事件... 當細胞識別的編輯就會觸發一個叫做CellBeginEdit事件。當它在編輯模式下設置的AllowUserToAddRows假

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) 
{ 
    dataGridView1.AllowUserToAddRows = false; 
} 

當細胞識別編輯它會觸發一個叫做CellEndEdit事件的結束。當它結束編輯模式檢查您的條件。根據結果​​設置AllowUserToAddRows爲true使其保持爲false。

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
{ 
    //instead of MessageBox there could be as well your check conditions 
    if (MessageBox.Show("Cell edit finished, add a new row?", "Add new row?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 
     dataGridView1.AllowUserToAddRows = true; 
    else dataGridView1.AllowUserToAddRows = false; 
} 
相關問題