【问题标题】:Using SelectedItems of TokenLookUpEdit to fill WPF Datagrid使用 TokenLookUpEdit 的 SelectedItems 填充 WPF Datagrid
【发布时间】:2019-09-07 08:56:48
【问题描述】:

我在使用我在 TokenLookUpEdit 中选择的值来填充发票编号 = TokenLookUpEdit 选择值的数据网格时遇到问题

这是我的代码:

   using (MySqlConnection cn = new MySqlConnection() { ConnectionString = "DataSource='" + DoSomething.Server_Param + "';UserID='" + DoSomething.Uid_Param + "';Password='" + DoSomething.Password_Param + "';Database='" + DoSomething.Database_Param + "';PORT=3306;CHARSET=utf8" })
            {
                using (MySqlCommand cmd = new MySqlCommand()
                {
                    Connection = cn,
                    CommandText = "SELECT InvoiceNumber,ItemCode,ItemName,Items_QTY,Reservation_Date, " +
                    "EngName,UserName,Address,InvoiceStore,ContsrType,Place FROM Delta_Invoices_Grid where InvoiceNumber  ='"+ InvoicesNumbersCombo.Text + "'"
                })
                {
                    cn.Open();
                    MySqlDataReader dr = cmd.ExecuteReader();
                    DataTable dt = new DataTable();
                    dt.Load(dr);
                    InvoicesDetails_Grid.ItemsSource = dt.DefaultView;
                    cn.Close();



                }


                }

现在的问题是,datagrid 只使用了 TokenLookUpEdit 中的一个值,而不是所有选定的值,我昨天已经搜索过但找不到解决方案

这是 TokenLookUpEdit 和 Datagrid 的 XAML 代码:

<GroupBox Header="أرقام الفواتير" Margin="76,10,592,439" FlowDirection="RightToLeft" FontFamily="Droid Arabic Kufi">
            <dxg:LookUpEdit x:Name="InvoicesNumbersCombo" Margin="16,12,16,16"
                                    ItemsSource="{Binding Delta_Invoices_Grid}"
                                    EditValue="{Binding InvoiceNumber, Mode=OneWay}" AutomationProperties.IsColumnHeader="True"
                                  >
                <dxg:LookUpEdit.StyleSettings>
                    <dxg:TokenLookUpEditStyleSettings />
                </dxg:LookUpEdit.StyleSettings>
            </dxg:LookUpEdit>

        </GroupBox>

        <DataGrid x:Name="InvoicesDetails_Grid" FlowDirection="RightToLeft" IsReadOnly="True" SelectionUnit="FullRow" AutoGenerateColumns="False" CanUserAddRows="True" ItemsSource="{Binding Dawam_Grid_ItemsBinding}" Margin="460,129,10,243">
            <DataGrid.Resources>
                <Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="Background" Value="#464646" />
                    <Setter Property="Foreground" Value="#F0F0F0" />


                </Style>

                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="Foreground" Value="#F88E1D"/>
                </Style>
                <Style TargetType="{x:Type ScrollBar}" >
                    <Setter Property="Background" Value="#FAFAFA" />

                </Style>



                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="#360E05"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>

            </DataGrid.Resources>
            <DataGrid.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#464646" Offset="0.221"/>
                    <GradientStop Color="#4E93CA" Offset="1"/>
                </LinearGradientBrush>
            </DataGrid.Background>
            <DataGrid.RowBackground >
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Transparent" Offset="0.221"/>
                    <GradientStop Color="Transparent" Offset="1"/>
                </LinearGradientBrush>
            </DataGrid.RowBackground>
            <DataGrid.Columns>
                <DataGridTextColumn Header="رقم الفاتورة" x:Name="InvoiceNumber" IsReadOnly="True" Binding="{Binding InvoiceNumber}" Foreground="#F0F0F0"/>
                <DataGridTextColumn Header="كود الصنف" x:Name="ItemCode" IsReadOnly="True" Binding="{Binding ItemCode}" Foreground="#F0F0F0"/>
                <DataGridTextColumn Header="إسم الصنف" x:Name="ItemName" Width="250"  IsReadOnly="True" Binding="{Binding ItemName}" Foreground="#F0F0F0"/>

                <DataGridTextColumn Header="الكمية" x:Name="Items_QTY" IsReadOnly="True" Binding="{Binding Items_QTY}" Foreground="#F0F0F0"/>
                <DataGridTextColumn Header="تاريخ التركيب" x:Name="Reservation_Date" IsReadOnly="True" Binding="{Binding Reservation_Date, StringFormat=dd/MM/yyyy}" Foreground="#F0F0F0"/>
                <DataGridTextColumn Header="إسم المهندس" x:Name="EngName" Width="70" IsReadOnly="True" Binding="{Binding EngName}"  Foreground="#F0F0F0"/>
                <DataGridTextColumn Header="إسم البائع" x:Name="UserName" IsReadOnly="True" Binding="{Binding UserName}" Foreground="#F0F0F0"/>
                <DataGridTextColumn Header="العنوان" x:Name="Address" Width="70" IsReadOnly="True" Binding="{Binding Address}"  Foreground="#F0F0F0"/>
                <DataGridTextColumn Header="المعرض" x:Name="InvoiceStore" IsReadOnly="True" Binding="{Binding InvoiceStore}" Foreground="#F0F0F0"/>
                <DataGridTextColumn Header="نوع التركيب" x:Name="ContsrType" Width="70" IsReadOnly="True" Binding="{Binding ContsrType}"  Foreground="#F0F0F0"/>
                <DataGridTextColumn Header="مكان التركيب" x:Name="Place" Width="70" IsReadOnly="True" Binding="{Binding Place}"  Foreground="#F0F0F0"/>
                <DataGridTextColumn Header="الفني" x:Name="TechName" Width="70" IsReadOnly="False" Binding="{Binding TechName}"  Foreground="#F0F0F0"/>

            </DataGrid.Columns>
        </DataGrid>

