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

Button in a GridDataControl, DaaContext issue

i needed to add a simple button in a GridDataControl. note complicated so far.

    <syncfusion:GridDataVisibleColumn HeaderText="Open Unit" MappingName="JobNumber">
        <syncfusion:GridDataVisibleColumn.HeaderStyle>
            <syncfusion:GridDataColumnStyle HorizontalAlignment="Center" />
        </syncfusion:GridDataVisibleColumn.HeaderStyle>
        <syncfusion:GridDataVisibleColumn.CellItemTemplate>
            <DataTemplate>                                        
                <Button HorizontalContentAlignment="Stretch"
                        VerticalContentAlignment="Stretch"
                        Name="cmdOpenUnit"
                        Click="cmdOpenUnit_Click"
                        Content="Open Unit" />   
            </DataTemplate>
        </syncfusion:GridDataVisibleColumn.CellItemTemplate>
    </syncfusion:GridDataVisibleColumn>

Now i tried the `GridDataUnboundVisibleColumn` but by using it i cannot use a mapping name since it crashes and anyway the`DataContext` is empty. So i used `GridDataUnboundVisibleColumn` and set `MappingName="JobNumber"` which is a random data in the source just to make sure i have `DataContext`

The click event :

    private void cmdOpenUnit_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            CUnit obj = ((Button)sender).DataContext as CUnit ;
        }
        catch { }
    }

I have a grid with `ObservableCollection<CProject>` as Datasource then when you choose one the `CProject.UnitList // <-- ObservableCollection<CUnit>` become the Datasource of the currect grid i am working with. Therefore each row in the grid is a specific `CUnit`.

The problem is that the DataContext on `((Button)sender).DataContext` is the `JobNumber` data in the `Cunit` object and i cannot get the parent property. i tried different casting but couldn't get anything yet. I have used this method a couple time with success but it's my first time doing this with syncfusion and the help files contains nothing about that.

the DataContext of the Sender i received is of type `Syncfusion.Windows.Controls.Grid.GridDataCellBoundWrapper` if that might help.

3 Replies

SM Saravanan M Syncfusion Team November 20, 2013 01:30 PM UTC

Hi Francis,

We are sorry for delay,

We have analyzed your query. When you want to get current record from the button click event you have to convert DataContext as GridDataCellBoundWrapper Class, from that you can get the Record and you have to typecast with RecodEntry. Now you can able to get the current record. You can also refer the below codesnippet.

Codesnippet[C#]:

private void cmdOpenUnit_Click(object sender, RoutedEventArgs e)

        {

            var data = ((Button)sender).DataContext as GridDataCellBoundWrapper;

            var style = data.Record as RecordEntry;

            var record = style.Data as Data;

           

        }

 

 

We have prepared a sample based on your requirement, please find the attached sample below,

Please let us know if this helps you,

Regards,

Saravanan.M



GDC_Sample_e7421fd4.zip


FR Francis Rouleau November 22, 2013 04:47 PM UTC

it works thanks


SM Saravanan M Syncfusion Team November 26, 2013 04:02 AM UTC

Hi Francis,

Thanks for your update.

Regards,

Saravanan.M


Loader.
Up arrow icon