2012-11-06 105 views
2

在我的datagridview中,四列1 & 2是隻讀列3 & 4得到了數字值。我想比較一下,第4列必須大於第3列。 例如:比較datagridview中的2個不同列

enter image description here

如果第4列值小於第3列,然後我想提議消息不導航到另一個控制。

我的簡單方法似乎不起作用。我怎樣才能比較2種特定的列這種情況?

private void datagridview_CellValidating(object sender, CellValidatingEventArgs e) 
{ 
    try 
    { 
     int bbor = datagridview.CurrentCell.ColumnIndex; 
     int ebor = datagridview.CurrentCell.RowIndex; 
     if (ebor <= bbor) 
     { 
      MessageBox.Show("Please verify the value"); 
      e.Cancel = true; 
     } 
    } 
    catch (Exception exception) 
    { 
    } 
} 
+0

你爲什麼要比較的行和列的索引?從適當位置的每個單元格中獲取值並比較值,而不是索引。 –

+0

在某些情況下,在datagridview中它可能有8行,我無法比較每一行。 BY排行索引可能會更好..如果我錯了,請糾正我。謝謝你; – linguini

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

1

我們再次見面。使用cell_click事件:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
{ 
    if (e.ColumnIndex != 0) 
    { 
     if (Double.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) <= Double.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString())) 
     { 
      MessageBox.Show("Please verify the value"); 
     } 
    } 
} 

編輯1:這似乎做工精細,還是讓我知道。

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
{ 
    if (e.ColumnIndex != 0) 
    { 
     if (Double.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) <= Double.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString())) 
     { 
      MessageBox.Show("Please verify the value"); 
      e.Cancel = true; 
     } 
    } 
} 

編輯2:更新了Telerik控制

private void radGridView1_CellValidating(object sender, Telerik.WinControls.UI.CellValidatingEventArgs e) 
    { 
     if (e.ColumnIndex != 0) 
     { 
      if (e.Value != null && radGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value != null) 
      { 
       if (Double.Parse(e.Value.ToString()) <= Double.Parse(radGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString())) 
       { 
        MessageBox.Show("error"); 
        e.Cancel = true;       
       } 
      } 
     } 
    } 

enter image description here

+0

很高興再次見到你,謝謝。如果我使用e.cancel它似乎不起作用。 – linguini

+0

@linguini你想在消息更改時彈出消息嗎?或者你想在點擊單元格時彈出它? – KreepN

+0

其實,如果第四個col值小於第三個Col值(從第一行開始),那麼用戶不應該導航到另一個單元格。這就是爲什麼我試圖細胞驗證。如我錯了請糾正我。謝謝 – linguini

1

我想看到的東西或多或少這樣的:

如果你想查詢的所有行:

DataRow dr; 
for(int i = datagridview.Rows.Count-1; i > 0; i--) { 
    dr = datagridview.Rows[i]; 
    if(dr[e.ColumnIndex] > dr[e.ColumnIndex+1]){ 
      //your message code  
      e.Cancel = true; 
     break; (or return;) 
    } 
} 

如果你想只檢查當前行了細胞被正在編輯:

DataRow dr = datagridview.Rows[e.RowIndex]; 
e.Cancel = dr[e.ColumnIndex] > dr[e.ColumnIndex+1]; 
if(e.Cancel) 
    //your message code 

也許你需要將對象轉換爲int來進行比較。

1

查看行物業的DataGridView http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx

this.dataGridView1 [COL,列] .value的

引用了特定的細胞爲每一行

foreach (Row r in this.dataGridView1.Rows) { 
    if (r.Cells[3].Value <= r.Cells[2].Value) { 
    System.Console.WriteLine ("error"); 
    } 
} 
+0

我沒有完全明白你的意思。我試過你的代碼,它不工作。 – linguini

+0

發佈錯誤消息。 – sammarcow

+0

int cell =(int)r.Cells [2] .Value; int cells =(int)r.Cells [3] .Value;如果(cell <= cell) MessageBox.Show(「check」);如果(cell <= cell) } – linguini

1

爲了您的驗證請檢查您是否希望使用FormattedValue property來查看您的用戶想要在他們編輯的單元格中插入的值。您不能使用當前單元格值,因爲它不會更新爲新值,直到CellValidating完成而DataGridViewCellValidatingEventArgs.Cancel設置爲true。

事情是這樣的:

private void datagridview_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { 
    // This is the new proposed value the user entered; could be for column 3 or 4. 
    int newValue = int.Parse(e.FormattedValue.ToString()); 

    // See which column fired the CellValidating event and use the new proposed value for it 
    // in place of the cell's actual value for purposes of our validation. 
    int col3Value = (e.ColumnIndex == 2) ? newValue : (int)dataGridView1[2, e.RowIndex].Value; 
    int col4Value = (e.ColumnIndex == 3) ? newValue : (int)dataGridView1[3, e.RowIndex].Value; 

    if (col3Value <= col4Value) { 
     MessageBox.Show("Please verify the value"); 
     e.Cancel = true; 
    } 
} 

我在這裏展示的代碼是展示一個解決您的問題。在您的實際生產代碼 中,您需要驗證從對象轉換爲int是否成功(通過int.TryParse)或捕獲此操作失敗時引發的異常。發生這種情況時,您可以Cancel = true單元格驗證並向用戶顯示他必須輸入數字的消息。

另一個快速注:don't use empty catch blocks(儘管我意識到這可能不在您的生產代碼中)。

+0

我收到一條錯誤消息,無法將索引應用於表達式類型。 – linguini

+0

你會在哪一行發現錯誤?另外,你使用的DataGridView?如果你是那麼你的第二個參數應該是'DataGridViewCellValidatingEventArgs'類型。 –