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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Button in a GridDataControl, DaaContext issue

Thread ID:

Created:

Updated:

Platform:

Replies:

114369 Nov 15,2013 01:24 PM UTC Nov 26,2013 04:02 AM UTC WPF 3
loading
Tags: GridControl
Francis Rouleau
Asked On November 15, 2013 01:24 PM UTC

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.

Saravanan M [Syncfusion]
Replied On 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

Francis Rouleau
Replied On November 22, 2013 04:47 PM UTC

it works thanks

Saravanan M [Syncfusion]
Replied On November 26, 2013 04:02 AM UTC

Hi Francis,

Thanks for your update.

Regards,

Saravanan.M


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;