DataGrid Cells

Hi! Everybody 

I need help regarding data grid cells. I want the cells of my datagrid to be locked. As in the default behaviour, whenever we select a cell, the text is selected and highlighted as well, and looks to be editable, although it is not(Coz readly only property for the grid has been set to false). I want to eliminate this behaviour. 
I want my cells to be selected as the same way in default. If I select a particular cell, the whole cell should be selected and should not look editable. 

In other and simple words, I want my cells to be locked 

Urgent response would help to solve my problem urgent. Coz I am looking for this from last two days or so.......... 

Mustansar 


Answer this question

DataGrid Cells

  • Vasya

    I'm trying to get familiar with this area so this is really a question. 

    Can't you set the cell style  editable = false


  • younessta

    Many Thanks!

    But, It seems to have the same behaviour as the Datagrid's Enabled property. But, I want to highlight(as blue) the cell as well. Then, I would be able to finish my task. Coz, I already captured the single click and double click for any cell. Now, the previous problem remains intact.

    I would be highly obliged if you would send me the snippet for doing this....

    Thanks a lot, once again, for anticipation.

  • SusanaMS

    I found that if you want to have a DataGrid which nothing happens when user clicks on it, you will have to execute the method RemoveAt(i) for ((DataView)dg).DataSource.Table.Columns.Count times (if the DataSource of the DataGrid is a DataView and the dg = DataGrid Object). The code is in C#:

    for (int i=0;i<dg.Controls.Count;i++)

    if (dg.Controls[i].GetType().ToString().IndexOf("DataGridTextBox") >= 0)

            for (int j=0;j<((DataView)dg).DataSource.Table.Columns.Count;j++)

                dg.Controls.RemoveAt(i);






  • Andrew Leckie

    I have to assume that you meant the ReadOnly proeprty of the DataGrid was set to TRUE.

    One option (though not exactly what you want) is to remove all of the textboxes from the datagrid after you set the datasource.  If the grid is readonly, removing the textboxes will prevent a cell from being selected.  The user can still select an entire row, but clicking an individual cell won't visibly do anything (the cell clicked still becomes current).

    If you HAVE to see that the cell is selected, you could create a custom datagridtextboxcolumn that changes the backcolor of the current cell.  This, combined with removing the textboxes, should give the exact functionality to spoke of.

    Here is a code routine to remove the textboxes from the grid:

            For i As Integer = DataGrid1.Controls.Count - 1 To 0 Step -1
                If DataGrid1.Controls(i).GetType Is GetType(DataGridTextBox) Then
                    DataGrid1.Controls.RemoveAt(i)
                End If
            Next

    Be sure to execute this after (and anytime after) you change the grid's datasource.

    Examples of creating a custom datagrid column can already be found in this forum.

  • DataGrid Cells