2016-09-17 25 views
3

我發現了很多類似的問題和答案,但沒有人幫助我解決我的問題。如何禁用dataGridView中不可點擊的按鈕(c#windows應用程序)

這裏是我現在面臨這個問題 dataGridView with buttoncell

我想如果按鈕上的文字說:「接受的」禁止(或不點擊)按鈕我的應用程序的截圖, 這裏就是我並且不工作

 private void Cellcontent() 
    { 
     foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 
      if ((row.Cells["Status"].Value.ToString()) == "ACCEPTED") 
      { 
       DataGridViewButtonCell cell = row.Cells["Status"] as DataGridViewButtonCell; 
       cell.ReadOnly = true; //if 1 cell will be disabled (not clickable) 

      } 
     } 
    } 

回答

0

,如果你使用一個不同的細胞,當值不ACCEPTED不介意,你可以做到以下幾點:

foreach (DataGridViewRow item in dataGridView1.Rows.OfType<DataGridViewRow>().Where(c => c.Cells[buttonCol.Index].Value != null && c.Cells[buttonCol.Index].Value.ToString() == "ACCEPTED")) 
{ 
    //you can replace the button with a textbox cell that contains the rejected value 
    item.Cells[buttonCol.Index] = new DataGridViewTextBoxCell { Value = "ACCEPTED" }; 
    item.Cells[buttonCol.Index].ReadOnly = true; 
    //note that you have to make a new button cell and replace the rejected ones if the status is updated later on to be able to use the button. 
} 

希望這會有所幫助。