How to get the data value of the Datagrid cell in WPF?

advertisements

I am very much new to WPF and I want to make an data entry form with Datagrid control. I am stuck with , how to get cell data value to store them into database?

Like in windows, we used to get it through gridview1.Rows[0].Cells[0].value.

how to get this in WPF?


You Can obtain using the following code.

public void DataGridCellValue(object param)
    {
        string clipboard = string.Empty;
        if (param != null)
        {

            if (param is DataGridCell)
            {
                DataGridCell cell = (DataGridCell)param;
                dynamic column = cell.Column as DataGridTextColumn;

                if (cell.Column is DataGridTextColumn)
                    column = cell.Column as DataGridTextColumn;

                if (cell.Content is TextBlock)
                {
                    TextBlock tBlock = cell.Content as TextBlock;
                    clipboard = string.IsNullOrEmpty(tBlock.Text) ? string.Empty : tBlock.Text.Trim();
                }
            }
            else if (param.GetType().IsValueType)
            {
                clipboard = param.ToString();
            }
        }
    }

    #endregion
}

public ICommand DataGridCellValue
    {
        get
        {
            return new DelegatingCommand((object param) =>
            {
                new Action(() =>
                {
                    DataGridCopyToClipBoard(param);
                }).Invoke();
            });
        }
    }

Use the Binding as like this in your WPF XAML

"{Binding CurrentCell, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}"

The Command and CommandParameter's are

Command="{Binding DataGridCellValue}"
CommandParameter="{Binding CurrentCell, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}"