2012-04-26 87 views
8

DataGridViewコントロールを持つwinFormがあります。これは5つの列を含み、そのうちの1つはCheckBox列です。同じ列の別の列にある値に基づいて、この列のチェックボックスセルを有効または無効にしたいとします。DataGridView CheckBox列の特定のチェックボックスセルを無効にする方法

私はDisabledCheckBoxCell

を使用して列全体を無効にすることができます。しかし、それは無効な状態で列全体になります。ここで

と、SourceColumn、DataGridViewのの抜粋です| DestinationColumn
真                                |有効
真                                |有効
偽                              |無効にする

誰でもアイデアを持っていますか、これはどのように.Netで実現できますか。

答えて

0

GridViewのRowDataBoundイベントを使用してみてください。 eventargsを行にキャストし、この行のコントロールを見つけて無効にすることができます。 このイベントは、ヘッダーとフッターの場合でもグリッドビューの各行に対して発生しますので、例外をキャッチしないようにしてください。ここ は、いくつかのコードは、あなたがそれが無効になっているかのようにように見せることができ、チェックボックスのスタイルを変更することによってのでDataGridViewCheckBoxColumnが無効と呼ばれるプロパティを持っていないあなたに

protected void xxx_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if ((e.Row != null) && e.Row.RowType == DataControlRowType.DataRow) 
+0

私はそれぞれの行のセルを無効にしたい、私は、CellのEnabledプロパティを見つけることができませんでした。私はRowPostPaintイベントを使用しています。 –

+0

それはwinformsですか? –

+0

個々のセルを無効にする機能はないかもしれませんが、無効にするセルの値を元に戻すなど、セルがクリックされている間何かを行うことができます – Nick

20

ビジェイ、

に有用であるかもしれないです。次のコードを見てください。

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) 
    { 
     if (e.RowIndex == 1) 
     { 
      DataGridViewCell cell=dataGridView1.Rows[e.RowIndex].Cells[0]; 
      DataGridViewCheckBoxCell chkCell = cell as DataGridViewCheckBoxCell; 
      chkCell.Value = false; 
      chkCell.FlatStyle = FlatStyle.Flat; 
      chkCell.Style.ForeColor = Color.DarkGray; 
      cell.ReadOnly = true; 

     } 

    } 
+0

ご返信ありがとうございます。これはいくつかの小さな問題で動作しています。列ヘッダーにもチェックボックスのセルがあります。これも彼らに影響しています。他のAppに切り替えて元のAppに戻すと、UIが歪んで見えます。 –

1

私が表示されるまで、実際の無効化]チェックボックスを取得するには、このようなことをやってしまった:

using System.Windows.Forms.VisualStyles; 

public partial class YourForm : Form 
{ 

    private static readonly VisualStyleRenderer DisabledCheckBoxRenderer; 

    static YourForm() 
    { 
     DisabledCheckBoxRenderer = new VisualStyleRenderer(VisualStyleElement.Button.CheckBox.UncheckedDisabled); 
    } 

    private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) 
    { 
     if (e.RowIndex > -1) 
     { 
      int checkBoxColumnIndex = this.yourCheckBoxColumn.Index; 
      var checkCell = (DataGridViewCheckBoxCell)this.dataGridView[checkBoxColumnIndex, e.RowIndex]; 
      var bounds = this.dataGridView.GetCellDisplayRectangle(checkBoxColumnIndex , e.RowIndex, false); 

      // i was drawing a disabled checkbox if i had set the cell to read only 
      if (checkCell.ReadOnly) 
      { 
       const int CheckBoxWidth = 16; 
       const int CheckBoxHeight = 16; 

       // not taking into consideration any cell style paddings 
       bounds.X += (bounds.Width - CheckBoxWidth)/2; 
       bounds.Y += (bounds.Height - CheckBoxHeight)/2; 
       bounds.Width = CheckBoxWidth; 
       bounds.Height = CheckBoxHeight; 

       if (VisualStyleRenderer.IsSupported) 
       { 

        // the typical way the checkbox will be drawn 
        DisabledCheckBoxRenderer.DrawBackground(e.Graphics, bounds); 
       } 
       else 
       { 

        // this method is only drawn if the visual styles of the application 
        // are turned off (this is for full support) 
        ControlPaint.DrawCheckBox(e.Graphics, bounds, ButtonState.Inactive); 
       } 
      } 
     } 
    } 
} 
関連する問題