0

I want to be able to choose either "true" or "false"(boolean) from a ComboBox that is within a wpf Datagrid and be able to save that choice to my database.

I want to be able to indicate inside of a column if the row is "Active" or not through a boolean variable saved to my database as a bit(1 = true; 0 = false).

Here is my create table statement: Create Table Statement(AccountType)

I populate the datagrid using an ObservableCollection as follows:

protected override void Get()
{
    using (var dbContext = new IEMASEntitiesDataContext())
    {
        var accountType = from s in dbContext.AccountTypes select s;
        var observable = new  ObservableCollection<AccountType>(accountType);

        Collection = observable;
    }
}

My XAML code is as follows:

<DockPanel DataContext="{StaticResource ResourceKey=AccountTypeViewModel}" LastChildFill="True">
    <ToolBar DockPanel.Dock="Top">
        <Button Content="Display"  Command="{Binding GetCommand}" Width="78"/>
        <Button Content="Save" Command="{Binding SaveCommand}" Width="78"/>
    </ToolBar>

    <Grid>
        <GroupBox x:Name="AccountTypeGroupBox">
            <DataGrid x:Name="DataGridAccountType" ItemsSource="{Binding Collection}" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn  Header="AccountType" Width="150" Binding="{Binding Path=AccountTypeName, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
                    <DataGridComboBoxColumn  Header="Active" Width="100" SelectedValueBinding="{Binding StatusList, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="AccountTypeId" DisplayMemberPath="Active"/>                   
                    <DataGridTemplateColumn Width="50" Header="Delete">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button x:Name="btnDelete" Content="Delete" Command="{Binding DeleteCommand}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </GroupBox>          
    </Grid>
</DockPanel>

It doesn't work. Nothing displays within the comboboxes, and I don't know how to save the selected item to the SQL Server database when it does display. I'd appreciate some help, please. I am using LINQ TO SQL. I am a newbie :-(.

krlzlx
  • 5,752
  • 14
  • 47
  • 55
Newbie101
  • 13
  • 4
  • Is your AccountType implements the INotifyPropertyChanged on binded propeties? Is there data inside the DataGrid ItemsSource collection? – Ilan Oct 21 '15 at 10:20
  • Hi. Yes, I implemented the INotifyPropertyChanged inside of my CommandBase class. I do have dummy data, which displays in all of the other columns, except for the ComboBox. My Commandbase is an Interface class is and is used by all of my ViewModels. Collection Itemsource is an Observable collection. – Newbie101 Oct 21 '15 at 11:20
  • there is no Combo ItemsSource binding there. Do you set this in code behind? – Ilan Oct 21 '15 at 11:27
  • I've opted to go the route of ListView. Let me show you what I mean: I have a GridViewColumn.Cellplate. And its DataTemplate's child object is the ComboBox. I want it to be populated with two items: True and False. I want to be able to choose from those two options: the ItemSource will bind to the Active column of the table in the database. Please show me an example of how you would do that. I want to be able to retrieve the choice and save it. I will modify the code as needed, if you can please show demonstrate how. – Newbie101 Oct 21 '15 at 15:50

1 Answers1

1

As I can understand the problem is how to bind some XAML resource as a combo ItemsSource and in addtion how to binnd the selected value of a combo to the model behind the DataGrid row. 1. List item:

<Window x:Class="SoDataGridProjectsHelpAttempt.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:soDataGridProjectsHelpAttempt="clr-namespace:SoDataGridProjectsHelpAttempt"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <x:Array x:Key="CountriesArray" Type="soDataGridProjectsHelpAttempt:Country">
        <soDataGridProjectsHelpAttempt:Country CountryName="Germany" CountryPop="150k"/>
        <soDataGridProjectsHelpAttempt:Country CountryName="France" CountryPop="125k"/>
        <soDataGridProjectsHelpAttempt:Country CountryName="Belarus" CountryPop="165k"/>
    </x:Array>
    <x:Array x:Key="StatusArray" Type="soDataGridProjectsHelpAttempt:ActivityStatus">
        <soDataGridProjectsHelpAttempt:ActivityStatus VerbalStatus="Yes" BoolStatus="True"/>
        <soDataGridProjectsHelpAttempt:ActivityStatus VerbalStatus="No" BoolStatus="False"/>
    </x:Array>
</Window.Resources>
<Window.DataContext>
    <soDataGridProjectsHelpAttempt:DataGridMainDataContext/>
