C# 列表到数组:Datagrid中选定单元格的行、列索引

C# 列表到数组:Datagrid中选定单元格的行、列索引,c#,arrays,winforms,datagridview,C#,Arrays,Winforms,Datagridview,我有一个选定单元格的列表,我想把它们转换成数组,这样我就可以保存它了。 我正在转换数组中的列表,这样我就可以得到所有选定单元格的索引(一行中按列),这样我就可以稍后检索以填充相同的单元格 问题是,因为单元格可以随机选择,即我可以选择第1行第1列第1、2、3、7、8、9列,而不选择第4、5、6列。当我遇到未选择的索引时,我得到“索引超出范围”错误。 如果我选择数据网格中间的某个东西,也会出现相同的错误,即在开始时不选择列1、2、3,而是选择第5行第5、6、7列 可能有人能在这方面提供帮助,也可能有

我有一个选定单元格的列表,我想把它们转换成数组,这样我就可以保存它了。 我正在转换数组中的列表,这样我就可以得到所有选定单元格的索引(一行中按列),这样我就可以稍后检索以填充相同的单元格

问题是,因为单元格可以随机选择,即我可以选择第1行第1列第1、2、3、7、8、9列,而不选择第4、5、6列。当我遇到未选择的索引时,我得到“索引超出范围”错误。 如果我选择数据网格中间的某个东西,也会出现相同的错误,即在开始时不选择列1、2、3,而是选择第5行第5、6、7列

可能有人能在这方面提供帮助,也可能有人指出其他有效的方法来完成同样的任务

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
}
List selectedCells=new List();
私有void dataGridView1_CellMouseDown(对象发送方,DataGridViewCellMouseEventArgs e)
{
DataGridViewCell单元格=dataGridView1[e.ColumnIndex,e.RowIndex];
如果(selectedCells.Contains(cell))selectedCells.Remove(cell);
else selectedCells.添加(单元格);
cell.Style.BackColor=所选单元格。包含(单元格)?颜色。粉红色:颜色。白色;
}
私有无效按钮保存按钮\单击(对象发送者,事件参数e)
{
string[,]selectedcellsArray=新字符串[dataGridView1.Rows.Count,dataGridView1.Columns.Count];
int i=0;
int j=0;
foreach(dataGridView1.Rows中的DataGridViewRow行)
{
而(j

我不是100%肯定,但我认为你最好还是用
bool[,]
来代替

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

为什么要使用
selectedCells
,一个空列表,而不是
?@juharr selectedCells不是空列表,我添加了代码供您考虑这里的问题是
j
基于列计数,但您使用它来索引
selectedCells
。我真的不清楚您想在
selectedcellsArray
中输入什么。您计划如何使用
selectedcellsArray
?或者您可以给出一个示例值来说明
selectedCells
的值,以及在
selectedcellsArray
中应该得到什么数组中的选定单元格,以便我可以保存它们,然后我可以检索索引并精确填充颜色/突出显示保存前已填充颜色/选定的单元格。在这种情况下,
selectedCellsRay
不应该是一个
bool[,]
来指示选择了哪些单元格?也许您可以展示一下您计划如何使用
selectedcellsArray
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;
}