George Shepherd's Windows Forms FAQ
Questions and answers in this FAQ have been collected from newsgroup posts, various mailing lists and the employees of Syncfusion.

5.84 How can I prevent all the cells in my DataGrid from being edited without deriving GridColumnStyle


One solution is to remove all the editing controls from the DataGrid.Controls collection. Without these controls, the grid contents cannot be edited. There is a technical problem that requires a little care. You do not want to delete all the controls in DataGrid.Controls as you would lose your scrollbars.

The code below deletes all controls except the scrollbars. Alternatively, you could also loop through the controls and only delete the TextBox.

[C#]
ArrayList al = new ArrayList();
foreach(Control c in this.dataGrid1.Controls)
{
     if(c.GetType() == typeof(VScrollBar) || c.GetType() == typeof(HScrollBar))
     {
          al.Add(c);
     }
}
this.dataGrid1.Controls.Clear();
this.dataGrid1.Controls.AddRange((Control[]) al.ToArray(typeof(Control)));

[VB.NET]
Dim al As New ArrayList()
Dim c As Control
For Each c In Me.dataGrid1.Controls
     If c.GetType() = GetType(VScrollBar) Or c.GetType() = GetType(HScrollBar) Then
          al.Add(c)
     End If
Next c
Me.dataGrid1.Controls.Clear()
Me.dataGrid1.Controls.AddRange(CType(al.ToArray(GetType(Control)), Control()))



Windows Forms-Datagrid

© 2001-2009 Copyright Syncfusion Inc. All rights reserved.  |  Privacy Policy  |  Contact  |  Sitemap