2011-10-20 7 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