prabhakar
prabhakar

Reputation: 57

how to commit the edit without moving to next cells in the datagrid

I am using ComboBox inside a DataGrid which is in CellEditingTemplate. I insert the selected item to the textblock in same cell, which is in CellTemplate. the insertion will happen only when I move to next cell.

what I want is when I select item from the ComboBox it should insert it in the TextBlock without moving to the next cell.

here is my xaml.

        <DataGrid.Columns>
            <DataGridTextColumn Header="Hours"  Binding="{Binding time}" FontSize="14" FontWeight="Bold" IsReadOnly="True"  Width="100"/>


            <DataGridTemplateColumn Header="Monday" Width="100">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel >
                            <TextBlock x:Name="mon" Text="{Binding Path=SelectedSubject}"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <!--<ComboBox x:Name="monday" Width="50"   IsSynchronizedWithCurrentItem="true"   Loaded="monday_Loaded" SelectionChanged="monday_SelectionChanged"></ComboBox>-->
                            <ComboBox x:Name="monday" Width="50" ItemsSource="{Binding Path=Subjects}" SelectedItem="{Binding Path=SelectedSubject}"  IsSynchronizedWithCurrentItem="true"   Loaded="monday_Loaded" SelectionChanged="monday_SelectionChanged"></ComboBox>

                            <ComboBox x:Name="staff" Width="50" Loaded="staff_Loaded"></ComboBox>
                        </StackPanel>
                    </DataTemplate>

                </DataGridTemplateColumn.CellEditingTemplate>

Is it possible to do this?

If any one have any idea about how to do it please help me.

Upvotes: 1

Views: 725

Answers (2)

Pranjal Jain
Pranjal Jain

Reputation: 361

Sorry i have use above code but it was not giving perfect answer. Use mouse leave event of monday combo box and commit edit in that event it will work fine. :)

private void monday_MouseLeave(object sender, MouseEventArgs e)
    {
        this.myGrid.CommitEdit();
    }

Upvotes: 3

Omri Btian
Omri Btian

Reputation: 6547

If you add a name to your DataGrid you can access it from monday_SelectionChanged and commit the edits:

<Grid x:Name="myGrid" ....>

in the handler of the ComboBox selection changed event

private void monday_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    myGrid.CommitEdit();

    // Rest of your implementation ....
}

Upvotes: 0

Related Questions