Monday, September 17, 2012

Datagrid all rows textbox while Edit button

Datagrid all rows textbox while Edit button

Hi,

When I click on edit button, in datagrid all rows should display textbox with data. please give me some examples.

Thanks
Suman G

Answers & Comments...

Answer: 1

Hi Suman,

                 You can make use of GridTemplate column with Textbox template.

Below is the link for your reference.

http://blogs.msdn.com/b/scmorris/archive/2008/03/27/defining-columns-for-a-silverlight-datagrid.aspx

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtemplatecolumn.celleditingtemplate(v=vs.95).aspx

Regards,

Karthikeyan



Answer: 2

Hi Suman,

Are you mean you want all rows in the datagrid enter Edit mode in one edit button click? By default all DataGridViewEditMode values except for EditProgrammatically also allow a user to double-click a cell to begin editing it. So your idea is not recommended.

But you can begin editing one cell in datagrid by click one edit button like this:

    <UserControl.Resources>          <local:Customers x:Key="customers"></local:Customers>      </UserControl.Resources>      <Grid x:Name="LayoutRoot" Background="White">          <Grid.RowDefinitions>              <RowDefinition Height="Auto"/>              <RowDefinition/>          </Grid.RowDefinitions>           <Button Name="EditDataGrid" Content="Edit current selected cell" Click="EditDataGrid_Click"></Button>           <data:DataGrid AutoGenerateColumns="False" Grid.Row="1" Height="Auto" HorizontalAlignment="Left" Margin="10,10,0,0"                         Name="dataGrid1" VerticalAlignment="Top" Width="Auto" ItemsSource="{StaticResource customers}">              <data:DataGrid.Columns>                  <!-- Name Column -->                  <data:DataGridTemplateColumn Header="Name">                      <data:DataGridTemplateColumn.CellTemplate>                          <DataTemplate>                              <StackPanel Orientation="Horizontal" VerticalAlignment="Center">                                  <TextBlock Padding="5,0,5,0"                              Text="{Binding FirstName}" Width="Auto"/>                                  <TextBlock Text="{Binding LastName}" Width="Auto"/>                              </StackPanel>                          </DataTemplate>                      </data:DataGridTemplateColumn.CellTemplate>                      <data:DataGridTemplateColumn.CellEditingTemplate>                          <DataTemplate>                              <StackPanel Orientation="Horizontal">                                  <TextBox Text="{Binding FirstName,Mode=TwoWay}" BorderThickness="0" Width="Auto"/>                                  <TextBox Text="{Binding LastName,Mode=TwoWay}" BorderThickness="0" Width="Auto"/>                              </StackPanel>                          </DataTemplate>                      </data:DataGridTemplateColumn.CellEditingTemplate>                  </data:DataGridTemplateColumn>                  <!-- Address Column -->                  <data:DataGridTextColumn              Header="Address" Width="Auto"              Binding="{Binding Address}" />              </data:DataGrid.Columns>          </data:DataGrid>      </Grid>  
        private void EditDataGrid_Click(object sender, RoutedEventArgs e)          {              dataGrid1.BeginEdit();          }
    public class Customer      {          public int ID { get; set; }          public String FirstName { get; set; }          public String LastName { get; set; }          public String Address { get; set; }            public Customer() { }            public Customer(int id, String firstName, String lastName, String address)          {              this.ID = id;              this.FirstName = firstName;              this.LastName = lastName;              this.Address = address;          }      }      public class Customers : ObservableCollection<Customer>      {          public Customers()          {              Add(new Customer(1, "Michael", "Anderberg",                      "12 North Third Street, Apartment 45"));              Add(new Customer(2, "Chris", "Ashton",                      "34 West Fifth Street, Apartment 67"));              Add(new Customer(3, "Cassie", "Hicks",                      "56 East Seventh Street, Apartment 89"));              Add(new Customer(4, "Guido", "Pica",                      "78 South Ninth Street, Apartment 10"));          }      }

Hope this will help you.
Best Regards,





No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog