locked
help me in DataGrid RRS feed

  • Question

  • Someone please could help me, how to add multiple cells and your result show in a cell down, also how change the method F2 for sign in in mode edition for the Key.Enter, please Help me.

    Sorry I speak spanish, I am teach english... :)

    Tuesday, November 17, 2009 11:03 AM

Answers

  • Hi,

       You asked how to add multiple cells to a cell down. Do you mean to add multiple controls to a cell and then display it vertically. Then you just need to add a DataTemplateColumn like this:

            <data:DataGrid x:Name="dg" KeyDown="dg_KeyDown">
                <data:DataGrid.Columns>
                    <data:DataGridTemplateColumn>
                        <data:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Vertical">
                                    <TextBlock Text="{Binding Age}"></TextBlock>
                                    <TextBlock Text="{Binding Name}"></TextBlock>
                                </StackPanel>
                            </DataTemplate>
                        </data:DataGridTemplateColumn.CellTemplate>
                        <data:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Vertical">
                                    <TextBox Text="{Binding Age}"></TextBox>
                                    <TextBox Text="{Binding Name}"></TextBox>
                                </StackPanel>
                            </DataTemplate>
                        </data:DataGridTemplateColumn.CellEditingTemplate>
                    </data:DataGridTemplateColumn>
                </data:DataGrid.Columns>
            </data:DataGrid>    
     Code Behind:
        public partial class MainPage : UserControl
        {
            public ObservableCollection people;
            public MainPage()
            {
                InitializeComponent();
                people = new ObservableCollection {
                    new Person(){Name="Micheal",Age=11},    
                    new Person(){Name="Fiona",Age=21},  
                    new Person(){Name="Diana",Age=31},  
                    new Person(){Name="Jason",Age=41}, 
                };
                dg.ItemsSource = people;
            }
            private void dg_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.F2) {
                    dg.BeginEdit();
                }
            }
        } 
    My Person Class:
          public class Person 
        {
            public int Age { get; set; }  
            public string Name { get; set; } 

        }

       This shall do the function you requested. Also if you replace the stackpanel with a grid , you will have more flexibility over the cell's layout.

    Best Regards

    Thursday, November 19, 2009 1:06 AM