使用TokenLookUpEdit的SelectedItems填充WPF Datagrid

时间:2019-04-17 07:20:23

标签: c# mysql wpf devexpress

我在使用TokenLookUpEdit中选择的值填充发票编号= TokenLookUpEdit选择的值的datagrid时遇到问题

这是我的代码:

   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";


                }
            }

2 个答案:

答案 0 :(得分:0)

您需要做的第一件事是将EditValue绑定到集合,因为您将获得行列表并使用CollectionConverter,而您需要维护{{1}的集合更改事件},这样您就可以获得选定行的列表

Xaml

ObservableCollection

转换器

`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);
        }

    }

CTOR

 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;
            }
        }

事件

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

答案 1 :(得分:0)

我以另一种方式完成了

首先我创建了公共结构

 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; }
        }

之后,我使用了添加语句抛出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();
            }



        }