这是 TokenLookUpEdit 填充代码:

InitializeComponent();
            using (MySqlConnection cn = new MySqlConnection() { ConnectionString = "DataSource='" + DoSomething.Server_Param + "';UserID='" + DoSomething.Uid_Param + "';Password='" + DoSomething.Password_Param + "';Database='" + DoSomething.Database_Param + "';PORT=3306;CHARSET=utf8" })
            {
                using (MySqlCommand cmd = new MySqlCommand() { Connection = cn, CommandText = "SELECT InvoiceNumber,CustomerName FROM Delta_Invoices_Grid " })
                {
                    cn.Open();
                    MySqlDataReader dr = cmd.ExecuteReader();
                    DataTable dt = new DataTable();
                    dt.Load(dr);

                    InvoicesNumbersCombo.ItemsSource = dt.DefaultView;
                    InvoicesNumbersCombo.ValueMember = "CustomerName";
                    InvoicesNumbersCombo.DisplayMember = "InvoiceNumber";


                }
            }

【问题讨论】:

  • 你应该显示你的 xaml 文件....
  • 为 Datagrid 和 TokenLookUpEdit 完成 @Frenchy
  • @MohamadSalama 可以告诉我你面临的问题是什么?
  • @Avinash 我的问题是 iam 使用 TokenLookUpEdit 并选择一些发票编号,然后按搜索按钮,从 MySql 数据库中选择其中发票编号 = TokenLookUpEdit 选择的数字并用数据库中的选定行填充 Datagrid,但是直到现在不能使用所有选定的项目它只带来一个项目
  • @MohamadSalama from lookupedit 你需要所有选择的值吗?

标签: c# mysql wpf devexpress


【解决方案1】:

您需要做的第一件事是将EditValue 绑定到一个集合,因为您将获得一个行列表并使用CollectionConverter,并且您需要主控ObservableCollection 的集合更改事件,以便您获得选定行的列表

