Wojciech Szabowicz
Wojciech Szabowicz

Reputation: 4208

WPF C# Get Cell value from autogenerated Data Grid and Data Table

I have created simple mechanism for data table xaml would looks simply:

<DataGrid ItemsSource="{Binding CurrentsFlagValuesView}" AutoGenerateColumns="True" />

And MVVM code behind is based on data tables, and as well fairly simple:

private void GenerateDataView()
{
    CurrentsFlagValuesView = new DataTable();
    CurrentsFlagValuesView.Columns.Add("Bits");

    var bitLength = 0;

    foreach (CurrentsFlagAnalysis flag in CurrentsFlagValues)
    {
        CurrentsFlagValuesView.Columns.Add(flag.DailyCurrentsTimestampInterval.ToString("yyyy-MM-dd"));
        bitLength = flag.CurrentFlagsLength;
    }

    for (var bit = 0; bit < bitLength; bit++)
    {
        List<CurrentFlagEventEnum> flags = CurrentsFlagValues
            .Select(value => value.CurrentFlags.ElementAt(bit))
            .Select(value => value ? (CurrentFlagEventEnum)bit + 1 : CurrentFlagEventEnum.None)
            .ToList();

        var dataRowValues = new List<object> { bit };
        dataRowValues.AddRange(flags.Cast<object>());

        CurrentsFlagValuesView.Rows.Add(dataRowValues.ToArray());
    }
}

But I came upon a problem, or two I want to get data of the cell when I click the cell, like Column title, and value of the cell. I managed to do this without MVVM like:

void EditingDataGrid_CurrentCellChanged(object sender, EventArgs e)
{
    DataGridCell Cell = EditingDataGrid.GetCurrentDataGridCell();
    var Position = Cell.PointToScreen(new Point(0, 0));

    TextBlock text = (TextBlock)Cell.Content;

    MessageBox.Show("Value=" + text.Text, "Position" );
}

public static DataGridCell GetCurrentDataGridCell(this DataGrid dataGrid)
{
    DataGridCellInfo cellInfo = dataGrid.CurrentCell;
    if (cellInfo.IsValid == false)
    {
        return null;
    }
    var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
    if (cellContent == null)
    {
        return null;
    }
    return cellContent.Parent as DataGridCell;
}

But now I want to remodel that to that pattern, but I do not know how. Any Ideas how to bind command to that?

Upvotes: 0

Views: 666

Answers (2)

vanderblugen
vanderblugen

Reputation: 1

Returns the clicked on row in the grid, starting from 0

int currentRowIndex = dataGrid1.Items.IndexOf(dtGrid.CurrentItem);

Get value of currentRowIndex for column 0, or a specific cell value

object cellContent = dataGrid1.Columns[0].GetCellContent(dtGrid.Items[currentRowIndex]);
string valueOfColumn0currentRowIndex = (cellContent as TextBlock)?.Text;

Upvotes: 0

Raviraj Palvankar
Raviraj Palvankar

Reputation: 879

You can simply bind the current cell property in the view model and you will have the current cell with you all the time:

      <DataGrid AutoGenerateColumns="True" 
      SelectionUnit="Cell" 
      CanUserDeleteRows="True" 
      ItemsSource="{Binding Results}" 
      CurrentCell="{Binding CellInfo}"            
      SelectionMode="Single">

In the view model:

private DataGridCell cellInfo;
public DataGridCell CellInfo
{
    get { return cellInfo; }
}

Upvotes: 0

Related Questions