1
votes

So I have searched everywhere to try and find an answer to my question but everyone seems to be OK with the way it selects cells by default (in a box).

I am working on a hex editor and currently have SelectionMode set to default RowHeaderSelect and MultiSelect is set to true. My issue is that when you click on a cell and drag select, it selects cells in a box shape (like excel). This is not how I need it to select cells.

I currently got around this by handling the following events, CellMouseDown, CellMouseEnter, CellMouseUp, & MouseUp. This gives me the selection I am looking for but the default box selection style is still there during drag selection.

This code sample is happening on CellMouseUp and is providing the selection style I am looking for. The first photo is what my selection looks like after the code below is ran (CellMouseUp). The second picture is what happens as your dragging (code below is ran on CellMouseEnter also to custom select while dragging) but its producing this visual bug of unselecting an entire column because its out of the default selection box style (excel style).

Edit: To clarify selection works after mouse up and mouse down occur ( dgvMainBinary.ClearSelection() then code below ) This is a visual bug that is happening during dragging. Only thing I can think of is the default box selection mode running after my CellMouseEnter event.

Any help would be appreciated, Thanks.

        for (int row = startY; row <= endY; row++)
        {
            int startColumn = 0;
            int endColumn = 15;
            if (row == startY)
            {
                startColumn = startX;
                if (startY == endY)
                    endColumn = endX;
            }
            else if (row == endY)
            {
                endColumn = (endX < 16)?endX:15;
            }
            for (int column = startColumn; column <= endColumn; column++)
            {
                dgvMainBinary.Rows[row].Cells[column].Selected = true;
            }
        }

image1

image2

1

1 Answers

0
votes

One way is to clear the selected cells in the MouseUp event handler and do your selection logic after clearing the selection done by DataGridView default logic.

void dataGridView1_MouseUp(object sender, MouseEventArgs e)
{            
    dataGridView1.ClearSelection();           
    // do your selection logic here
}