1
votes

I am trying to modify the way in which selections are made for my datagridview so that it selects in a similar way to text selection.

Currently when a series of cells are selected in multiple rows only the cells selected are highlighted. Standard Selection

However what i'd like it to do is when the second row is selected it also selects all cells to the right of the first selected cell, and all cells left of the second selected cell, exactly the same as highlighting text in a browser.

Banded Selection

Is there some property or mode that I can set to make the datagridview selection behave like this?

2
Why do you need this functionality? What's the intended behavior in the end? Perhaps, you are abusing DataGridView, and could use a custom control instead.Neolisk
The datagridview shows Months (downward) and days (across) from which users will be able to select date ranges. Naturally if the user spans more than one row the range should include as described above.Matt Skeldon

2 Answers

0
votes

You can use a built-in MonthCalendar control:

enter image description here

The Windows Forms MonthCalendar control can display up to twelve months at a time. By default, the control displays only one month, but you can specify how many months are displayed and how they are arranged within the control. When you change the calendar dimensions, the control is resized; so be sure there is enough room on the form for the new dimensions.

0
votes

You can try capturing the mouse down, move and up events and handling the selection yourself. This works for dragging from top to bottom, additional logic needs to be added to drag the other direction, but that should doable for you.

    { 
        dataGridView1.CellMouseDown += dataGridView1_CellMouseDown;
        dataGridView1.CellMouseMove += dataGridView1_CellMouseMove;
        dataGridView1.CellMouseUp += dataGridView1_CellMouseUp;
        dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
    }

    int startRow;
    int startColumn;
    bool beginSelection;

    void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
    {
        beginSelection = false;
    }

    void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (!beginSelection) 
            return;

        dataGridView1.ClearSelection();

        int curRow = e.RowIndex;
        int curCol = e.ColumnIndex;

        for (int r = startRow; r <= curRow; r++)
        {
            int maxC = dataGridView1.ColumnCount-1;
            int minC = 0;
            if (r == curRow)
                maxC = curCol;
            if (r == startRow)
                minC = startColumn;
            for (int c = minC; c <= maxC; c++)
            {
                dataGridView1[c,r].Selected = true;
            }
        }
    }

    void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
        beginSelection = true;
        startRow = e.RowIndex;
        startColumn = e.ColumnIndex;
    }