mercredi 17 juin 2020

Multiple Select Data grid based on Checkbox MVVM Wpf

So i want to be able to Insert into my database based on checkbox being selected in the database. I hope to be able to do this in MVVM style. I guess i might have to use attached properties to get selected items. So far what i have looked up online doesn't show how to insert to database. I will implement a button for inserting items into database.

My view model class

public class ContractorSkillsViewModel : BaseViewModel
{
    ObservableCollection<Contractor> _contractors;
    private Contractor _selectedContractor;
    ObservableCollection<ContractorSkill> _BITSkill;
    private ContractorSkill _contractorSkills;




    public ICommand SelectionChangeCommand { get; set; }

    public ObservableCollection<Contractor> Contractors
    {
        get { return _contractors; }
        set { _contractors = value; }
    }


    public Contractor SelectedContractor
    {
        get { return _selectedContractor; }
        set { _selectedContractor = value; }
    }




    public ContractorSkillsViewModel()
    {
        SelectionChangeCommand = new MyCustomCommand<SelectionChangedEventArgs>(OnSelectionChange);
        Contractors = new ObservableCollection<Contractor>(ContractorSQLHelper.GetAllContractors());
        _contractorSkills = new ContractorSkill();



    }







private void OnSelectionChange (SelectionChangedEventArgs obj)
    {

        BITSkill = new ObservableCollection<ContractorSkill>(ContractorSkillSQLHelper.GetAllBITSkills(_selectedContractor.ContractorID.ToString()));


    }

    public void InsertMethod()
    {


        if (ContractorSkill.IsSelected ==true)
        {
            ContractorSkillSQLHelper.InsertSkill(_selectedContractor.ContractorID, _contractorSkills.SkillID);
        }
        else
        {
            ContractorSkillSQLHelper.DeleteSkill(_selectedContractor.ContractorID, _contractorSkills.SkillID);
        }      





    }


    public ContractorSkill ContractorSkill
    {
        get
        {
            return _contractorSkills;
        }
        set
        {
            _contractorSkills = value;
        }
    }
    public ObservableCollection<ContractorSkill> BITSkill
    {
        get { return _BITSkill;

        }
        set { _BITSkill = value; }
    }

My Model class

public class ContractorSkill : BaseModel
{
    private string _contractorSkill;
    private int _skillID;
    private string _skillDescription;
    private bool _isSelected;
    private int _contractorID;
    public bool IsSelected
    {
        get { return _isSelected; }
        set
        { 

                _isSelected = true;
                OnPropertyChanged("IsSelected");


        }
    }


    public string SkillDescription
    {
        get
        {
            return _skillDescription;
        }
        set
        {
            _skillDescription = value;
            OnPropertyChanged("SkillDescription");
        }
    }

    public int SkillID
    {
        get { return _skillID; }
        set
        {
            _skillID = value;
            OnPropertyChanged("SkillID");
        }
    }

    public string ContractorSkills
    {
        get { return _contractorSkill; }
        set
        {   
            if (_contractorSkill == value)
            {
                _contractorSkill = value;
                OnPropertyChanged("ContractorSkill");

            }
        }
    }

    public int ContractorID
    {
        get { return _contractorID; }
        set
        {


                _contractorID = value;
                OnPropertyChanged("ContractorID");


        }
    }
}

}

My Sql class

 public static void InsertSkill(int c, int s)
    {


        ConnectDB();
        string sql = "insert into ContractorSkill(ContractorID, SkillID) values(@ContractorID,@SkillID)";
        SqlCommand addCommand = new SqlCommand(sql, bitConn);

        addCommand.Parameters.AddWithValue("@SkillID",s);
         addCommand.Parameters.AddWithValue("@ContractorID", c);



        addCommand.Connection.Open();
        addCommand.ExecuteNonQuery();
        addCommand.Connection.Close();

    }

Xaml

<Grid>
    <Grid Margin="20,0,20,0">

        <Grid>
            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="auto"/>
            </Grid.RowDefinitions>

            <StackPanel  local:MarginSetterProperty.Margin="10" local:MarginSetterProperty.LastItemMargin="10"  Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.ColumnSpan="2" >
                <TextBox x:Name="yo" Tag="Search" Text="{Binding ContractorSkill.SkillID}" ></TextBox>
                <TextBox x:Name="hey" Text="{Binding SelectedContractor.ContractorID}"/>
                <Button  Content="Search" Command="{Binding InsertCommand}"   />
            </StackPanel>


            <DataGrid x:Name="dgContractors" ItemsSource="{Binding Contractors}"  SelectedItem="{Binding SelectedContractor}" Grid.Row="1" Grid.ColumnSpan="2" IsReadOnly="True" HorizontalContentAlignment="Stretch" Width="auto"   local:SelectionChangedBehavior.SelectionChangedEvent="{Binding SelectionChangeCommand}" >
            </DataGrid>
            <DataGrid IsReadOnly="True"
                          CanUserResizeColumns="False"
                        CanUserAddRows="False"
                          AutoGenerateColumns="False"
                          x:Name="dgContractorSkills"
                          ItemsSource="{Binding BITSkill}"
                          SelectedItem="{Binding ContractorSkill}"
                          SelectionMode="Extended"
                          Grid.Column="1"
                          Grid.Row="4"
                          IsSynchronizedWithCurrentItem="False"

                          >

                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="SkillName">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding SkillDescription}">

                                </TextBlock>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Contractor Skill">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" Command="{Binding InsertCommand}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>


            </DataGrid>

        </Grid>
    </Grid>
</Grid>

Please see picture below so basically when i click on the checkbox and then click button all of those skills should be inserted into database. The sql query is from 2 tables the left side column is for all skills and right hand side coloum is for contractor that have those skills selected in top datagrid. I have got binding to database working just am only able to insert last row i check in database however that will be annoying for user. I prefer mvvm as all of project is like that.

[The picture of checkboxed grid1




Aucun commentaire:

Enregistrer un commentaire