I have a DataGrid in my wpf app
<DataGrid Name="datagrid2" ItemSource="{Binding}" CanUserReorderColumns="False"
IsReadOnly="True" SelectionMode="Single" CanUserResizeColumns="False"
CanUserResizeRows="False" LoadingRow="datagrid2_LoadingRow" />
and I am providing its ItemSource as
datagrid2.ItemSource = mydatatable.DefaultView;
and its rowheader as
private void datagrid2_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = Some_string_araay[e.Row.GetIndex()];
}
Sometimes my problem arises that rowheader becomes first column’s data. Hence the last column and its data becomes headerless. I thought it was a layout problem, so after providing ItemSource and in LoadingRow I do datagrid2.UpdateLayout(). But the problem remains the same.


When I click on any ColumnHeader, data gets aligned correctly.


What could be the reason and solution for this problem?
Ok, I think I know why this is happening.
The first column(having your row headers) width is determined at run-time based on its contents(row header data) when grid loads. Now in your case when grid loads your row headers have no data(you set header in
LoadingRowevent) so the width of first column gets set to 0; Once you update the row headers it doesn’t get reflected asDataGriddoesn’t refreshes itself.Once you click on a column header it recalculates the
RowHeaderwidth and this time it is correct as your row headers have data.There should be some easy solution to this but one way to do this can be to bind your
RowHeaderWidthwith the SelectAllButton(in 0,0, cell) like this –I have done something similar to change the first column’s widht based on 0,0’th cell data and it works fine; Hope this will work for you.