This project has moved. For the latest updates, please go here.

Beginner problems: Get the value of selected row of a datagrid with ironpython

Mar 24, 2015 at 8:58 AM
I'm using VS 2013 with PTVS and I want to get the values of the selected line in my datagrid. But I'm beginner for python and WPF so I'm getting some trouble, so my datagrid:
<DataGrid CurrentCellChanged="infoVendas" IsReadOnly="True" x:Name="datagridvendas" Opacity="0" AutoGenerateColumns="False" Height="auto" Width="auto" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource XML_Vendas},XPath=Vendas/*,Mode=TwoWay}">
                <Style TargetType="DataGrid">
                        <DataTrigger Binding="{Binding ElementName=checkstackVenda, Path=IsChecked}" Value="True">
                                        <DoubleAnimation  Duration="0:0:0.300" From="0" To="1" Storyboard.TargetProperty="Opacity" />
                                        <DoubleAnimation  Duration="0:0:0.300" From="1" To="0" Storyboard.TargetProperty="Opacity" />
                <DataGridTextColumn Header="Data" Binding="{Binding XPath=@Compra_Data}"/>
                <DataGridTextColumn Header="Produto" Binding="{Binding XPath=@Prod_Desc}"/>
                <DataGridTextColumn Header="Status" Binding="{Binding XPath=@Status}"/>
                <DataGridTextColumn Header="Rastreio" Binding="{Binding XPath=@Rast_Id}"/>
                <DataGridTextColumn Header="Entrega" Binding="{Binding XPath=@Met_Entrega}"/>
                <DataGridTextColumn Header="Nick" Binding="{Binding XPath=@Buyer_Nick}"/>
                <DataGridTextColumn Header="Nome" Binding="{Binding XPath=@Buyer_Name}"/>
                <DataGridTextColumn Header="CPF" Binding="{Binding XPath=@Buyer_Cpf}"/>
                <DataGridTextColumn Header="Email" Binding="{Binding XPath=@Buyer_Email}"/>
                <DataGridTextColumn Header="Avaliacao Recebida" Binding="{Binding XPath=@Buyer_Feed}"/>
                <DataGridTextColumn Header="Avaliacao Enviada" Binding="{Binding XPath=@Seller_Feed}"/>
                <DataGridTextColumn Header="Lucro" Binding="{Binding XPath=@Profit}"/>
                <DataGridTextColumn Header="Total" Binding="{Binding XPath=@Total}"/>
and here is my try to get at least one value:
class MyWindow(Window):
def __init__(self):
    wpf.LoadComponent(self, 'Metal_Goat_Manager.xaml')
def infoVendas(self, sender, e):
    self.textConsole.Text = sender.SelectedValue
But nothing happen, the textblock shows nothing. I've tried selectedrows and selecteditems but i get an object and not a string with values. Maybe there is some way to 'read' this objects that I don't know?
Mar 24, 2015 at 4:16 PM
There shouldn't be anything too special, though DataGrid is a control that I've never used, so there may be.

Have you tried setting a breakpoint in your event handler and looking at all the properties on sender?
Marked as answer by pantalipe on 3/24/2015 at 4:35 PM
Mar 24, 2015 at 5:23 PM
If you want to use SelectedValue, then you should set SelectedValuePath on your grid.

I would reiterate that this is not the best place to ask generic WPF/IronPython questions. You will get more and better replies on StackOverflow.
Marked as answer by pantalipe on 3/24/2015 at 4:35 PM
Mar 24, 2015 at 11:34 PM
Thanks for the answers guys!

Ok, I tried the breakpoint as suggested by Zooba and discovered that i can get all the information in xml format using sender.CurrentItem.OuterXml

That's a useful tip for when you are learning about the controls and how to use them, just toggle a breakpoint in the line that you try to read the sender and go to "locals" to see what properties and values a control have.

pminaev I always try StackOverflow and codeproject before post here, but as you can see if you follow the links, 1 week without a single answer.
Here as always a few hours later i get answers that helped me to solve the problem.

Thanks again for your help!