如何在WPF中获取DevExpress GridControl的选定行值? [英] How to get the selected row values of DevExpress GridControl in WPF?

查看:571
本文介绍了如何在WPF中获取DevExpress GridControl的选定行值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows应用程序工作时,我在2012年10月询问了

解决方案

您可以使用 DataControlBase.CurrentItemChanged 事件。

这里是例子:

WPF:

 < dxg:GridControl x:Name =gridControl1ItemsSource ={Binding Data}CurrentItemChanged =gridControl1_CurrentItemChanged> 
< / dxg:GridControl>

事件处理程序:

  private void gridControl1_CurrentItemChanged(object sender,CurrentItemChangedEventArgs e)
{
TBGRNo.Text = gridControl1.GetFocusedRowCellValue(GRNo)ToString();
TBSName.Text = gridControl1.GetFocusedRowCellValue(SName)。ToString();
TBFName.Text = gridControl1.GetFocusedRowCellValue(FName)。ToString();
}


I asked this question in Oct, 2012 when I was working on windows application. now when I turn to WPF application, I get the same problem again i.e How to get the selected row values of DevExpress GridControl in WPF? I've failed to find my answer on google and none of the answers in the above mentioned link is working. there is nothing like CellClick, RowClick or RowCellClick event in devexpress gridcontrol of wpf as it is in winform gridcontrol. I'll be glad if someone can solve out this problem

Edit

I have updated my application with required namespaces as you updated in your answer but the problem remains the same. i'm getting following tow errors in

<Grid.DataContext>
        <dxmvvm:ViewModelSource Type="{x:Type local:EntitiesViewModel}"/>
    </Grid.DataContext>

1- the type 'dxmvvm:ViewModelSource'" was not found in dxmvvm:ViewModelSource

2- name "EntitiesViewModel" does not exist in the namespace "clr-namespace:DXApplication1"

my application coding is as follows

XAML

<dx:DXWindow
    x:Class="DXApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DXApplication1"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
    Title="DXApplication" Height="700" Width="1100"
    SnapsToDevicePixels="True" UseLayoutRounding="True">

    <dx:DXWindow.Resources>
    </dx:DXWindow.Resources>

    <Grid Margin="12">
        <Grid.DataContext>
            <dxmvvm:ViewModelSource Type="{x:Type local:EntitiesViewModel}"/>
        </Grid.DataContext>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Margin="0,0,0,8">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="8"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Label Content="Item1" Grid.Row="0" VerticalAlignment="Center"/>
            <Label Content="Item2" Grid.Row="1" VerticalAlignment="Center"/>
            <Label Content="Item3" Grid.Row="2" VerticalAlignment="Center"/>
            <dxe:TextEdit Text="{Binding SelectedEntity.Item1}" Grid.Row="0" Grid.Column="2"/>
            <dxe:TextEdit Text="{Binding SelectedEntity.Item2}" Grid.Row="1" Grid.Column="2"/>
            <dxe:TextEdit Text="{Binding SelectedEntity.Item3}" Grid.Row="2" Grid.Column="2"/>
        </Grid>
        <dxg:GridControl Grid.Row="1" 
                     AutoGenerateColumns="None" 
                     ItemsSource="{Binding Entities}"
                     SelectedItem="{Binding SelectedEntity}">
            <dxg:GridControl.Columns>
                <dxg:GridColumn FieldName="Item1"/>
                <dxg:GridColumn FieldName="Item2"/>
                <dxg:GridColumn FieldName="Item3"/>
            </dxg:GridControl.Columns>
            <dxg:GridControl.View>
                <dxg:TableView ShowTotalSummary="True" AllowEditing="False"/>
            </dxg:GridControl.View>
        </dxg:GridControl>
    </Grid>
</dx:DXWindow>

EntitiesViewModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace DXApplication1
{
    public class EntitiesViewModel
    {
        public EntitiesViewModel()
        {
            LoadEntities();
        }
        void LoadEntities()
        {
            Entities = new ObservableCollection<Entity>
        {
            new Entity(){ Item1="A", Item2="A0", Item3="A00"},
            new Entity(){ Item1="B", Item2="B0", Item3="B00"},
            new Entity(){ Item1="C", Item2="C0", Item3="C00"},
        };
        }
        public ObservableCollection<Entity> Entities { get; private set; }
        public virtual Entity SelectedEntity { get; set; } // Bindable property
    }
    public class Entity
    {
        public string Item1 { get; set; }
        public string Item2 { get; set; }
        public string Item3 { get; set; }
    }
}

Image for libraries, error, class etc

解决方案

You can use DataControlBase.CurrentItemChanged event.
Here is example:
WPF:

<dxg:GridControl x:Name="gridControl1" ItemsSource="{Binding Data}" CurrentItemChanged="gridControl1_CurrentItemChanged">
</dxg:GridControl>

Event handler:

private void gridControl1_CurrentItemChanged(object sender, CurrentItemChangedEventArgs e)
{
    TBGRNo.Text = gridControl1.GetFocusedRowCellValue("GRNo").ToString();
    TBSName.Text = gridControl1.GetFocusedRowCellValue("SName").ToString();
    TBFName.Text = gridControl1.GetFocusedRowCellValue("FName").ToString();    
}

这篇关于如何在WPF中获取DevExpress GridControl的选定行值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