2011-10-19 15 views
2

2つのdatatable列のreadonlyプロパティをtrueに設定しました。DataGridViewでは、true(c#.net)を更新すると、新しい行を追加するときにcolumnのReadOnlyプロパティをfalseに設定します。

List.Columns[0].ReadOnly = true; 
    List.Columns[1].ReadOnly = true; 

しかし、私は唯一の彼らは、ユーザーが更新しようとしたとき、私は新しい行を追加しようとするとfalseにReadOnlyプロパティをオンにするので、ユーザーがDataGridViewのに新しい行を追加することができ、読み取り専用にしたいです。私は、データグリッドのCellDoubleClickイベントでこれをやろうとしましたが、呼び出されるようにbegineditが遅れるので何もしません。

if(e.RowIndex == GridView.Rows.Count-1) 
       GridView.Rows[e.RowIndex].Cells[1].ReadOnly = GridView.Rows[e.RowIndex].Cells[0].ReadOnly = false; 
      else 
       GridView.Rows[e.RowIndex].Cells[1].ReadOnly = GridView.Rows[e.RowIndex].Cells[0].ReadOnly = true; 

あなたがtrueにセル読み取り専用プロパティを作成するcellbegin編集を使用する必要が任意のアイデア

答えて

5

。 。

private void dataGridView1_CellBeginEdit(object sender,DataGridViewCellCancelEventArgs e) 
    { 
     if (dataGridView1.Columns[e.ColumnIndex].Name == "ColName0") 
     { 
      // you can check whether the read only property of that cell is false or not 

     } 
    } 

は私が...それ意志はあなたの役に立てば幸い

2

それはこのようにして作成された行をすることはできませんつまり、あなたは彼らが新しい行でない限り、読み取り専用グリッドのすべての行を作成されて何をしたいかのように聞こえます編集されました。それはあなたが何ができるか、正しい場合にはそのようなDataBindingCompleteイベント中に読み取り専用に行を設定されている:

dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete); 

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) 
{ 
    foreach (DataGridViewRow item in dataGridView1.Rows) 
    { 
     if (!item.IsNewRow) 
      item.ReadOnly = true; 
    } 
} 

重要な部分は、行が新しい行であるかどうかを確認するためのチェックです。

関連する問題