2011-10-20 79 views
1

我需要啓用/禁用datagridview的行列,這可以通過在綁定所有行後進行循環來輕鬆完成。 但我想這樣做,而數據綁定...有沒有辦法做到這一點?也如何啓用/禁用行單元格?Winform DataBinding

dgvLayout.AutoGenerateColumns = false; 
dgvLayout.DataSource = list; 
細胞點擊

,但它不工作

if ((dgvLayout.Rows[e.RowIndex].Cells["colControlText"].Value.ToString()) == "-Invalid-") 
{ 
    if (e.ColumnIndex == 2 || e.ColumnIndex == 5) 
    { 
     return; 
    } 
    else if (e.ColumnIndex == 1) 
    { 
     return; 
    } 
} 
+0

winform它的頂部! – thewayman

回答

2

您可以使用此解決方案的啓用和禁用電池

要「禁用」一個單元格,它必須是隻讀的,並以某種方式變爲灰色。此功能啓用/禁用DataGridViewCell:

/// <summary> 
    /// Toggles the "enabled" status of a cell in a DataGridView. There is no native 
    /// support for disabling a cell, hence the need for this method. The disabled state 
    /// means that the cell is read-only and grayed out. 
    /// </summary> 
    /// <param name="dc">Cell to enable/disable</param> 
    /// <param name="enabled">Whether the cell is enabled or disabled</param> 
    private void enableCell(DataGridViewCell dc, bool enabled) { 
     //toggle read-only state 
     dc.ReadOnly = !enabled; 
     if (enabled) 
     { 
      //restore cell style to the default value 
      dc.Style.BackColor = dc.OwningColumn.DefaultCellStyle.BackColor; 
      dc.Style.ForeColor = dc.OwningColumn.DefaultCellStyle.ForeColor; 
     } 
     else { 
      //gray out the cell 
      dc.Style.BackColor = Color.LightGray; 
      dc.Style.ForeColor = Color.DarkGray; 
     } 
    } 
2

你可以在數據網格的RowsAdded事件給你寫代碼

+0

謝謝!多數民衆贊成我正在尋找 – thewayman