项目主题:我有一个MS Access 2003 adp VBA项目,它可以连接到SQL Server数据库,现在我正在将该项目转换为C# WPF,并且我要将数据直接从C# WPF中的DataGrid保存到SQL Server数据库中
详情:我有一个DataGrid,ItemsSource绑定到一个名为'ObservableCollection'的:“INVO_DATA_RASID_KHARID”和“DataGridComboBoxColumn”命名为:'ANBAR_COLUMN'此'DataGridComboBoxColumn'的'ItemSource'通过SQL Server填充。
我希望此“DataGridComboBoxColumn”以默认值显示,以便用户知道不需要从“DataGridComboBoxColumn”列表中手动选择它。
我通过这个方法中的一个静态变量设置了这个默认值,这个变量名为:'FillALLComboboxes'。
enter image description here
问题:考虑到DataGrid中的'DataGridComboBoxColumn'、'ANBAR_COL'、'DataGridComboBoxColumn'有一个默认值,用户不需要从'DataGridComboBoxColumn'列表中手动选择该项,但只要双击'ANBAR_COL'组合框单元格,默认值就会出现!关键是,为“DataGridComboBoxColumn”设置的默认值在用户进入单元格之前不会显示!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>
我的窗口背后的代码(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:
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));
}
}
}
我想要的是在加载'DataGrid'数据时显示'DataGrid'内的此组合框已选择默认值(无需双击并进入CellEditing模式)。
1条答案
按热度按时间b5lpy0ml1#
我绕过了我的问题,它的工作!:
XAML:
C#: