Basit
Basit

Reputation: 41

List to Array : Row, Column Indices of Selected Cells in Datagrid

I have list of selected cells, I want them to be converted to array so that I can save it. I am converting list in an array so that I can get indices of all selected cells (column wise in a row) so that I can retrieve later to fill same cells.

Problem is since cells can be selected in random way i.e I can select row 1 column 1,2,3,7,8,9 leaving column 4,5,6 unselected. As soon as I encounter unselected indices I get "The Index was out of range" error. Same error occur if I select some thing in middle of data grid i.e not selecting column at start like column 1,2,3 but selecting row 5 column 5,6,7.

May be some one can help in this or may be point to some other efficient way of doing same task.

List<DataGridViewCell> selectedCells = new List<DataGridViewCell>();

private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
    if (selectedCells.Contains(cell) ) selectedCells .Remove(cell);
    else selectedCells .Add(cell);
    cell.Style.BackColor = selectedCells .Contains(cell) ? Color.Pink : Color.White;
}

private void buttonSaveButton_Click(object sender, EventArgs e)
{
    string [,] selectedcellsArray = new string[dataGridView1.Rows.Count, dataGridView1.Columns.Count];
    int i = 0;
    int j = 0;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        while (j < dataGridView1.Columns.Count)
        {
            selectedcellsArray[i, j] = selectedCells[j].ColumnIndex.ToString();
            j++;
        }

        j = 0;
        i++; //next row
    } 
    //some more code
}

enter image description here

Upvotes: 0

Views: 1597

Answers (1)

juharr
juharr

Reputation: 32286

I'm not 100% sure but I think you'd be better off with a bool[,] instead.

private void buttonSaveButton_Click(object sender, EventArgs e)
{
    bool [,] cellIsSelected = new bool[dataGridView1.Rows.Count, dataGridView1.Columns.Count];
    foreach(var selectedCell in selectedCells)
    {
        cellIsSelected[selectedCell.RowIndex,selectedCell.ColumnIndex] = true;
    }

    for(int i=0; i<dataGridView1.Rows.Count; i++)
    {
        for(int j=0; j<dataGridView1.Columns.Count; j++)
        {
            //determine if the cell at postion i,j is selected
            if(cellIsSelected[i,j])
            {
                //It is selected.
            }
        }
    }
}

But if that works you'd be better off just keeping track of that instead of a list of the selected cells (unless you use them somewhere else). Also this would assume that dataGridView has a set number of rows and columns.

bool[,] cellIsSelected = new bool[dataGridView1.Rows.Count, dataGridView1.Columns.Count];

private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    cellIsSelected[e.RowIndex, e.ColumnIndex] = !cellIsSelected[e.RowIndex, e.ColumnIndex];
    cell.Style.BackColor = cellIsSelected[e.RowIndex, e.ColumnIndex] ? Color.Pink : Color.White;
}

Upvotes: 0

Related Questions