DataGridView - Column/Cell Not Selectable
Hi,
I have a DataGridView with 5 columns but only want the user to be able to select cells in one column (prevent selection of the other cells)
I have tried to find a property for a column to say you can't select it, but can't find anything. Surely you don't have to write a whole bunch of event handlers (cellclick, keydown, etc) and dynamically change the focus to the one columns cell you allow to select.
Can anyone help me with this - even microsoft access has the ability to not allow selection of a particular column cell.
Thanks in advance,
Grant.
All Replies
Hi gJen :
we can judge if the column is column2, then cancel celledit event as follow:
private
void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e){
if (((DataGridView)sender).CurrentCell.ColumnIndex == 1)e.Cancel =
true;}
also we can set columns readonly:)
hope it help you
Hi,
Thanks for your reply - but it didn't answer my question.
I can easily make sure that only some columns are editable, but I want to make some columns not selectable.
So if a user tries to click, tab, scroll, etc to a particular column it won't let them.
Basically - I have a grid and want to have only one column selectable so can enter marks for that column, but have some other columns visible for infomation only.
Kind Regards,
Grant.
- foreach (DataGridViewColumn c in dataGridView1.Columns)
if (c.Index != 0) c.ReadOnly = true; Hi:
we can use selected=false to deselect the cell in GDV
as:
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (((DataGridView)sender).CurrentCell.ColumnIndex == 1)
e.Cancel = true;
}
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if(this.dataGridView1.CurrentCell.ColumnIndex==1)
this.dataGridView1.CurrentCell.Selected = false;
}but if you want to cancel the tab key, maybe we need overried the class method as follow
public class myDgv : DataGridView
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (this.CurrentRow.Index == 0)
{
if (e.KeyCode == Keys.Tab)
{
return;
}
}
base.OnKeyDown(e);
}
}
Hi Gavin,
That just makes them non editable (not answered my question) which I have already done through the columns property - but I want them to be non selectable (by tab, mouse click, keyboard arrows, anything)
Thanks anyway.
Cheers,
Grant.
Hi Grant:
did you get the answer to your question? I've got the same trouble.
can anyone help me, plese???
pzmpzm
Hi,
No - I actually gave up trying to get it to work.
I'm currently rebuilding my app using Silverlight which I'm sure will allow me to do anything I want.
I've built a few small apps now using Silverlight and just love it - complete flexibility.
Hope someone can help you with this, and still can't understand why it's so hard to do something so simple.
Kind Regards,
Grant.
Hi Grant,
can you read spanish? I have found a web page where there is an article called, in spanish, "Not selectable colum in dataGridView":
http://rafavargas.wordpress.com/2007/07/23/columna-no-seleccionable-en-un-datagridview/
I think it is what I was looking for,
pzm
One idea that could work is overriding the SelectionChanged event.
Determine what column was selected and if its not the correct column just change your selection to null
private
void dataGridView1_SelectionChanged(object sender, EventArgs e){
if (dataGridView1.CurrentCell.ColumnIndex == 0)//0 being the invalid column indexdataGridView1.CurrentCell.Selected =
false; }if you want to keep the previous cell then you will probably need to keep track of the last cell location and revert back to that selection instead.
Hi gjen020,
Set dgv.SelectionMode = CellSelect, set all non-editable columns to readonly and trap the CellStateChanged event and set selected to false if the column index doesn't represent an editable column. Works for me!
Steve
Private Sub dgv_CellStateChanged( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.DataGridViewCellStateChangedEventArgs) _ Handles _dgv.CellStateChanged
If e.Cell.ColumnIndex <> 1 Then
e.Cell.Selected =
False End If
End SubHi
You can do that for that first change the selection mode property of the grid to Cell select mode.
hope this will solve your problem....
i.e on simple way just do this
With Me.dataGridView1
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
End Withjust chk it out once...

