6

I'm trying to add data to a datagrid (in fact, any control that presents data in a grid will do), but the columns (both names and numbers) are not known until runtime.

The columns I DO know how to create: E.g

DataGridTextColumn textColumn = new DataGridTextColumn();
textColumn.Header = column.DisplayName;
MyDataGrid.Columns.Add(textColumn);

But how do I add rows? I don't see how I can use binding because my data is not contained in an object with known properties. For example, data for each row might come in as a string[]. So one time I might have three columns, another time I might have five.

I was expecting do be able to do something like this:

// Example data to represent a single row.
string[] row1 = new[] { "value1", "value2", "value3" };

var row = new Row;
row.AddCell(row1[0]);
row.AddCell(row1[1]);
row.AddCell(row1[2]);
MyDataGrid.Rows.Add(row);
1
  • Binding works with indexes - you can create a column whose binding expression points to an index of the data source if you want. You could also use a dictionary for each row and use a string as the key - then your binding could use the column name on the dictionary to fetch the value rather than a numeric index Commented May 18, 2012 at 10:35

2 Answers 2

13

I'd have to start plugging away in VS to get my head round the exact code, but you can most likely just create your columns and use the column key as the binding expression seeing as indexed bindings work in WPF

I'll get some code up in a minute - but it would look something like your row creation code but with bindings on the columns that look something like (forgive the possibly incorrect method names)

textColumn.Bindings.Add(new Binding("this[" + columnIndex.ToString() + "]"));

Update:

Yeah not sure if this is what you are looking for but it works:

Created a single window with a datagrid on it (dataGrid1)

 public MainWindow()
    {
        InitializeComponent();

        var col = new DataGridTextColumn();
        col.Header = "Column1";
        col.Binding = new Binding("[0]");
        dataGrid1.Columns.Add(col);

        col = new DataGridTextColumn();
        col.Header = "Column2";
        col.Binding = new Binding("[1]");
        dataGrid1.Columns.Add(col);

        col = new DataGridTextColumn();
        col.Header = "Column3";
        col.Binding = new Binding("[2]");
        dataGrid1.Columns.Add(col);

        //dataGrid1.ad

        List<object> rows = new List<object>();
        string[] value;

        value = new string[3];

        value[0] = "hello";
        value[1] = "world";
        value[2] = "the end";
        rows.Add(value);

        dataGrid1.ItemsSource = rows;
    }

Example

Sign up to request clarification or add additional context in comments.

2 Comments

Cool, I didn't know you could bind on an index. Works very well, thank you!
I have been looking for answers all over SO and this is by far the most simple and straight forward solution I have seen for this. Thank you. -- As an added note to other people who use this solution, make sure that the 'AutoGenerateColumns' property for the DataGrid is set to 'False'.
-1

Haven't played with datagrids a lot but you can try something like this

int currentRow = MyDataGrid.Rows.Add();

MyDataGrid.Rows[currentRow].Cells[0].Value = row1[0];  
MyDataGrid.Rows[currentRow].Cells[1].Value = row1[1];
MyDataGrid.Rows[currentRow].Cells[2].Value = row1[2];

1 Comment

System.Windows.Controls.DataGrid doesn't have a Rows property. My 'code' was for illustration only.

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.