locked
can this be done and if so how? RRS feed

  • Question

  • I have a datagrid, which is populated by ria control. The datagrid has a check box field. Is it possible to either;

    1 put a check box in the header and when that is checked then the all checkboxes in that column are checked

    or

    2. just click on that column header and cll check boxes to be checked / unckecked

            <my:BusyIndicator HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="busyIndicator" IsBusy="False"  >
                        <sdk:DataGrid AutoGenerateColumns="False" Height="450" Name="DGSubmission" Width="1110"  MaxHeight="500" IsReadOnly="True" ItemsSource="{Binding Data,ElementName=Ria_SubmissionFiles}">
                            <sdk:DataGrid.Columns>
                                <sdk:DataGridTemplateColumn    Width="90"   CanUserReorder="False" CanUserResize="False" Header="Select Files">
                                    <sdk:DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <Grid>
                                                <CheckBox VerticalAlignment="Center" HorizontalAlignment="Center"  IsChecked="False"  />
                                            </Grid>
                                        </DataTemplate>
                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                </sdk:DataGridTemplateColumn>
                               <sdk:DataGridTemplateColumn    Width="90"   CanUserReorder="False" CanUserResize="False" Header="Ledger Key"  Visibility="Collapsed" >
                                    <sdk:DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox Name="txtLedgerKey" Text="{Binding LedgerKey}" FontStyle="Normal" FontWeight="Bold"   TextAlignment="Center"   />
                                        </DataTemplate>
                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                </sdk:DataGridTemplateColumn>
                                <sdk:DataGridTemplateColumn    Width="90"   CanUserReorder="False" CanUserResize="False"  Header="Licence">
                                    <sdk:DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox Name="txtLicence" Text="{Binding Licence}"   FontStyle="Normal"    TextAlignment="Center"   />
                                        </DataTemplate>
                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                </sdk:DataGridTemplateColumn>
    
                                <sdk:DataGridTemplateColumn    Width="200"   CanUserReorder="False" CanUserResize="False"  Header="Company Name">
                                    <sdk:DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox Name="txtCompany" Text="{Binding Company}"   FontStyle="Normal"    TextAlignment="Center"   />
                                        </DataTemplate>
                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                </sdk:DataGridTemplateColumn>
                                <sdk:DataGridTemplateColumn    Width="90"   CanUserReorder="False" CanUserResize="False"   Header="Serial">
                                    <sdk:DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox Name="txtSerial" Text="{Binding Serial}"    FontStyle="Normal"    TextAlignment="Center"   />
                                        </DataTemplate>
                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                </sdk:DataGridTemplateColumn>
                                <sdk:DataGridTemplateColumn    Width="90"   CanUserReorder="False" CanUserResize="False" Header="Account ID">
                                    <sdk:DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox Name="txtACCID" Text="{Binding ACCID}"   FontStyle="Normal"    TextAlignment="Center"   />
                                        </DataTemplate>
                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                </sdk:DataGridTemplateColumn>
                                <sdk:DataGridTemplateColumn    Width="90"   CanUserReorder="False" CanUserResize="False"  Header="BACS Number">
                                    <sdk:DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox Name="txtBACSID" Text="{Binding BACSID}"     FontStyle="Normal"    TextAlignment="Center"   />
                                        </DataTemplate>
                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                </sdk:DataGridTemplateColumn>
                                <sdk:DataGridTemplateColumn    Width="90"   CanUserReorder="False" CanUserResize="False"  Header="Transactions" >
                                    <sdk:DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox Name="txtTrans" Text="{Binding Trans}"    FontStyle="Normal"   TextAlignment="Right"    />
                                        </DataTemplate>
                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                </sdk:DataGridTemplateColumn>
                                <sdk:DataGridTemplateColumn    Width="90"   CanUserReorder="False" CanUserResize="False"  Header="File Value">
                                    <sdk:DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox Name="txtFILEVALUE" Text="{Binding FileValue, StringFormat=\{0:n2\}}"    FontStyle="Normal" TextAlignment="Right"   />
                                        </DataTemplate>
                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                </sdk:DataGridTemplateColumn>
                                <sdk:DataGridTemplateColumn    Width="90"   CanUserReorder="False" CanUserResize="False"   Header="Payment Date">
                                    <sdk:DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox Name="txtPaymentDate" Text="{Binding PaymentDate, Converter={StaticResource DateConvert}}"     FontStyle="Normal"    TextAlignment="Center"   />
                                        </DataTemplate>
                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                </sdk:DataGridTemplateColumn>
                                <sdk:DataGridTemplateColumn    Width="90"   CanUserReorder="False" CanUserResize="False"  Header="File Type" >
                                    <sdk:DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox Name="txtFILETYPE" Text="{Binding FileType}"    FontStyle="Normal"    TextAlignment="Center"   />
                                        </DataTemplate>
                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                </sdk:DataGridTemplateColumn>
                                <sdk:DataGridTemplateColumn    Width="90"   CanUserReorder="False" CanUserResize="False"   Header="View transactions">
                                    <sdk:DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <Button Name="Btnview" Width="AUTO" Content="View" Click="ViewTrans_Click" CommandParameter="{Binding LedgerKey}"/>
                                        </DataTemplate>
                                    </sdk:DataGridTemplateColumn.CellTemplate>
                                </sdk:DataGridTemplateColumn>
                            </sdk:DataGrid.Columns>
                        </sdk:DataGrid>


     

    Thursday, March 31, 2011 7:50 PM

