locked
Getting the value of selected item in DataGrid RRS feed

  • Question

  • Hi,

    I have a SketchFlow prototype screen that I'm working on. The screen contains a DataGrid control which is hard-coded with three sample values:

    Assembly
    Part
    Bolt

    I also have a TextBox control which I want to populate with the value of the selected item in the DataGrid. So, if I click on "Assembly" in the DataGrid the TextBox should show "Assembly."

    I have this in the code-behind of the DataGrid control, on the SelectionChanged event:

    theTextBox.Text=theDataGrid.SelectedItem.ToString();

    However, all this does not return the value I'm expecting.

    Every resource I find on the web about how to get the selected value of a datagrid tells me this is how it should be done, but it does not work. I'm wondering if it is a SketchFlow problem, or if I'm missing some other code.

    Thanks in advance.
    Thursday, March 18, 2010 4:35 PM

All replies

  • I'm not quite sure what the problem is, I replicated what I think you are doing and it works for me.  Could you post the xaml and code you are using?  Here is what I used that worked (in SL).  Please also specify if you are using WPF or SL.  I also wasn't sure if you were using a textBlock or textBox so I included both.

    public partial class Screen_1 : UserControl
    	{
    		public Screen_1()
    		{
    			// Required to initialize variables
    			InitializeComponent();
    			
    			this.Loaded += new System.Windows.RoutedEventHandler(Screen_1_Loaded);
    			
    		}
    
    		private void Screen_1_Loaded(object sender, System.Windows.RoutedEventArgs e)
    		{
    			this.dg.ItemsSource = new List<string> { "Assembly", "Part" ,"Bolt" };
    				
    		}
    
    		private void SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    		{
    			this.textBlock.Text = this.dg.SelectedItem.ToString();
    			this.textBox.Text = this.dg.SelectedItem.ToString();
    		}
    	}
    <Grid x:Name="LayoutRoot" Background="White">
    		<data:DataGrid x:Name="dg" Margin="96,78,112,179" SelectionChanged="SelectionChanged"/>
    		<TextBox x:Name="textBox" Height="106" HorizontalAlignment="Left" Margin="77,0,0,33" VerticalAlignment="Bottom" Width="216" Text="TextBox" TextWrapping="Wrap"/>
    		<TextBlock x:Name="textBlock" Height="97" HorizontalAlignment="Right" Margin="0,0,72,42" VerticalAlignment="Bottom" Width="225" Text="TextBlock" TextWrapping="Wrap"/>
    	</Grid>
    Thursday, March 18, 2010 6:01 PM
    Moderator
  • Hi Chuck,

    The data is hardcoded, then it works fine when selected.

    But when I populate the data in the datagrid using XML sample data then the text box and text block shows Expression.Blend.SampleData.empsSampleDataSource.Employee instead of the data in XML.. How should I go about this?

     

     

    Wednesday, April 21, 2010 12:28 PM
  • What you are seeing is the default ToString() method on the generated data classes.  You need to do 1 of 2 things, if you are working in the code behind, cast the object to the right type and use the property you want to show as the text data, if you are working in xaml doing databinding, change the binding path to match the property you want to display.
    Wednesday, April 21, 2010 1:25 PM
    Moderator