how to Set the position of Column in datagrid wpf
i have a problem i m making a wpf application and using data grid Control to Show tabular data i m adding a Single Coloum in data Grid through Xaml And My All Other Column Would be add Dynamic my Fixed Column Comes At first postion in Grid i Want to be it to be come At last
how can i achive it
my Xaml code is As below
Thanks in Advance shashank`
<my:DataGrid.Columns>
<my:DataGridTemplateColumn Header="Edit" Width="110" IsReadOnly="True" >
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="file:///D:\Projects\ChadCarpenter\AARFID\AARFID\Images\boardicon.png" />
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
And below are my C #` public void CreateDataTableheader() {
if (Utility.CheckGroupFamily == "Group")
{
dtAdultinfo.Columns.Add("Group Name", typeof(String));
}
dtAdultinfo.Columns.Add("Last Name", typeof(String));
dtAdultinfo.Columns.Add("First Name", typeof(String));
dtAdultinfo.Columns.Add("Phone", typeof(String));
dtAdultinfo.Columns.Add("CellPhone", typeof(String));
dtAdultinfo.Columns.Add("TowerID", typeof(String));
dtAdultinfo.Columns.Add("Room #", typeof(String));
dtAdultinfo.Columns.Add("IS primary", typeof(int));
Lst.ItemsSource = dtAdultinfo.DefaultView;
//DataGridColumn dc;
//DataGridTemplateColumn dgColumn = new DataGridTemplateColumn();
//DataTemplate dt = new D开发者_StackOverflowataTemplate();
//dgColumn.Header="Edit";
//dgColumn.Width = 110;
//dgColumn.IsReadOnly= true;
//dgColumn.CellTemplate = dt;
//Image img = new Image();
//BitmapImage logo=new BitmapImage();
//logo.UriSource =new Uri("file:///D:/Projects/ChadCarpenter/AARFID/AARFID/Images/boardicon.png");
DataGridTemplateColumn dgColumn = new DataGridTemplateColumn();
dgColumn.Header = "Edit";
FrameworkElementFactory factory1 = new FrameworkElementFactory(typeof(Image));
Binding b1 = new Binding("Picture");
b1.Mode = BindingMode.TwoWay;
factory1.SetValue(Image.SourceProperty, b1);
DataTemplate cellTemplate1 = new DataTemplate();
cellTemplate1.VisualTree = factory1;
dgColumn.CellTemplate = cellTemplate1;
Lst.Columns.Add(dgColumn);`
It might be late but in order to help others in similar issues, setting DatagridColumn.DisplayIndex
in AutoGeneratedColumns
event will do the trick.
Assuming Column with Header = "Edit"
is your fixed column and your DataGrid
looks like this:
<DataGrid AutoGeneratedColumns="DataGrid_AutoGeneratedColumns">
<DataGrid.Columns>
<my:DataGrid.Columns>
<my:DataGridTemplateColumn Header="Edit">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<... />
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</DataGrid.Columns>
</DataGrid>
Then you can find the desired column by it's header and send it to the last position:
private void DataGrid_AutoGeneratedColumns(object sender, EventArgs e)
{
var grid = (DataGrid)sender;
foreach (var item in grid.Columns)
{
if (item.Header.ToString() == "Edit")
{
item.DisplayIndex = grid.Columns.Count - 1;
break;
}
}
}
精彩评论