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

Platform: WinForms| Category: Datagrid

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()))

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.