0

How can I bind these two properties? I tried SelectedValue and I get

System.NullReferenceException

I tried to add it with SelectedIndex, but it doesn't bind the right one.

I tried SelectedItem but I get

System.InvalidCastException

also tried with binding in xaml : SelectedValuePath="ClientId"

My Question what do I use to bind these properties?

Reservation rs = new Reservation();
{
      rs.ClientId = (int)ClientDataGrid.SelectedValue;
      rs.RoomId = (int)AvailableRoomDataGrid.SelectedValue;
}

in class Reservation :

public int? ClientId { get; set; }
[ForeignKey("ClientId")]
public Client Client { get; set; }
CDspace
  • 2,639
  • 18
  • 30
  • 36
BooZzken
  • 19
  • 3

1 Answers1

0

Here's a quick and dirty sample. On load, I initialize the grid and add a couple records to it. Then when button is clicked, the selected FName populates the TheName variable.

I'm guessing this(tailored to YOUR class) would take care of your invalid cast on SelectedItem. ((FamilyMember)dg_MainGrid.SelectedItem).FName

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dg_MainGrid" HorizontalAlignment="Left" Margin="249,114,0,0" VerticalAlignment="Top" RenderTransformOrigin="-14.748,-2.867"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="238,291,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Windows;
using System.ComponentModel;

namespace WpfApplication1
{

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        BindingList<FamilyMember> Family = new BindingList<FamilyMember>();

        public MainWindow()
        {
            InitializeComponent();
            InitializeDG();
            AddFamilyMembers();
        }

        private void InitializeDG()
        {
            dg_MainGrid.ItemsSource = Family;
            dg_MainGrid.AutoGenerateColumns = true;
        }

        private void AddFamilyMembers()
        {
            Family.Add(new FamilyMember { FName = "Name1", LName = "D", Notes = "34" });
            Family.Add(new FamilyMember { FName = "Name2", LName = "L", Notes = "36" });
            Family.Add(new FamilyMember { FName = "Name3", LName = "D", Notes = "7" });
            Family.Add(new FamilyMember { FName = "Name3", LName = "D", Notes = "5" });
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            string TheName = ((FamilyMember)dg_MainGrid.SelectedItem).FName;//get the selected FName
        }
    }

    public class FamilyMember
    {
        public string FName { get; set; }
        public string LName { get; set; }
        public string Notes { get; set; }
    }
}
blaze_125
  • 2,262
  • 1
  • 9
  • 19