Skip to main content

Microsoft Silverlight

Answered Question Dynamically enable/disable editing in datagridRSS Feed

(0)

pratixa
pratixa

Member

Member

264 points

88 Posts

Dynamically enable/disable editing in datagrid

 Hello all,

              I want to disable editing in datagrid runtime......say at datagrid loading row event......... user can not able to edit in the datagrid by double clicking the row of the datagrid........ how can i achieve this...Any help would be highly appreciated......

Mark as answer if it helpful to u....

Thanks,
Pratixa

Ardman
Ardman

Contributor

Contributor

3320 points

919 Posts

Answered Question

Re: Dynamically enable/disable editing in datagrid

You could set the IsReadOnly to true in order to stop the editing.

pratixa
pratixa

Member

Member

264 points

88 Posts

Re: Dynamically enable/disable editing in datagrid

 Hi Ardman,

                Thanx for reply..

                In which event and which elemwnt's read only property i can change.......if possible then provide the source code...............

 

Mark as answer if it helpful to u....

Thanks,
Pratixa

ken tucker
ken tucker

All-Star

All-Star

16276 points

2,479 Posts

Re: Dynamically enable/disable editing in datagrid

In the DataGrid PreparingCellForEdit event cast the EditElement to a textbox and set it IsReadOnly to true when you do not want the user to be able to edit the cell

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.preparingcellforedit(VS.95).aspx

 

 

    Private Sub dgProducts_PreparingCellForEdit(ByVal sender As Object, ByVal e As System.Windows.Controls.DataGridPreparingCellForEditEventArgs) Handles dgProducts.PreparingCellForEdit
        If DirectCast(dgProducts.SelectedItem, Northwind.Products).ProductName.StartsWith("C") Then
            DirectCast(e.EditingElement, TextBox).IsReadOnly = True
        End If

    End Sub
 

 

pratixa
pratixa

Member

Member

264 points

88 Posts

Re: Dynamically enable/disable editing in datagrid

 Hi Ken tucker,

                   Actually i have the combobox in the editing..........i just give u my design code...............

                   <data:DataGridTemplateColumn.CellTemplate>
                               <DataTemplate>
                                        <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Stretch">
                                                <TextBlock x:Name="txtFinRespCode" Text="" Margin="3,0,0,0" />
                                                                    <TextBlock x:Name="txtFinRespLvl" Text="" Margin="10,0,0,0" />
                                        </StackPanel>
                               </DataTemplate>
                 </data:DataGridTemplateColumn.CellTemplate>
                 <data:DataGridTemplateColumn.CellEditingTemplate>
                              <DataTemplate>
                                     <ComboBox x:Name="cbFinancialResLvl" Width="200" Height="30"
                                                                  VerticalAlignment="Top" HorizontalAlignment="Left"
                                                                  ItemsSource="{Binding FRL,Source={StaticResource cFRL}}"
                                                                  SelectionChanged="cbFinancialResLvl_SelectionChanged">
                                             <ComboBox.ItemTemplate>
                                                  <DataTemplate>
                                                         <StackPanel Orientation="Horizontal">
                                                                 <TextBlock x:Name="txtFinRespCode" Text="{Binding auto_id,Mode=TwoWay}"/>
                                                                  <TextBlock x:Name="txtFinRespLvl" Text="{Binding mfrl_desc,Mode=TwoWay}" Margin="10,0,0,0" />
                                                          </StackPanel>
                                                  </DataTemplate>
                                              </ComboBox.ItemTemplate>
                                     </ComboBox>
                                </DataTemplate>
                 </data:DataGridTemplateColumn.CellEditingTemplate> 

Mark as answer if it helpful to u....

Thanks,
Pratixa

pratixa
pratixa

Member

Member

264 points

88 Posts

Re: Dynamically enable/disable editing in datagrid

Hi,

    So, i m getting the comboBox as the EdititngElement.........................but does not want to allow the user to double click the cell and go in to edit mode................

Mark as answer if it helpful to u....

Thanks,
Pratixa

Ardman
Ardman

Contributor

Contributor

3320 points

919 Posts

Re: Re: Dynamically enable/disable editing in datagrid

Is it not just worth assigning the IsReadOnly property to true in your XAML on the columns that you don't want users to edit?

pratixa
pratixa

Member

Member

264 points

88 Posts

Re: Re: Dynamically enable/disable editing in datagrid

 Hi ardman,

                  That is y i have not set the  IsReadOnly propertyin XAML............i want to set it run time..dynamically......

Mark as answer if it helpful to u....

Thanks,
Pratixa

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24939 points

2,425 Posts

Microsoft
Answered Question

Re: Dynamically enable/disable editing in datagrid

Hi Pratixa,

Another direct way is to detect the ComboBox's Loaded event and set its IsEnabled/IsHitTestVisible property.

Best regards,

Jonathan

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

sasideva
sasideva

Member

Member

240 points

58 Posts

Re: Dynamically enable/disable editing in datagrid

hi,

Datagrid loading row you can give like this. in loading row you have to enable/disable control .

 

private void Grd_Payroll_LoadingRow(object sender, DataGridRowEventArgs e)

{

CheckBox Chk_Status = Grd_Payroll.FindName("Chk_Status") as CheckBox;

Chk_Status.IsEnabled = false;

}

 

Regards,
Deva

pratixa
pratixa

Member

Member

264 points

88 Posts

Re: Dynamically enable/disable editing in datagrid

 Hi sasideva,

                Thanks for the reply...My combobox is in the celleditTemplate, so i can not find the combo box in the loading_row event....

Mark as answer if it helpful to u....

Thanks,
Pratixa

pratixa
pratixa

Member

Member

264 points

88 Posts

Re: Dynamically enable/disable editing in datagrid

 Hi, jonathan,

                     Thanks for reply..There is no any property like IsReadOnly of the combo box...i have generated the loded event of the combo box in the datagrid's preparecellforedit event.but i m not able find the combo box's IsReadOnly property.

Mark as answer if it helpful to u....

Thanks,
Pratixa

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24939 points

2,425 Posts

Microsoft
Answered Question

Re: Dynamically enable/disable editing in datagrid

Hi Pratixa,

Several alternative ways here.  We can set its IsEnabled or IsHitTestVisible property or detect its SelectionChanged event and change back its original value.

Best regards,

Jonathan

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities