Windows Developer Center > Windows Forms Forums > Windows Forms General > DataGridView - Column/Cell Not Selectable
Ask a questionAsk a question
 

QuestionDataGridView - Column/Cell Not Selectable

  • Thursday, January 25, 2007 9:45 PMGrant Jenkins Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Friday, January 26, 2007 4:17 AMBob zhu - SJTU Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Friday, January 26, 2007 4:31 AMGrant Jenkins Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

  • Friday, January 26, 2007 5:24 AMGavin Jin - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    foreach (DataGridViewColumn c in dataGridView1.Columns)
                    if (c.Index != 0) c.ReadOnly = true;
  • Friday, January 26, 2007 7:37 AMBob zhu - SJTU Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

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

            }

        }


     

  • Friday, January 26, 2007 9:56 AMGrant Jenkins Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

  • Friday, April 25, 2008 3:36 PMpzmpzm Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Grant:

     

    did you get the answer to your question? I've got the same trouble. 

    can anyone help me, plese???

     

     

    pzmpzm

     

  • Sunday, April 27, 2008 1:21 AMGrant Jenkins Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

  • Monday, April 28, 2008 8:00 PMpzmpzm Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

     

     

     

     

     

  • Tuesday, August 26, 2008 12:04 AMOBBoy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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 index

    dataGridView1.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.

  • Thursday, October 30, 2008 8:57 AMstephengraham Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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 Sub

  • Thursday, October 30, 2008 9:24 AMSreenath G V Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi

     

    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....

     

  • Thursday, October 30, 2008 9:27 AMSreenath G V Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    i.e on simple way just do this

     

    With Me.dataGridView1
        .SelectionMode = DataGridViewSelectionMode.FullRowSelect
        .MultiSelect = False
    End With

     

    just chk it out once...