2
votes

I have a C# WPF DataGrid to list information about documents (e.g. invoices). The user is able to select one row in the Datagrid. After double-clicking the row I need to access a specific cell value (docId).

Unfortunately I don't know how to access the correct datarow behind the selected datagrid row.

I can access following values from my grid:

dataGrid_docs.selectedValue
dataGrid_docs.selectedItem

...

selected Value is not the answer to my problem because the user ist clicking any row, not always the correct column.

        // Create a new DataTable.    
        DataTable custTable = new DataTable("Customers");


        // Create docId column 
            dtColumn = new DataColumn();
            dtColumn.DataType = typeof(string);
            dtColumn.ColumnName = "docId";
            custTable.Columns.Add(dtColumn);

        // Create createDate column 
            dtColumn = new DataColumn();
            dtColumn.DataType = typeof(string);
            dtColumn.ColumnName = "createDate";
            custTable.Columns.Add(dtColumn);


        dtSet = new DataSet();
        dtSet.Tables.Add(custTable);



        foreach (var doc in docs)
        {
            myDataRow = custTable.NewRow();
            foreach (var attribute in ecoDmsAttributes)
            {                    
                myDataRow["docId"] = "123"
                myDataRow["createDate"] = "2019-08-27"
            }
            custTable.Rows.Add(myDataRow);
        }


        dataGrid_docs.ItemsSource = custTable.DefaultView;




    private void DataGrid_docs_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        DataRowView docid = dataGrid_docs.SelectedItem;

        txt_Debug.Text = "Clicked on docId: " + docid.ToString();
    }
4

4 Answers

1
votes

you can retrive values from DataRowView by column name, e.g. dataRowView["docId"]

private void DataGrid_docs_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    DataRowView dataRowView = (DataRowView)dataGrid_docs.SelectedItem;

    txt_Debug.Text = "Clicked on docId: " + dataRowView["docId"];
}
0
votes

You might try the DataGridView's CellContentClick event. There, you can access e.RowIndex and e.ColumnIndex to get the value of the cell clicked. You may have to combine that somehow with another method though to see if it was a double click.

0
votes

You seem to be most the way there:

DataRowView docid = dataGrid_docs.SelectedItem;

A DataRowView has a .Row property that will retrieve the underlying datarow that the view is passing through:

DataRow ro = (dataGrid_docs.SelectedItem as DataRowView)?.Row;
var docid = ro["docId"]; //returns object. Use cast (string) or .ToString()

There's probably little practical difference between this and ash's answer, though I interpreted your requirement as wanting the DataRow itself, rather than the values in the row as exposed by the view

-1
votes

Sometimes you just should try harder before asking on stackoverflow. It was pretty simple calling:

            DataRowView docid = dataGrid_docs.SelectedItem as DataRowView;

            txt_Debug.Text = "Clicked on: " + docid.Row["DocID"];