0

I want to add a column programatically to my datagrid and bind it.

I have tried following examples on the web but cant get the data do be displayed.

I did have it working with columns declared in the xaml.

Here is the datagrid Xaml declaration:

<DataGrid Grid.Row="0" x:Name="dtgMain" AutoGenerateColumns="False" ItemsSource="{Binding}" RowHeight="25" SizeChanged="dtgMain_SizeChanged"></DataGrid>

Here is where I set the datacontext for the datagrid:

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.LoadXml(response);
                provider.Document = doc;
                provider.XPath = "/moo/response/data/load/panel";
                dtgMain.DataContext = provider;

Here is where I add the column:

DataGridTextColumn dc = new DataGridTextColumn();
            dc.Binding = new Binding("panelCode");
            dc.Header = "Job Number";
            dtgMain.Columns.Add(dc);

And here is an example of the xml:

<moo>
  <response>
    <data>
      <load count="2">true
        <panel index="10">
          <panelCode>100072
          </panelCode>
        </panel>
      </load>
    </data>
  </response>
</moo>

1 Answer 1

3

Your column binding is not using XPath when it should it seems to me.

dc.Binding = new Binding() { XPath = "panelCode" };
Sign up to request clarification or add additional context in comments.

4 Comments

like I said this code was working fine when the columns were declared in the xaml. So im guessing the issue Is something to do witht eh way im declaring the binding on the datacolumn. I just tried this and it still does not work.
I assumed that since I use Xpath to set my datacontext that the binding of the datacolumn would start in the xml from that point? So would it be something like dc.Binding = new Binding("/moo/response/data/load/panel/panelCode");
@user589195: Check out debugging data bindngs by the way.
@user589195: See my edit. Your source object in every row is an XmlElement, if you want to bind xml you need XPath.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.