Irakli Lekishvili
Irakli Lekishvili

Reputation: 34158

WPF c#, bind datagrid column with code behind

I have question how i can bind datagrid column to collection?

 dataGrid1.Columns.Add(new DataGridTemplateColumn { Header="d", Binding = "RoomNumber"}); 

binding does not exsists what can i use to bind?

it work perfectly but i need to bind it with code behind

 <DataGridTextColumn Header="Room Number" Binding="{Binding RoomNumber}"/>

Upvotes: 8

Views: 41056

Answers (5)

Nir Geffen
Nir Geffen

Reputation: 106

Just wasted a few couple of hours to make this work, adding my starting code as example if someone is having trouble.

Base Model:

public class SampleDataGridLine
{
    public SampleDataGridLine()
    {
        data = new string[6];
        for (int i = 0; i < 6; i++)
            data[i] = "n/a";
    }
    public string[] data;
    public string Cycle { get { return data[0]; } set { data[0] = value; } }
    public string a1 { get { return data[1]; } set { data[1] = value; } }
    public string a2 { get { return data[2]; } set { data[2] = value; } }
    public string a3 { get { return data[3]; } set { data[3] = value; } }
    public string a4 { get { return data[4]; } set { data[4] = value; } }
    public string a5 { get { return data[5]; } set { data[5] = value; } }
}

Code Behind:

int runningIndex;
private void Button_Click(object sender, RoutedEventArgs e)
{
    string text = "a" + runningIndex;

    var col = new DataGridTextColumn();
    col.Header = text;
    col.Binding = new Binding(text);
    dataGrid.Columns.Add(col);

    runningIndex++;
}

UI:

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition/>
  </Grid.RowDefinitions>
  <StackPanel Grid.Row="0" Orientation="Vertical" Margin="20">
    <Button Click="Button_Click" Width="100" Height="30" Content="AddColumn"/>
  </StackPanel>
  <StackPanel Grid.Row="1" Orientation="Vertical" Margin="20">
    <DataGrid x:Name="dataGrid" AutoGenerateColumns="False">
      <DataGrid.Columns>
        <DataGridTextColumn Header="Cycle" Binding="{Binding Cycle}"/>
      </DataGrid.Columns>
    </DataGrid>
  </StackPanel>
</Grid>

Upvotes: 0

Henry Minute
Henry Minute

Reputation: 33

The answer from bathineni (and others) is essentially correct, so long as, at some point you have set the ItemsSource property of the DataGrid to your collection.

Upvotes: 3

Bathineni
Bathineni

Reputation: 3496

As far as i understand you want to add data grid column from code behind and that column should work with binding..?

here is sample snippet to add datagrid column from code behind

 var col = new DataGridTextColumn();
            col.Header = "d";
            col.Binding = new Binding("RoomNumber");
            dataGrid1.Columns.Add(col);

With this approach you can add as many columns as you want and you can give data binding at run time for each column and you can specify itemssource at once....

make sure to mark AutoGenerateColumns="False" in your data grid so that you can avoid unwanted columns get added from itemssource..

Upvotes: 14

Haris Hasan
Haris Hasan

Reputation: 30097

There is no way to bind a single column with a collection. You will have to bind whole DataGrid with an itemssource which can be a collection or anything and then you will bind each column with some part of that collection

Upvotes: 1

xdumaine
xdumaine

Reputation: 10329

Use itemssourceproperty of the datagrid to bind to a collection.

MSDN

Example in xaml:

<DataGrid ItemsSource="{Binding Path=Tracks, IsAsync=True}" AutoGenerateColumns="False" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="Song" Width="*" Binding="{Binding Name}" />
            <DataGridTextColumn Header="Artist"  Width="*" Binding="{Binding Artist}" />
            <DataGridTextColumn Header="Album" Width="*" Binding="{Binding Album}" />
        </DataGrid.Columns>
</DataGrid>

example in code behind:

dataGrid1.ItemsSource = tracks;

Upvotes: 0

Related Questions