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'.
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! enter image description here
XAML :
<DataGrid x:Name="INVO_LST_RASID_SUB"
EnableColumnVirtualization="True"
EnableRowVirtualization="True"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.VirtualizationMode="Recycling"
ScrollViewer.CanContentScroll="False"
KeyboardNavigation.TabNavigation="Contained"
ItemsSource="{Binding INVO_DATA_RASID_KHARID}"
Grid.Row="5"
Margin="5,10"
AutoGenerateColumns="False" FlowDirection="RightToLeft" SelectionUnit="FullRow" BorderThickness="1" >
<DataGrid.Columns>
<DataGridComboBoxColumn x:Name="ANBAR_COLUMN" MinWidth="80" Width="auto" Header=" انبار "
SelectedValueBinding="{Binding ANBAR,UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="NAMES" SelectedValuePath="CODE">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="IsEditable" Value="True"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
Code behind of my Window (C#):
//MainWindow:
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()
{
InitializeComponent();
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)
{
FillALLComboboxes();
var QRE_LST = dbms.Database.SqlQuery<INVO_LST_RASID_KHARID_CSHARP>($@"SELECT * FROM INVOICES").ToList();
INVO_DATA_RASID_KHARID?.Clear();
foreach (var item in QRE_LST)
INVO_DATA_RASID_KHARID.Add(item);
}
}
Model Class :
//Model:
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
{
get
{
return _anbar;
}
set
{
if (_anbar == value)
return;
_anbar = value;
OnPropertyChanged("ANBAR");
}
}
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).
I bypassed my problem and it worked! :
XAML :
<DataGridComboBoxColumn x:Name="ANBAR_COLUMN"
DisplayMemberPath="NAMES" SelectedValuePath="CODE"
SelectedValueBinding="{Binding ANBAR,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
MinWidth="80" Width="auto" Header=" انبار "
>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="IsEditable" Value="True"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="SelectedIndex" Value="{Binding DefaultValDisplay, Source={StaticResource BoundyAnbar}}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
C# :
using System.ComponentModel;
namespace WpfApp5.Boundy
{
public class CL_Boundy : INotifyPropertyChanged
{
private int _myPropertyVariable = 0;
public int DefaultValDisplay
{
get { return _myPropertyVariable; }
set
{
_myPropertyVariable = value;
OnPropertyChanged(nameof(DefaultValDisplay));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}