We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Row Data Bound

Hello,
I have a grid with a TemplateColumn inside where there are various buttons. I need to manage the bind of each row and to show or hide buttons by row.
What is the of row data bound? Once I handled the event, how do I take the cell with the buttons and hide / view them?

Thanks in advance

Best regards

1 Reply

GM Gobikrishnan Murugesan Syncfusion Team July 10, 2015 11:33 AM UTC

Hi VAT,

Thank you for contacting Syncfusion support.

We have analyzed your Query. The requirement “Show/hide buttons in template column based on row data” can be achieved in two ways. Please find the details below,

Using IValueConverter
While loading the grid, you can show/hide buttons by using IValueConverter.

XAML

        <syncfusion:SfDataGrid x:Name="grid"

                        ItemsSource="{Binding student}" Grid.Row="0" Grid.RowSpan="2" AutoGenerateColumns="False"

                        ShowGroupDropArea="True" AllowGrouping="True"

                        >

            <syncfusion:SfDataGrid.Columns>

                <syncfusion:GridTextColumn   HeaderText="Name" MappingName="Name"  />

                <syncfusion:GridTextColumn    HeaderText="Age"  MappingName="Age" />

                <syncfusion:GridCurrencyColumn AllowScrollingOnCircle="False" CurrencyDecimalDigits="2" CurrencyPositivePattern="2" CurrencySymbol="$" HeaderText="Income" MappingName="Income" />

                <syncfusion:GridCheckBoxColumn MappingName="IsCheck" HeaderText="Registered" />

                <syncfusion:GridTemplateColumn HeaderText="Template Column" MappingName="Name">

                    <syncfusion:GridTemplateColumn.CellTemplate>

                        <DataTemplate>

                            <Button VerticalAlignment="Center" Height="25" Width=" 65"

                                                            FontStyle="Italic"

                                                            FontWeight="Bold"

                                                            Content="{Binding Quantity}"

                                                            Visibility="{Binding Converter={StaticResource converter}}"

                                                            />

                        </DataTemplate>

                    </syncfusion:GridTemplateColumn.CellTemplate>

                </syncfusion:GridTemplateColumn>

            </syncfusion:SfDataGrid.Columns>

        </syncfusion:SfDataGrid>


C#

class Converters :IValueConverter

    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            var values =(Student) value;

            if (values.Age == 12)

                return Visibility.Visible;


            return Visibility.Hidden;

        }


        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            throw new NotImplementedException();

        }

    }



Using Current cell activated event
Using CurrentCellActivated event, you can change the button visibility based on row data while clicking the grid cell at run time.

C#

public UserControl1()

{

    InitializeComponent();

    grid.CurrentCellActivated+=grid_CurrentCellActivated;

}


private void grid_CurrentCellActivated(object sender, CurrentCellActivatedEventArgs args)

{

    //get the current cell

    var currentcell = grid.SelectionController.CurrentCellManager.CurrentCell.ColumnElement;


    // search the button using Visual tree

    Button button = GetVisualChild<Button>(currentcell);


    //current cell row data

    var rowdata = (Student)currentcell.DataContext;


    //based on the data button visibility has been decided

    if (rowdata.Age != 12 && button!=null)

        button.Visibility = Visibility.Visible;     

}


We have also prepared the sample based on this and please find the sample under the following location
http://www.syncfusion.com/downloads/support/forum/119565/ze/DataGrid-2053672819

If your requirement is differ from this, could you please share more information on your requirement. This would be helpful for providing better solution.

Please let me know if this solution helps you.


Thanks,

Gobikrishnan M


Loader.
Up arrow icon