.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Get the value from the Selected Row in WPF DataGrid
 

Get the value from the Selected Row in WPF DataGrid

Hi,


How to getthe value from the Selected Row and a particular column in WPF DataGrid? I should get this value from any of the button clicked event.

Please help me on this
ksvimal
Then, unfortunately, you're not really using WPF. (opinion).

If you use DataView as ItemsSource the SelectedItem will be a DataRowView.

<Window x:Class="MSDNTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
    Width="640" Height="480">

    <StackPanel>
        <dg:DataGrid x:Name="dataGrid" SelectionMode="Single"/>
        <TextBox x:Name="property" Text="Age"/>
        <TextBlock x:Name="result"/>
    </StackPanel>
</Window>
using System;
using System.Data;
using System.Windows;

namespace MSDNTest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            var table = new DataTable();
            var colname = new DataColumn("Name", typeof(string));
            table.Columns.Add(colname);
            var colage = new DataColumn("Age", typeof(int));
            table.Columns.Add(colage);
            for (var i = 0; i < 10; i++)
            {
                var row = table.NewRow();
                row["Name"] = "Name " + (i + 1);
                row["Age"] = 20 + i;
                table.Rows.Add(row);
            }
            dataGrid.ItemsSource = table.DefaultView;
            dataGrid.SelectionChanged += (s, e) =>
                {
                    var item = dataGrid.SelectedItem as DataRowView;
                    if (null == item) return;
                    try
                    {
                        result.Text = item.Row[property.Text].ToString();
                    }
                    catch (Exception ex)
                    {
                        result.Text = ex.Message;
                    }
                };
        }
    }
}


Bigsby, Lisboa, Portugal - O que for, quando for, é que será o que é... http://bigsby.eu
  • Marked As Answer byksvimal Thursday, September 24, 2009 3:46 AM
  •  
Bigsby
In WPF rows actually represent objects in a list and columns actually represent a planefication of each object's properties.

It depends on what DataGrid.ItemsSource is. If it is implemented in real WPF it should be an Enumerable of some type of object. And you would get it by:

var value = ((ObjectClass)dataGridControl.SelectedItem).ObjectProperty;

Bigsby, Lisboa, Portugal - O que for, quando for, é que será o que é... http://bigsby.eu
  • Edited byBigsby Tuesday, September 22, 2009 4:42 PMCompletion
  •  
Bigsby
Hi,

Thanks for your feedback.

C# is my code behind.

Actually i have assigned DataTable to the Itemsource. Check the below code.
DataGrid.ItemsSource = AccountDataTable.DefaultView;

As i am new to .NET and WPF, your code is bitconfusing for me. I dont see the Column namein your code.
I assume that the dataGridControl.SelectedItem is the SelectedRow in WPF DataGrid.

Actually my intention is to getthe value from the Selected Row and a particular column (The column may change as per the requirement, ex 1st col or 2nd col)in WPF DataGrid.

Please help me
ksvimal
Then, unfortunately, you're not really using WPF. (opinion).

If you use DataView as ItemsSource the SelectedItem will be a DataRowView.

<Window x:Class="MSDNTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
    Width="640" Height="480">

    <StackPanel>
        <dg:DataGrid x:Name="dataGrid" SelectionMode="Single"/>
        <TextBox x:Name="property" Text="Age"/>
        <TextBlock x:Name="result"/>
    </StackPanel>
</Window>
using System;
using System.Data;
using System.Windows;

namespace MSDNTest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            var table = new DataTable();
            var colname = new DataColumn("Name", typeof(string));
            table.Columns.Add(colname);
            var colage = new DataColumn("Age", typeof(int));
            table.Columns.Add(colage);
            for (var i = 0; i < 10; i++)
            {
                var row = table.NewRow();
                row["Name"] = "Name " + (i + 1);
                row["Age"] = 20 + i;
                table.Rows.Add(row);
            }
            dataGrid.ItemsSource = table.DefaultView;
            dataGrid.SelectionChanged += (s, e) =>
                {
                    var item = dataGrid.SelectedItem as DataRowView;
                    if (null == item) return;
                    try
                    {
                        result.Text = item.Row[property.Text].ToString();
                    }
                    catch (Exception ex)
                    {
                        result.Text = ex.Message;
                    }
                };
        }
    }
}


Bigsby, Lisboa, Portugal - O que for, quando for, é que será o que é... http://bigsby.eu
  • Marked As Answer byksvimal Thursday, September 24, 2009 3:46 AM
  •  
Bigsby
Hi Bigsby,



You are right. I am new to .NET andWPF.

Thanks for your reply with an good example. It works fine.

I hope to get more input from you.

thanks once again.
ksvimal

You can use google to search for other answers

Custom Search

More Threads

• Is there any way to create <filename>.g.cs in WPF Project
• Named controls in XAML in a custom control
• TreeView grouping nodes
• Question about accessing members in WPF constructs
• Button Template/Properties
• ObjectDataProvider explanation?
• LastChildFill on DockPanel not guaranteed
• DependencyProperty of type PointCollection
• Show ToolTip when IsEnabled = false;
• WPF Styling routed events and some more animation problems