Answers

  • Is the result from the service call (entityset of spWeb_SubmissionFileBuilder_Result) what you use as items source for the data grid? You need to get access to this collection of items again - either you have stored the result of the service call somewhere in a variable/field, or you can cast back the ItemsSource property of the data grid to that entityset. This is what you use in the for loop.


    Monday, April 4, 2011 4:48 PM

All replies

  • Hi. Yes, this is possible. To change the header, you need to use the HeaderStyle property. This link contains the default column header style (the last XAML on the page), you can use it as starting point and change what you want, in particular the control template.

    When the check box is clicked, you have to iterate through your data items (not the data grid rows!) and flip the property that is bound to the check box column. Through your data bindings the UI will pick up this change. That way you can check/uncheck all at once.


    Friday, April 1, 2011 2:15 AM
  • Hi MisterG

    I have looked into this and aslo looked at a few other threads, but still can't grasp it. Do you know of a good example that even I can follow ?

    Friday, April 1, 2011 5:31 AM
  • All the samples I found either were much too complicated or even faulty because they try to manipulate the UI elements instead of the data source (which you should avoid in any case, it will fail).

    So I put together the following quick sample:

    <sdk:DataGrid x:Name="MyDataGrid"
                    AutoGenerateColumns="False">
        <sdk:DataGrid.Columns>
            <sdk:DataGridTemplateColumn>
                <sdk:DataGridTemplateColumn.HeaderStyle>
                    <Style TargetType="sdk:DataGridColumnHeader">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <CheckBox x:Name="CheckAll"
                                                    Click="CheckAll_Click"
                                                    Grid.Column="0" />
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </sdk:DataGridTemplateColumn.HeaderStyle>
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"/>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>
                    
            <sdk:DataGridTextColumn Header="Name"
                                        Width="150"
                                        Binding="{Binding Path=Name}"
                                        FontSize="10" />
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>


    The required data class for this looks like:

    public class Employee : INotifyPropertyChanged
    {
        private bool _isSelected;
        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }
            set
            {
                _isSelected = value;
                RaisePropertyChanged("IsSelected");
            }
        }
    
        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                RaisePropertyChanged("Name");
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public Employee(string name)
        {
            Name = name;
        }
    
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        public static List<Employee> GetSampleEmployeeList()
        {
            return new List<Employee>(new Employee[4] {
                                        new Employee("Mike"),
                                        new Employee("Jack"),
                                        new Employee("John"),
                                        new Employee("Thomas")
                    });
        }
    }


    Nothing fancy here, it has two properties and implements INotifyPropertyChanged so the UI can pick up changes. The last method just produces some sample data.

    Finally, here is the main page code that uses the data to fill the grid, and has the checkbox code that simply sets the selected property of all items.

    public partial class MainPage : UserControl
    {
        private List<Employee> _sourceCollection;
    
        public MainPage()
        {
            InitializeComponent();
    
            _sourceCollection = Employee.GetSampleEmployeeList();
            MyDataGrid.ItemsSource = _sourceCollection;
        }
    
        private void CheckAll_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var checkBox = sender as CheckBox;
            bool selected = checkBox.IsChecked.HasValue ? checkBox.IsChecked.Value : false;
    
            foreach (var employee in _sourceCollection)
            {
                employee.IsSelected = selected;
            }
        }
    }



    Friday, April 1, 2011 6:01 AM
  • Hi Mister G;

    Thnk you for the sample code, however I still can't get it to work. In my code the datagrid is being popilated by a rialcontrol. I have tried numerous variations for the CheckAll_Click event, but all are throwing up different errors including;

    This EntitySet of Type 'Boss2011.spWeb_SubmissionFileBuilder_Result' does not support the 'Edit' operation.

    That is issue 1. The next issue is that the when the grid is loaded, the user would either select all records or just a selection by that the checkbox value ischecked = true. In my code behind i need to be able to determine which recorrd have been selected and access the ledgerref value

     

    Saturday, April 2, 2011 10:17 PM
  • If you want to get all entries that are selected, you do - again - not work with the UI elements, but with the data items, like:

    var selectedEmployees = _sourceCollection.Where(e => e.IsSelected);
    foreach (var employee in selectedEmployees)
    {
      // do something with the selected employees
    }


    Your problem here:

    This EntitySet of Type 'Boss2011.spWeb_SubmissionFileBuilder_Result' does not support the 'Edit' operation.

    Is related to the setup of your domain service. Apparently there are no methods in your service that allow edit operations of the entity in question, which is why you see this error. You can either add these methods to your domain service, or e.g. add the IsSelected flag to the client side of your entity class (they are partial), then it shouldn't need to be editable for a two-way binding to work.


    Sunday, April 3, 2011 4:03 AM
  • Hi MisterG

    Im still struggling here. I have spent hours looking at different examples to my problem. All examples i have found deal with entity and not complex type. The spweb_submissionBuilder is a complex type.

    The function in my domain services is

            Public Function spWeb_SubmissionFileBuilder(ByVal Country As String) As IQueryable(Of spWeb_SubmissionFileBuilder_Result)
                Return Me.ObjectContext.spWeb_SubmissionFileBuilder(Country).AsQueryable
            End Function
    

    I have tried to apply your suggestion on adding the edit method or addinga IsSelected flag, with no sucess.  I appricate all you have done so far.

    Pete

    Monday, April 4, 2011 5:00 AM
  • Hi again. Can you show the client-side generated code for that return type?


    Monday, April 4, 2011 9:48 AM
  • Hi MisterG,

    Firstly, thank you for all your help, you must have the patience of a saint.

    Ok here goes,

    The metadata

    <MetadataTypeAttribute(GetType(spWeb_SubmissionFileBuilder_Result.spWeb_SubmissionFileBuilder_Result_Metadata))>
    Partial Public Class spWeb_SubmissionFileBuilder_Result
        Friend NotInheritable Class spWeb_SubmissionFileBuilder_Result_Metadata
            <Key()>
            Public LedgerKey As Guid
        End Class
    End Class


    I populate the datagrid using;

                   <riaControls:DomainDataSource Name="Ria_SubmissionFiles" LoadSize="500" AutoLoad="False" QueryName="spWeb_SubmissionFileBuilder" Height="5"  
                                              LoadedData="BureauFilesDS_LoadedData"  LoadingData="BureauFilesD_LoadingData" >
                <riaControls:DomainDataSource.DomainContext>
                    <DomainDataContext:BureauFilesDS  />
                </riaControls:DomainDataSource.DomainContext>
                <riaControls:DomainDataSource.QueryParameters>
                            <riaControls:Parameter  ParameterName="Country" />
                </riaControls:DomainDataSource.QueryParameters>
            </riaControls:DomainDataSource>


     

    is this what you are looking for ?

    Monday, April 4, 2011 10:08 AM
  • No problem. I think we're getting to the core of the problem.

    What I meant was the client-side automatically generated code for that type. When you expose a query like that, there will be a client-side class for the return value. This generated class (in a hidden file by default) is a partial class, and the way to go is to add the property to that class, not the one on the server side.


    Monday, April 4, 2011 10:33 AM
  • Hi MisterG,

    Would I be right in thinking that the clientside code would be in the  MyapplicationName.web.g.vb file?

    Monday, April 4, 2011 10:41 AM
  • Yes, exactly.


    Monday, April 4, 2011 12:39 PM
  • Hi MisterG,
     Below is all references in that file for this SP
     
    ''' <summary>
    ''' The 'spWeb_SubmissionFileBuilder_Result' entity class.
    ''' </summary>
    <DataContract([Namespace]:="http://schemas.datacontract.org/2004/07/Boss2011")>  _
    Partial Public NotInheritable Class spWeb_SubmissionFileBuilder_Result
        Inherits Entity
        
        Private _accid As Nullable(Of Integer)
        
        Private _bacsid As String
        
        Private _company As String
        
        Private _fileType As String
        
        Private _fileValue As Nullable(Of Decimal)
        
        Private _ledgerKey As Guid
        
        Private _licence As String
        
        Private _paymentDate As DateTime
        
        Private _serial As String
        
        Private _trans As Nullable(Of Integer)
        
        #Region "Extensibility Method Definitions"
    
        ''' <summary>
        ''' This method is invoked from the constructor once initialization is complete and
        ''' can be used for further object setup.
        ''' </summary>
            Private Partial Sub OnCreated()
            End Sub
            Private Partial Sub OnACCIDChanging(ByVal value As Nullable(Of Integer))
            End Sub
            Private Partial Sub OnACCIDChanged()
            End Sub
            Private Partial Sub OnBACSIDChanging(ByVal value As String)
            End Sub
            Private Partial Sub OnBACSIDChanged()
            End Sub
            Private Partial Sub OnCompanyChanging(ByVal value As String)
            End Sub
            Private Partial Sub OnCompanyChanged()
            End Sub
            Private Partial Sub OnFileTypeChanging(ByVal value As String)
            End Sub
            Private Partial Sub OnFileTypeChanged()
            End Sub
            Private Partial Sub OnFileValueChanging(ByVal value As Nullable(Of Decimal))
            End Sub
            Private Partial Sub OnFileValueChanged()
            End Sub
            Private Partial Sub OnLedgerKeyChanging(ByVal value As Guid)
            End Sub
            Private Partial Sub OnLedgerKeyChanged()
            End Sub
            Private Partial Sub OnLicenceChanging(ByVal value As String)
            End Sub
            Private Partial Sub OnLicenceChanged()
            End Sub
            Private Partial Sub OnPaymentDateChanging(ByVal value As DateTime)
            End Sub
            Private Partial Sub OnPaymentDateChanged()
            End Sub
            Private Partial Sub OnSerialChanging(ByVal value As String)
            End Sub
            Private Partial Sub OnSerialChanged()
            End Sub
            Private Partial Sub OnTransChanging(ByVal value As Nullable(Of Integer))
            End Sub
            Private Partial Sub OnTransChanged()
            End Sub
    
            #End Region
        
        
        ''' <summary>
        ''' Initializes a new instance of the <see cref="spWeb_SubmissionFileBuilder_Result"/> class.
        ''' </summary>
        Public Sub New()
            MyBase.New
            Me.OnCreated
        End Sub


            ''' <summary>
            ''' Gets the set of <see cref="spWeb_SubmissionFileBuilder_Result"/> entity instances that have been loaded into this <see cref="BureauFilesDS"/> instance.
            ''' </summary>
            Public ReadOnly Property spWeb_SubmissionFileBuilder_Results() As EntitySet(Of spWeb_SubmissionFileBuilder_Result)
                Get
                    Return MyBase.EntityContainer.GetEntitySet(Of spWeb_SubmissionFileBuilder_Result)
                End Get
            End Property


            ''' <summary>
            ''' Gets an EntityQuery instance that can be used to load <see cref="spWeb_SubmissionFileBuilder_Result"/> entity instances using the 'spWeb_SubmissionFileBuilder' query.
            ''' </summary>
            ''' <param name="Country">The value for the 'Country' parameter of the query.</param>
            ''' <returns>An EntityQuery that can be loaded to retrieve <see cref="spWeb_SubmissionFileBuilder_Result"/> entity instances.</returns>
            Public Function spWeb_SubmissionFileBuilderQuery(ByVal Country As String) As EntityQuery(Of spWeb_SubmissionFileBuilder_Result)
                Dim parameters As Dictionary(Of String, Object) = New Dictionary(Of String, Object)()
                parameters.Add("Country", Country)
                Me.ValidateMethod("spWeb_SubmissionFileBuilderQuery", parameters)
                Return MyBase.CreateQuery(Of spWeb_SubmissionFileBuilder_Result)("spWeb_SubmissionFileBuilder", parameters, false, true)
            End Function
    


                ''' <summary>
                ''' Asynchronously invokes the 'spWeb_SubmissionFileBuilder' operation.
                ''' </summary>
                ''' <param name="Country">The value for the 'Country' parameter of this action.</param>
                ''' <param name="callback">Callback to invoke on completion.</param>
                ''' <param name="asyncState">Optional state object.</param>
                ''' <returns>An IAsyncResult that can be used to monitor the request.</returns>
                <FaultContract(GetType(DomainServiceFault), Action:="http://tempuri.org/BureauFilesDS/spWeb_SubmissionFileBuilderDomainServiceFault", Name:="DomainServiceFault", [Namespace]:="DomainServices"),  _
                 OperationContract(AsyncPattern:=true, Action:="http://tempuri.org/BureauFilesDS/spWeb_SubmissionFileBuilder", ReplyAction:="http://tempuri.org/BureauFilesDS/spWeb_SubmissionFileBuilderResponse"),  _
                 WebGet()>  _
                Function BeginspWeb_SubmissionFileBuilder(ByVal Country As String, ByVal callback As AsyncCallback, ByVal asyncState As Object) As IAsyncResult
                
                ''' <summary>
                ''' Completes the asynchronous operation begun by 'BeginspWeb_SubmissionFileBuilder'.
                ''' </summary>
                ''' <param name="result">The IAsyncResult returned from 'BeginspWeb_SubmissionFileBuilder'.</param>
                ''' <returns>The 'QueryResult' returned from the 'spWeb_SubmissionFileBuilder' operation.</returns>
                Function EndspWeb_SubmissionFileBuilder(ByVal result As IAsyncResult) As QueryResult(Of spWeb_SubmissionFileBuilder_Result)
            End Interface
    



    Monday, April 4, 2011 12:48 PM
  • Good, as you can see the spWeb_SubmissionFileBuilder_Result type is declared as partial, which means you can extend it with your own properties. Simply add a new file somewhere in your project to hold this partial class definition:

    Partial Public Class spWeb_SubmissionFileBuilder_Result
    
        Private _isSelected As Boolean
    
        Property IsSelected() As Boolean
            Get
               Return _isSelected
            End Get
            Set(ByVal Value As String)
                _isSelected = Value
                RaisePropertyChanged("IsSelected")
            End Set
        End Property
    
    End Class


    What this does is add the IsSelected property to the existing class on the client. From now on, you should be able to see and use this property in your code. That means you can now proceed with the example code from my second reply in this topic.


    Monday, April 4, 2011 1:52 PM
  • Thank you, After following your example and codeing, I have sorted my problem , and after stepping through the code, can now see how it works.  Just need one more bit of help,

    When the user has selected the file, they click a button, What i need to do is get the values from a few fields for each selected row and pass into a stored procedure

    I have tried convertting the for loop into vb but only get errors.

     

    Monday, April 4, 2011 3:44 PM
  • Is the result from the service call (entityset of spWeb_SubmissionFileBuilder_Result) what you use as items source for the data grid? You need to get access to this collection of items again - either you have stored the result of the service call somewhere in a variable/field, or you can cast back the ItemsSource property of the data grid to that entityset. This is what you use in the for loop.


    Monday, April 4, 2011 4:48 PM
  • Ahh I see now. Thank you for all your help, I have learned a lot from thjis exercise

    Monday, April 4, 2011 5:20 PM