Grant Jenkins

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.



Re: Windows Forms General DataGridView - Column/Cell Not Selectable

Bob zhu - MSFT

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






Re: Windows Forms General DataGridView - Column/Cell Not Selectable

gjen020

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.





Re: Windows Forms General DataGridView - Column/Cell Not Selectable

Gavin Jin - MSFT

foreach (DataGridViewColumn c in dataGridView1.Columns)
if (c.Index != 0) c.ReadOnly = true;




Re: Windows Forms General DataGridView - Column/Cell Not Selectable

Bob zhu - MSFT

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

}

}







Re: Windows Forms General DataGridView - Column/Cell Not Selectable

gjen020

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.