Xaml

`EditValue="{Binding SelectedRows, Converter={local:CollectionConverter}}`

转换器

public class CollectionConverter : MarkupExtension, IValueConverter {
        public CollectionConverter() { }

        public override object ProvideValue(IServiceProvider serviceProvider) {
            return this;
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            return ((ObservableCollection<Object>)value).ToList<object>();
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            return new ObservableCollection<Object>((List<object>)value);
        }

    }

道具

 public ObservableCollection<Order> Orders { get; private set; }
        ObservableCollection<Object> _SelectedRows;
        public ObservableCollection<Object> SelectedRows {
            get { return _SelectedRows; }
            set {
                _SelectedRows.CollectionChanged -= OnSelectedRowsCollectionChanged;
                _SelectedRows.Clear();
                foreach (var item in value)
                    _SelectedRows.Add(item);
                _SelectedRows.CollectionChanged += OnSelectedRowsCollectionChanged;
            }
        }

CTOR

 _SelectedRows = new ObservableCollection<object>();
            _SelectedRows.CollectionChanged += OnSelectedRowsCollectionChanged;

事件

 void OnSelectedRowsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
            NotifyPropertyChanged("SelectedRows");

【讨论】:

    【解决方案2】:

    我用另一种方式做到了

    首先我创建了公共结构

     public struct MyData
            {
                public string InvoiceNumber { set; get; }
                public string ItemCode { set; get; }
                public string ItemName { set; get; }
                public string Items_QTY { set; get; }
                public DateTime Reservation_Date { set; get; }
                public string EngName { set; get; }
                public string UserName { set; get; }
                public string Address { set; get; }
                public string InvoiceStore { set; get; }
                public string ContsrType { set; get; }
                public string Place { set; get; }
            }
    

    之后我使用了 add 语句 throw foreach 循环

     private void Search_btn_Click(object sender, RoutedEventArgs e)
            {
                InvoicesDetails_Grid.Items.Clear();
                foreach (System.Data.DataRowView ZR in InvoicesNumbersCombo.SelectedItems)
                {
                    MySqlConnection conn2 = new MySqlConnection("DataSource='" + DoSomething.Server_Param + "';UserID='" + DoSomething.Uid_Param + "';Password='" + DoSomething.Password_Param + "';Database='" + DoSomething.Database_Param + "';PORT=3306;CHARSET=utf8");
                    conn2.Open();
                    MySqlCommand cmd = new MySqlCommand();
                    MySqlDataReader rd;
                    DataSet ds = new DataSet();
    
                    ds.Clear();
                    cmd.Connection = conn2;
    
                    cmd.CommandText = "SELECT InvoiceNumber,ItemCode,ItemName,Items_QTY,Reservation_Date, " +
                            "EngName,UserName,Address,InvoiceStore,ContsrType,Place FROM Delta_Invoices_Grid where InvoiceNumber  ='" + ZR["InvoiceNumber"] + "'";
                    rd = cmd.ExecuteReader();
    
                    while (rd.Read())
                    {
                        InvoicesDetails_Grid.Items.Add(new MyData()
                        {
                            InvoiceNumber = (rd["InvoiceNumber"].ToString()),
                            ItemCode = (rd["ItemCode"].ToString()),
                            ItemName = rd["ItemName"].ToString(),
                            Items_QTY = (rd["Items_QTY"].ToString()),
                            Reservation_Date = Convert.ToDateTime(rd["Reservation_Date"]),
                            EngName = rd["EngName"].ToString(),
                            UserName = rd["UserName"].ToString(),
                            Address = rd["Address"].ToString(),
                            InvoiceStore = rd["InvoiceStore"].ToString(),
                            ContsrType = rd["ContsrType"].ToString(),
                            Place = rd["Place"].ToString()
                        });
                    }
    
    
    
                    conn2.Close();
                }
    
    
    
            }
    

    【讨论】:

      猜你喜欢
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      相关资源
      最近更新 更多