Для # 1 вы можете сделать следующее:
foreach (DataGridCellInfo cellInfo in dataGrid.SelectedCells)
{
DataGridCell gridCell = TryToFindGridCell(dataGrid, cellInfo);
if (gridCell != null && gridCell.Content is TextBlock)
Console.WriteLine(((TextBlock)gridCell.Content).Text);
}
ниже приведена реализация процедуры TryToFindGridCell:
static DataGridCell TryToFindGridCell(DataGrid grid, DataGridCellInfo cellInfo)
{
DataGridCell result = null;
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item);
if (row != null)
{
int columnIndex = grid.Columns.IndexOf(cellInfo.Column);
if (columnIndex > -1)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
result = presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
}
}
return result;
}
static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null) child = GetVisualChild<T>(v);
if (child != null) break;
}
return child;
}
# 2:
a.line "object obj= новый объект (); "не требуется;
b. Вы можете привести результат cmd.ExecuteScalar к DateTime.Вы также хотели бы проверить, что это не нуль или DBNull перед приведением;в противном случае вы можете получить исключение «Указанное приведение неверно».
надеюсь, это поможет, с уважением