how to remove theextra row at the end of the datagrid and datagrid header width problems

Jun 11, 2009 at 1:32 PM

Hi ,

Need urgent help!!!!

Im a newbie to WPF, i have a problem in the WPF Toolkit Datagrid

1.   An empty row appears as the last row in the datagrid after the grid has binded.

2. Im unable to wrap the header , i need to show the header in two lines.For Example: my header is "Display Value" , i need to show  "Display" in one line and "Value in another line.Im unable to accomplish this one.

Thanks in advance

   Im using the following XAML

  
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="10"></RowDefinition>
<RowDefinition Height="400"></RowDefinition>
<RowDefinition Height="55"></RowDefinition>
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="900"></ColumnDefinition>
</Grid.ColumnDefinitions>

<dg:DataGrid Grid.Row="1" HorizontalAlignment="Left" HorizontalScrollBarVisibility="Hidden" RowHeight="32" Width="800" Margin="55,30,0,0" Name="dgAdminSecurity"  AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" ItemsSource="{Binding}" >

<dg:DataGrid.Columns >
<dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox Name="chkHeader" HorizontalAlignment="Center" Margin="10,0,0,0" VerticalAlignment="Center" Content="" ></CheckBox>
</DataTemplate>
</dg:DataGridTemplateColumn.HeaderTemplate>

<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkRow" HorizontalAlignment="Center" VerticalAlignment="Center" Content="" ></CheckBox>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>

<dg:DataGridTemplateColumn Header="Display" >
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Name="tbDisplayValue" Width="75" Height="20" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Path=Display}"></TextBox>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>

<dg:DataGridTemplateColumn Header="Parent Row" >
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Name="txtParent" Width="75" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding Path=ParentRow}"></TextBlock>
<Button Width="20" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="15,0,0,0">...</Button>
</StackPanel>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>

 


<dg:DataGridTemplateColumn Header="Description" >
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ListBox Name="tbDescription" Height="25" Width="200" VerticalAlignment="Center" HorizontalAlignment="Left" ItemsSource="{Binding Path=Description}"></ListBox>
<Button Width="20" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5,0,0,0">...</Button>
</StackPanel>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>

 


<dg:DataGridTemplateColumn Header="Modified By" >
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Name="txtModfiedBy" Width="150" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding Path=ModifiedBy}"></TextBlock>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>


<dg:DataGridTemplateColumn Header="Modification Date" >
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Name="txtModificationDate" Width="80" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding Path=ModificationDate}"></TextBlock>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>

 


<dg:DataGridTemplateColumn Header="Status" >
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Name="txtStatus" VerticalAlignment="Center" Width="63" HorizontalAlignment="Center" Text="{Binding Path=Status}"></TextBlock>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>

</dg:DataGrid.Columns>
</dg:DataGrid>
</Grid>

Jun 12, 2009 at 7:38 AM

hello sir,

I am trying to solve your problm.

I thik your are binding a object of DataTable to this datagrid. just rename your column name of data table like this. i wrote sample code for you. just read it it will help you.


            DataTable dataTable = new DataTable();

            DataColumn mycolumn = new DataColumn("Hello World",typeof(string));

            dataTable.Columns.Add(mycolumn);

            int ColumnCount = dataTable.Columns.Count;

            for (int i = 0; i < ColumnCount; i++)
            {
                dataTable.Columns[i].ColumnName = dataTable.Columns[i].ColumnName.Replace(" ", System.Environment.NewLine);
            }

    gridViewObject.ItemsSource = dataTable.DefaultView;

 

Thanks.

Jun 12, 2009 at 7:47 AM

and your first problm is very simple.

<my:DataGrid AutoGenerateColumns="False" Height="Auto" ItemsSource="{Binding}" x:Name="GridTable" Width="Auto" ......  CanUserAddRows="False">
just set this property which is bold in code block. there are more properties like 
 
CanUserDeleteRows
CanUserReorderColumns
CanUserResizeColumns
CanUserResizeRows
CanUserSortColumns
 
you can use these also......
 
Thanks...
 
 
Jun 12, 2009 at 7:56 AM

Hi lokeshsony,

Thanks alot for the help. It indeed is very simple also can u kindly tel me how to wrap the header content, i need to show the header for some columns in 2 lines but im unable to wrap the header.Can u let me know regarding this.I really appreciate your help.