Hi guys,

I would like to know if there is any way that I could disable a datagridview row and make it unselectable. I need to do this because I have clients in the grid of which some are active while others have been disabled but i am still displaying all. Only the active clients should be selectable.

Thanks
Azo

>Disable a datagridview row from being selected.

Set value false to Enabled property of datagrid.

Setting the Enable property of the grid will prevent the user from moving to any subsquent rows.

You may have to manually prevent them from selecting that row by trapping the row in the Select event. Then setting the CurrentCell to the next selectable row. This could get messy because they could use the keyboard or the mouse. You may have to monitor the last Up/Down key used for the grid to know which direction they were going.

Another option maybe to just set all of the cells for the invalid row(s) to ReadOnly before you give them control of the grid. If you do that, even if they select the row, they can not do anything with it.

In any case, you should use the CellFormating event handler to indicate this is not a selectable cell. You can even set the colors to give them the illusion that it is not selectable:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.RowIndex == 3) // some condition meaning not editable
    {
e.CellStyle.BackColor = Color.White;
e.CellStyle.ForeColor = Color.Black;
e.CellStyle.SelectionBackColor = Color.White;
e.CellStyle.SelectionForeColor = Color.Black;
     }
}

If you want to go the messy way (and this is just a start), Give youself a key var to track with, format the cell to make them think they can not select it, and monitor mouse and key strokes. Finally move the selection when they attempt to select a row you do not want them to.

private Keys _lastKey = Keys.Up; // last key used in the grid


 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex == 3) // some condition meaning not editable
            {
                e.CellStyle.BackColor = Color.White;
                e.CellStyle.ForeColor = Color.Black;
                e.CellStyle.SelectionBackColor = Color.White;
                e.CellStyle.SelectionForeColor = Color.Black;
            }
        }

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Up || e.KeyData == Keys.Down)
                _lastKey = e.KeyData;
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                _lastKey = Keys.Down;
        }
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            int rowIndex = dataGridView1.CurrentRow.Index;
            if (rowIndex == 3) // some condition meaning not editable
            {
                if (_lastKey == Keys.Down)
                {
                    if (rowIndex < dataGridView1.Rows.Count - 1)
                        dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex + 1].Cells[0];
                }
                else if (_lastKey == Keys.Up && rowIndex > 0)
                {
                    dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex - 1].Cells[0];
                }
            }
        }

Hope this gets you on your way.
Jerry

Thanks Jerry for your suggestions you've really help me get something to work on here. I have since decided to take the option of just making the Disabled client rows to read-only and just coloring them. The second option I also think could work but let me just try the first option.

Thanks a lot.
Azo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.