How to get a cell out of DataGridCellInfo

Jun 22, 2009 at 10:20 PM

Hi,

I am using new WPF DataGid control with C# and visual Studio 2008. My question is:  How can I obtain a collection of the selected cells?  I am using the following  code:

List<DataGridCellInfo> selected = this.GridRotation.SelectedCells.ToList();

I am getting a correct number of items in the list, however, each item has Column, Item and IsValid properties.
Where is the Cell property? How come CellInfo do not contain reference to a cell?

(My grid has the following attributes  SelectionMode="Extended" SelectionUnit="Cell")

Jun 23, 2009 at 1:22 AM
Edited Jun 23, 2009 at 1:22 AM

I figured out a solution. DataGridCellInfo gives us a column object and the data that corresponds to the row.

There seems to be no way to get the column index. So, when construct DataGrid (I do it dynamically) I assign an integer index a Tag to each columns, then I add column to the DataGrid:

 

            for (int ix = 0; ix < _columnsCount; i++)



            {
                DataGridTemplateColumn column = new DataGridTemplateColumn();

                column.SetValue(FrameworkElement.TagProperty, i);


                .... constructing column …..
 

                this.MyDataGrid.Columns.Add(column);
}

 

When reading selected cell collection, I can get a reference to a column and read my integer Tag back.

To obtain a row I can access the data object I bound the row to.

 

 

            foreach (DataGridCellInfo info in this.MyDataGrid.SelectedCells)

            {

 

                DataGridColumn column = info.Column;

                object tag = column.GetValue(FrameworkElement.TagProperty);

    int ix = int.Parse(tag.ToString());

                MyRowData myData = info.Item as MyRowData;

}