在DataGrid中保存和还原行选择

时间:2016-03-30 02:44:06

标签: c# wpf data-binding datagrid

问题相当简单。我的datagrid是从我的ItemSource(bindingList)填充的,它基本上是一个用字符串填充的对象列表。

在我的代码的这一部分中,我需要更新我的bindingList。 不幸的是,当它更新时,DataGrid中所做的所有用户行选择都会消失。

这对我想补救的用户造成不便。因此,当用户单击导致bindingList更新的按钮时,将保存选择以防用户想要进行进一步更改。

现在代码:

//Save DataGrid Row Selections
bindingList[1] = (new ItemClass() { columnnumber = colonum, xcoord = xpos, ycoord = ypos, description = descrip });
dataGrid.ItemSource = bindingList;
//Restore DataGrid Row Selections

编辑: 更广泛的要求范围:

private void Y_Incre_Button_Click(object sender, RoutedEventArgs e)
{
    if (dataGrid.SelectedItems.Count != 0)
    {
        string colonum;
        string xpos;
        string ypos;
        string descrip;

        for (int i = 0; i < bindingList.Count; i++)
        {
            int selectionIndex = dataGrid.SelectedIndex;

            if (selectionIndex > -1)
            {
                var curItem = bindingList[selectionIndex];
                int yNum = int.Parse(curItem.ycoord);
                int yNum2 = (yNum + 10);
                colonum = curItem.columnnumber;
                xpos = curItem.xcoord;
                ypos = yNum2.ToString();
                descrip = curItem.description;

                //Save DataGrid Row Selections
                bindingList[selectionIndex] = (new ItemClass() { columnnumber = colonum, xcoord = xpos, ycoord = ypos, description = descrip });
                //Restore DataGrid Row Selections
            }
        }
    }
    else
    {
        MessageBox.Show("No Rows Selected");
    }
}

1 个答案:

答案 0 :(得分:1)

为了使这个工作存储在替换之前所选项的行索引,然后在“替换”操作完成后重新选择这些记录。另外,请查看this示例。

private void Y_Incre_Button_Click(object sender, RoutedEventArgs e)
{
    if (dataGrid.SelectedItems.Count != 0)
    {
        // Save DataGrid Row Selections
        List<int> selectedRowIndexList = new List<int>();
        foreach (object item in dataGrid.SelectedItems)
        {
            selectedRowIndexList.Add(dataGrid.Items.IndexOf(item));
        }

        for (int i = 0; i < bindingList.Count; i++)
        {
            int selectionIndex = dataGrid.SelectedIndex;

            if (selectionIndex > -1)
            {
                ItemClass curItem = bindingList[selectionIndex];

                int yNum = int.Parse(curItem.ycoord);

                int yNum2 = yNum + 10;

                string colonum = curItem.columnnumber;
                string xpos = curItem.xcoord;
                string ypos = yNum2.ToString();
                string descrip = curItem.description;

                bindingList[selectionIndex] = new ItemClass { columnnumber = colonum, xcoord = xpos, ycoord = ypos, description = descrip };
            }
        }

        // Restore DataGrid Row Selections
        dataGrid.SelectedItems.Clear();
        foreach (int rowIndex in selectedRowIndexList)
        {
            if (rowIndex < 0 || rowIndex > dataGrid.Items.Count - 1)
                throw new ArgumentException(string.Format("{0} is an invalid row index.", rowIndex));

            object item = dataGrid.Items[rowIndex];
            dataGrid.SelectedItems.Add(item);
        }
    }
    else
    {
        MessageBox.Show("No Rows Selected");
    }
}