This is a migrated thread and some comments may be shown as answers.

How to set width of edit field in GridViewDataColumn

3 Answers 330 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Roger
Top achievements
Rank 1
Roger asked on 14 Jan 2011, 11:09 PM
I have an editable RadGridView with set column widths.  When the user clicks a cell to edit its contents, the editing TextBox appears to be much more narrow than the width of its column.  How can I change this so that the TextBox stretches to the column width?

I tried using a template for the cell but this prevented the cell/row validation from working:
<DataTemplate x:Name="editCellTemplate">
    <Grid>
        <TextBox MaxLength="100" Margin="5,3"
                 Width="{Binding ActualWidth,
                            RelativeSource={RelativeSource
                               Mode=FindAncestor,
                               AncestorType={x:Type telerik:GridViewCell}}}"
                 Text="{Binding Label, Mode=TwoWay}" />
    </Grid>
</DataTemplate>
 
<telerik:RadGridView Margin="10" MinHeight="300" RowHeight="30"
              HorizontalAlignment="Left" VerticalAlignment="Top"
              AutoGenerateColumns="False" IsFilteringAllowed="False"
              CanUserInsertRows="True" CanUserDeleteRows="False"
              CanUserFreezeColumns="False" CanUserReorderColumns="False"
              ShowGroupPanel="False" ShowInsertRow="True"
              ItemsSource="{Binding SourceView, Mode=OneWay}">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn x:Name="Label" Header="Label" Width="300"
                           DataMemberBinding="{Binding Label, Mode=TwoWay}"
                           CellEditTemplate="editCellTemplate" />
        <telerik:GridViewCheckBoxColumn x:Name="Inactive" Header="Inactive"
                             DataMemberBinding="{Binding IsDeleted, Mode=TwoWay}"
                             Width="Auto" />
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

Then I tried setting the CellStyle property, but this had no effect:
<telerik:GridViewDataColumn.CellStyle>
    <Style>
        <Setter Property="TextBox.MaxLength" Value="100" />
        <Setter Property="TextBox.Margin" Value="5" />
        <Setter Property="TextBox.Width"
              Value="{Binding ActualWidth, RelativeSource={RelativeSource
                 Mode=FindAncestor, AncestorType={x:Type telerik:GridViewCell}}}" />
    </Style>
</telerik:GridViewDataColumn.CellStyle>

3 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 17 Jan 2011, 05:28 PM
Hi Roger,

 
Since there is no built-in way to achieve this result, you may handle RadGridView's PreparedCellForEdit event to access the default editing element, as shown below:

private void clubsGrid_PreparedCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
{
 if ((string) e.Column.Header == "Name")
 {
   var tb = e.EditingElement as TextBox;
   tb.Width=200;
 }
}


Please let me know if you need any further assistance.


Kind regards,
Vanya Pavlova
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Yungsung
Top achievements
Rank 1
answered on 21 Jun 2016, 09:07 PM

is there a way for wpf mvvm?

 

0
Stefan
Telerik team
answered on 22 Jun 2016, 11:36 AM
Hello Yungsung,

A possible solution for such requirement is to benefit from the EventToCommandBehavior. Please review the referred help topic for more information.

Hope this fits your requirements.

Regards,
Stefan X1
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
GridView
Asked by
Roger
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
Yungsung
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or