</Window.DataContext>
<Grid>
    <DataGrid ItemsSource="{Binding Collection}" AutoGenerateColumns="False" CanUserAddRows="True">
        <DataGrid.Columns>
            <DataGridTextColumn     Width="Auto" Binding="{Binding UName}"/>
            <DataGridComboBoxColumn Header="Country" DisplayMemberPath="CountryName"
                                    ItemsSource="{StaticResource CountriesArray}" Width="Auto"
                                    SelectedItemBinding="{Binding CountryData}"/>
            <!--<DataGridComboBoxColumn Header="ActivityStatus" Width="Auto" ItemsSource="{StaticResource StatusArray}" 
                                    SelectedValueBinding="{Binding IsActive}" SelectedValuePath="BoolStatus" DisplayMemberPath="VerbalStatus"/>-->
            <DataGridComboBoxColumn Header="ActivityStatus" SelectedItemBinding="{Binding IsActive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                <DataGridComboBoxColumn.ItemsSource>
                    <x:Array Type="system:Boolean">
                        <system:Boolean>True</system:Boolean>
                        <system:Boolean>False</system:Boolean>
                    </x:Array>
                </DataGridComboBoxColumn.ItemsSource>
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

  1. DataGrid viewmodel:

    public class DataGridMainDataContext
    

    { public DataGridMainDataContext() { Collection = new ObservableCollection(new List { new UserData { UName = "Greg", IsActive = false, }, new UserData { UName = "Joe", IsActive = false, }, new UserData { UName = "Iv", IsActive = false, } });

    }
    
    public ObservableCollection<UserData> Collection { get; set; }
    

    }

  2. Models: public class UserData : BaseObservableObject { private string _uName; private object _countryData; private bool _isActive;

    public bool IsActive
    {
        get { return _isActive; }
        set
        {
            _isActive = value;
            OnPropertyChanged();
        }
    }
    
    public string UName
    {
        get { return _uName; }
        set
        {
            _uName = value;
            OnPropertyChanged();
        }
    }
    
    public object CountryData
    {
        get { return _countryData; }
        set
        {
            _countryData = value;
            OnPropertyChanged();
        }
    }
    

    }

    public class ActivityStatus:BaseObservableObject { private bool _boolStatus; private string _verbalStatus;

    public bool BoolStatus
    {
        get { return _boolStatus; }
        set
        {
            _boolStatus = value;
            OnPropertyChanged();
        }
    }
    
    public string VerbalStatus
    {
        get { return _verbalStatus; }
        set
        {
            _verbalStatus = value;
            OnPropertyChanged();
        }
    }
    

    }

    public class Country : BaseObservableObject { private string _countryName; private string _countryPop;

    public string CountryName
    {
        get { return _countryName; }
        set
        {
            _countryName = value;
            OnPropertyChanged();
        }
    }
    
    public string CountryPop
    {
        get { return _countryPop; }
        set
        {
            _countryPop = value;
            OnPropertyChanged();
        }
    }
    
    public Country() { }
    public Country(string n, string d)
    {
        this.CountryName = n;
        this.CountryPop = d;
    }
    

    } Hope it will help you.

regards,

Ilan
  • 2,762
  • 1
  • 13
  • 24
  • @Newbie101 you're welcome.I'm glad to help, feel free to mark it as answered if helped. – Ilan Oct 22 '15 at 07:08
  • I will. I just have one more question. Is there an alternative to using SelectedValueBinding for the property of the DataGridComboBoxColumn? I ask because I'm using VS2013 and it is not recognised or it may be inaccessible, and if that's the case, what is the namespace for it? We will have solved this problem, if I can just resolve this. – Newbie101 Oct 22 '15 at 07:58
  • @Newbie101 very strange. I'm working on VS2013 to, and I don't have any prolem with that. What vhersion of .Net you have? – Ilan Oct 22 '15 at 08:10
  • @Newbie101 try this, it must help. there is no using in SelectedValueBinding. – Ilan Oct 22 '15 at 09:47
  • The problem is now officially resolved. You were the only person here, helping me. The rest of the world viewed it, and clicked passed it. Either they really didn't have the answer or just didn't want to go through the effort it would take to explain the problem to me. I can't express my appreciation enough. I will tag this as answered. – Newbie101 Oct 22 '15 at 16:11
  • @Newbie101 you're welcome and thank you, each case is a learning for me to. – Ilan Oct 22 '15 at 16:22
  • Just I didn't get it how the "Activity Status" column gonna performe the mapping from '"true or false" to the verbose values in the "status array". The only link I can see is the class name "ActivityStatus" is the same as the column header. – MohOuss May 23 '18 at 19:59