Click here to Skip to main content
15,918,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Project subject: I had an MS Access 2003 adp VBA project that would connect to SQL Server Database, Now I'm converting that project to C# WPF, and I'm going to save data directly into SQL Server database from DataGrid in C# WPF

Detail: I have DataGrid that ItemsSource binds to an 'ObservableCollection' named: 'INVO_DATA_RASID_KHARID' and also 'DataGridComboBoxColumn' named: 'ANBAR_COLUMN' the 'ItemSource' of this 'DataGridComboBoxColumn' is filled through SQL Server.

I want this 'DataGridComboBoxColumn' to be displayed with a default value so that the user knows that there is no need to manually select it from the 'DataGridComboBoxColumn' list.

I set this default value through a static variable in code behind in this method named: 'FillALLComboboxes'.

enter image description here

The Problem: Considering that 'DataGridComboBoxColumn' in the DataGrid, the 'ANBAR_COL' 'DataGridComboBoxColumn' has a default value, the user does not need to select that item manually from the 'DataGridComboBoxColumn' list, but as long as they double-click on the 'ANBAR_COL' combobox cell, the default value It will appear! The point is that the default value set for 'DataGridComboBoxColumn' will not be displayed until the user enters the cell!

What I have tried:


        <DataGrid   x:Name="INVO_LST_RASID_SUB"
                        ItemsSource="{Binding INVO_DATA_RASID_KHARID}"
                        AutoGenerateColumns="False"  FlowDirection="RightToLeft"  SelectionUnit="FullRow"   BorderThickness="1" >

                <DataGridComboBoxColumn x:Name="ANBAR_COLUMN" MinWidth="80" Width="auto" Header=" انبار " 
                                        SelectedValueBinding="{Binding ANBAR,UpdateSourceTrigger=PropertyChanged}"
                                        DisplayMemberPath="NAMES" SelectedValuePath="CODE">
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="IsEditable" Value="True"/>


Code behind of my Window (C#):

    public partial class HEAD_LST_RASID : Window
        public ObservableCollection<INVO_LST_RASID_KHARID_CSHARP> INVO_DATA_RASID_KHARID { get; set; } = new ObservableCollection<INVO_LST_RASID_KHARID_CSHARP>();

        public HEAD_LST_RASID()
            this.DataContext = this;
        public void FillALLComboboxes()
            string RowSource_ANBAR = "SELECT    CODE, NAMES FROM  dbo.TCOD_ANBAR";
            //Search for the default value if it exists
            var rst = dbms.Database.SqlQuery<int?>("SELECT     ANBCO FROM dbo.OPANBACCESS WHERE     (USERCO = " + Baseknow.USERCOD + " ) ORDER BY dbo.OPANBACCESS.RDF").ToList();
            if (rst.Count > 0)
                Baseknow.anbardef = (int)rst.FirstOrDefault();
            var ARST = dbms.Database.SqlQuery<Custom_TCODANBAR>(RowSource_ANBAR).ToList();
            ANBAR_COLUMN.ItemsSource = ARST;
        private void Window_Loaded(object sender, RoutedEventArgs e)
              var QRE_LST = dbms.Database.SqlQuery<INVO_LST_RASID_KHARID_CSHARP>($@"SELECT * FROM INVOICES").ToList();
                foreach (var item in QRE_LST)
Model Class :

    public class CTABLES
        public class INVO_LST_RASID_KHARID_CSHARP : INotifyPropertyChanged, ICloneable
            public object Clone()
                return this.MemberwiseClone();
            public INVO_LST_RASID_KHARID_CSHARP()
                ANBAR = Baseknow.anbardef;

            private int? _anbar;
            public int? ANBAR
                    return _anbar;
                    if (_anbar == value)
                    _anbar = value;

            public event PropertyChangedEventHandler PropertyChanged;
            protected virtual void OnPropertyChanged(string propertyName = "")
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

What I want is to show that a default value has been selected for this combobox inside the 'DataGrid' when the 'DataGrid' data is loaded (without the need to double-click and go to CellEditing mode).
Updated 2-May-23 11:28am
[no name] 2-May-23 16:45pm    
By default, most "selector" controls default to .SelectedIndex == -1 (empty set or nothing selected); so, you should set the index if it makes a difference (after loading).
mojtabahakimian 2-May-23 17:05pm    
How can I set SelectedIndex for the 'DataGridComboBoxColumn' dynamically ? (Binding)

1 solution

When I hit an issue, sometimes I like to remove the noise and break out a small project that focuses on the main issue. I have done just that for you.

Start a new WPF project with the name WpfDataGridComboBox and drop this code in and you can see how it works.

1. MainWindow.xaml
<Window x:Class="WpfDataGridComboBox.MainWindow"
        Title="MainWindow" Height="450" Width="800"

    <Grid DataContext="{Binding ElementName=Window1}">
            <CollectionViewSource x:Key="ChoicesCVS"
                                  Source="{Binding Choices}" />

        <DataGrid AutoGenerateColumns="False"
                  ItemsSource="{Binding Items}">
                <DataGridTextColumn Header="Name"
                                    Binding="{Binding Name}" />
                <DataGridTextColumn Header="Age"
                                    Binding="{Binding Age}" />
                    Header="Mode" Width="100" 
                    SelectedItemBinding="{Binding Choice}"
                    ItemsSource="{Binding Source={StaticResource ChoicesCVS}}">


2. the code behind file MainWindow.xaml.cs
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;

namespace WpfDataGridComboBox
    public partial class MainWindow : Window
        public MainWindow()

        public List<string> Choices { get; set; } = new()
            "Choice 1",
            "Choice 2",
            "Choice 3"

        public ObservableCollection<Person> Items { get; set; } = new()
            new() { Name = "Freddie", Age = 21, Choice = "Choice 1" },
            new() { Name = "Milly", Age = 18, Choice = "Choice 2" },
            new() { Name = "Caddie", Age = 23, Choice = "Choice 3" },

    public class Person
        public string Name { get; set; }
        public int Age { get; set; }
        public string Choice { get; set; }

Now run the project and it will perform as expected. Now you can take this and apply to your own code.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900