DataGrid AutoGenerateColumns = "True" - how to add one additional column?

I am using a DataGrid with AutoGenerateColumns = "True". Now, in addition to the auto-generated columns, I want to add one of my “custom” columns, the so-called “service” column. (in it I want to have several hyperlinks "Start" "Stop" "Reset").

How to add an add column?

I found this page http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.autogeneratecolumns.aspx , which describes how to change or delete a column, but I can not find how to add a column .

+5
source share
3

, . .

, WinForms. , XAML:

    <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Src}" x:Name="Grid">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn Header="Junk"></DataGridCheckBoxColumn>
            <DataGridHyperlinkColumn Header="Junk2"></DataGridHyperlinkColumn>
        </DataGrid.Columns>
    </DataGrid>

ViewModel:

public class ViewModel
{
    public ViewModel()
    {
        Src = new ObservableCollection<Item>() { new Item { Id = 1, Name = "A" }, new Item { Id = 2, Name = "B" } };
    }

    public ObservableCollection<Item> Src { get; set; }
}

public class Item{
    public int Id { get; set; }
    public string Name { get; set; }
}

:

enter image description here

+7

XAML <DataGrid.Columns> .

, XAML, , .. .

AutoGeneratedColumns, , .

+3

, ?

if so, the easier it will be to add a column in the Visual Studio GUI, set the name and name of the DataPropertyName to what you can recognize, and then add this column to the DataSet.

//Assumes you added a column that you named 'clNew'
//Assumes you have one table in your dataset that the DGV is bound to
clNew.Name = "clNew";
clNew.DataPropertyName = "clNew";

ds.Tables[0].Columns.Add("clNew");

The column is now in the dataset, so if you iterate through datarows, you can also access it.

0
source

All Articles