none
Get Cell Value from Selected Row WPF Datagrid RRS feed

  • Question

  • I am using a dataset created from my SQL Server database.  I bound the dataset to a WPF Datagrid (from the WPF Tookkit).  My problem is I need to get information back out of the datagrid to use in other places (I have marked the datagrid itself as read-only).  So when a user selects a row on the datagrid, I need to be able to see the cell value for a particular column that was selected (an ID in this case), so I can reference it against the dataset I have populated that data with.  All the I have been able to extract is the row # of the datagrid when selected.

    Is there any way to get the cell values from a selected row and say, column 0 in a datagrid?

    Thanks for any help.
    Wednesday, October 21, 2009 7:52 PM

Answers

  • Hello bennej30

    Welcome to MSDN forums!

    As my understanding you need to revrieve  the column value of a specific row that user selected, right?

    You mentioned you could retrieve the row number of the selected row, would you be more specific how do you do this? Because this is important for us to obtain the entire row as a Data Item.

    Typically, we could subscribe DataGrid.SelectionChanged to get notified and obtain the Selected Row, please check below sample:

     dataGrid1.ItemsSource = ds.Tables[0].AsDataView ();
     dataGrid1.AutoGenerateColumns = true;
     dataGrid1.SelectionChanged += new SelectionChangedEventHandler(dataGrid1_SelectionChanged);
     void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                (e.AddedItems[0] as DataRowView).Row["ColID"];
               //Or 
              //(dataGrid1.SelectedItem as DataRowView).Row["ColID"];
            }
    Hope this helps you, looking forward to your comments.

    Thanks.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Tim Li Tuesday, October 27, 2009 9:17 AM
    Thursday, October 22, 2009 6:45 AM

All replies

  • Hello bennej30

    Welcome to MSDN forums!

    As my understanding you need to revrieve  the column value of a specific row that user selected, right?

    You mentioned you could retrieve the row number of the selected row, would you be more specific how do you do this? Because this is important for us to obtain the entire row as a Data Item.

    Typically, we could subscribe DataGrid.SelectionChanged to get notified and obtain the Selected Row, please check below sample:

     dataGrid1.ItemsSource = ds.Tables[0].AsDataView ();
     dataGrid1.AutoGenerateColumns = true;
     dataGrid1.SelectionChanged += new SelectionChangedEventHandler(dataGrid1_SelectionChanged);
     void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                (e.AddedItems[0] as DataRowView).Row["ColID"];
               //Or 
              //(dataGrid1.SelectedItem as DataRowView).Row["ColID"];
            }
    Hope this helps you, looking forward to your comments.

    Thanks.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Tim Li Tuesday, October 27, 2009 9:17 AM
    Thursday, October 22, 2009 6:45 AM
  • Hello,

     

    I am writing to check the status of the issue on your side. Could you please let me know if the suggestion works for you or not? If you have any questions or concerns, please feel free to let me know. I will be more than happy to be of assistance.

      

    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, October 26, 2009 7:17 AM
  • Hello bennej30

    Welcome to MSDN forums!

    As my understanding you need to revrieve  the column value of a specific row that user selected, right?

    You mentioned you could retrieve the row number of the selected row, would you be more specific how do you do this? Because this is important for us to obtain the entire row as a Data Item.

    Typically, we could subscribe DataGrid.SelectionChanged to get notified and obtain the Selected Row, please check below sample:

     dataGrid1.ItemsSource = ds.Tables[0].AsDataView ();
     dataGrid1.AutoGenerateColumns = true;
     dataGrid1.SelectionChanged += new SelectionChangedEventHandler(dataGrid1_SelectionChanged);
     void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                (e.AddedItems[0] as DataRowView).Row["ColID"];
               //Or 
              //(dataGrid1.SelectedItem as DataRowView).Row["ColID"];
            }
    Hope this helps you, looking forward to your comments.

    Thanks.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    This works like this, but what if the DataGrid is populated with a LINQ query from a LINQ to SQL class.  How would you modify the code to accommodate the types cause using DataRowView doesn't work.

    This is the query I used to populate the grid:

    public IQueryable GetAllUsers()
            {
                return from us in myDb.User
                       from usst in myDb.UserStatus
                       where us.UserStatusID == usst.UserStatusID
                       select new
                       {
                           us.UserID,
                           us.UserName,
                           usst.UserStatusDef,
                           us.Address,
                           us.MothersName
                       };
            }
    And this is how the query is bound to the DataGrid
    // Loading initial data into the DataGrids
    clsLoans objLoans = new clsLoans();
    
    dgUserSearch.ItemsSource = objLoans.GetAllUsers();
    The data shows up nice and I can bound values to other controls, but I'm having trouble getting the values and assigning them to variables.
    Pleas help!
    Wednesday, December 2, 2009 6:48 PM