Click here to Skip to main content
15,887,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi every one,
I want to repeat datagrid data in textboxes of another window. Data has been saved in sql databse and i want to retrieve them on the new window based on click on one datagrid row.
how can i do? thanks.

XML
<DataGrid Margin="0,23,0,0" AutoGenerateColumns="False" EnableRowVirtualization="True"
                 ItemsSource="{Binding}" Name="grdPeople" VerticalContentAlignment="Center"
                 IsReadOnly="True" DataContext="{Binding}">

           <DataGrid.Columns>
               <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="Auto" ></DataGridTextColumn>
               <DataGridTextColumn Binding="{Binding Path=Job}" Header="Job" Width="Auto"></DataGridTextColumn>
               <DataGridTemplateColumn Header="Picture" Width="45" >
                   <DataGridTemplateColumn.CellTemplate >
                       <DataTemplate >
                           <Image Source="{Binding Path=Picture}" Width="30" Height="30" Stretch="Uniform">
                           </Image>
                       </DataTemplate>
                   </DataGridTemplateColumn.CellTemplate>
               </DataGridTemplateColumn>
           </DataGrid.Columns>
       </DataGrid>
Posted
Updated 7-Jan-13 6:22am
v2

1 solution

Define and implement some interface with an event object in it. It's very convenient to write a separate file with another part of the partial window class, then you would not need to mention the interface in different parts of this class. Implement it in one of the window. For example, a window where you grid view is can introduce and event DataGridRawSelected. Create it based on your own event arguments class (derived from System.EventArgs class) to pass all data you need to show in a different window.

As the instance of the first window can now be represented by an interface instance, with everything else hidden from the user, you can pass this interface reference to the second window. It should have a field in its class of the interface type. Assign this field to the first window. Now your second window have a field of the interface type, and the interface reference points to the first window, but only interface members are accessed. In the code of the second window, add a handler to the event, which is a member of the interface. An event handler will receive event arguments which carry all the information related to the selected raw, but only the information needed to be presented in a second window. Use the reference to the text boxes, which are now accessible, because the handler is the method of a second window. Show this information in these text boxes.

Another way around is possible: implement and interface for the second window; this interface properties should represent texts to be shown in the text boxes, but in some abstract form (just use your semantic names for the meaning of those strings), pass it in the first window, which should use the interface reference to fill in the text data.

However, first approach provides better isolation and better abstracts out the UI. An interface is used only for handling an event. You can consider any combination approaches.

I did not say anything new here; you should be able to create and implement interfaces, exchange interface instances instead of references to implementing classes/structures, define event instances and add event handlers to the invocation lists of interface instances. All of these chores is a must for any UI development. I only suggested a neatly packages and robust design of the code.

—SA
 
Share this answer
 
Comments
M.H. Shojaei 7-Jan-13 19:53pm    
Thanks
Sergey Alexandrovich Kryukov 7-Jan-13 20:02pm    
OK, will you accept the answer formally (green button)? — thanks.
Of course, if you have some follow-up questions, you are welcome to ask.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900