views:

1847

answers:

5

I have a binded DataGridView that contains a large amount of data. The problem is that some cells has to be ReadOnly and also when the user navigates with TAB or ENTER between cells, the ReadOnly cells should be bypassed. What's the best way of making some specific cells ReadOnly imediatly after loadging?

Looping through cells after I set DataSource is not a good idea taking in consideration that the grid has a large amount of data. Also, making the cell ReadOnly on CellEnter does not work because when navigating with TAB key I have to already know if the next cell is ReadOnly or not.

+2  A: 

Try to make the column rather than individual cells readonly before binding the data:

this.dgrid.Columns["colName"].ReadOnly = true;

If you need to do for individual cells within the column, then you will have to loop and set them like this:

this.dgridvwMain.Rows[index].Cells["colName"].ReadOnly = true;
Rashmi Pandit
I cannot, some of the cells within a Column could be ReadOnly and others not. It depends on some flags.
For individual columns you need to loop and set it to true. It will be time-consuming, but I don't think there is any other alternative.
Rashmi Pandit
A: 

Once the column is read only (see Rashmi's response) you can handle this event...

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Tab)
    {
        Boolean readOnly = (sender as DataGridView).SelectedCells[0].ReadOnly;

        return;
    }

}

Which will get the next cell's read only property.

Thanks

Chalkey
I cannot put on ReadOnly a column because some of the cells within the same Column could be ReadOnly and others not. It depends on some flags.
To be hounest am not 100% sure weather the column being ReadOnly will even affect the code I wrote. It might be worth giving it a try...
Chalkey
A: 

I haven't tried this.

But, you could set the cell's readonly property to true (as per Rashmi), on RowEnter event?

I guess RowEnter event should fire when you move from one row to the other (or it should when you change from cell A1 to B3).

Does that help at all?

shahkalpesh
A: 

There's a very nice sample here:
http://blogs.msdn.com/netcfteam/archive/2006/04/25/583542.aspx

You just need to override Paint() , I've used this on compact framework to change the backcolor depending on the cell contents so on the same note you shouldn't have any problem to set them read only.

Fiur
A: 

Could you not use a template column instead of a bound column then have a condition for the readonlyness of the field?

Then you could present a label for readonly and a textbox for editable. Labels would not interfere with your tab index.

<asp:TemplateColumn>
  <ItemTemplate>
<%
    if ( <%# Eval( "ReadOnlyFlag" ) %> )
    { 
%>
    <asp:Label Text="<%# Eval( "BoundColumn" ) %>" />
<%
    }
    else
    {
 %>
    <asp:Textbox Text="<%# Eval( "BoundColumn" ) %>" />
<%
    }
%>
    </ItemTemplate>
</asp:TemplateColumn>
Adam Fox