locked
Datagrid bindings dynamically RRS feed

  • Question

  • Here i have a question regarding DataGrid binding. My situation is that i'm not sure how many columns can be bound to my datagrid. That will be decided during runtime. Now how do i bind that to my grid. Also i want to customize column heading (or else i may have directly binded itemssource and set AutoGenerateColumns=true). Expecting more generic procedure. Thanks in advance.

     

    Tuesday, June 9, 2009 9:12 AM

Answers

All replies

  •  You can programmatically build the columns on your DataGrid.

    Here's one example: 

    Programmatically create combo box template column in datagrid

    Tuesday, June 9, 2009 1:13 PM
  • Do you then at runtime always have the same number of columns per row? Or can each row have a different number of columns?
    Wednesday, June 10, 2009 4:55 AM
  • Nope! every row will hold same number of columns.

    Wednesday, June 10, 2009 5:24 AM
  • can each row have a different number of columns?
     

     

    By definition, a grid has the same number of columns for every row. :)

     

    Mark

     

    Wednesday, June 10, 2009 12:25 PM
  • @msalbury: I'm so sorry that my english isn't as good as yours.

     

    @vemuri.kishore: Take a look at this post, there even are used lists with different count to be bound to a datagrid row.

     

    Friday, June 12, 2009 9:22 AM
  • If you are binding objects, then there is no need to specify any column definition. Its all that depends upon your binding source.Could u please tell me, what kind of binding source you are going to use?.

     

    Regards,

    Bala.

    Friday, June 12, 2009 11:26 AM
  • @msalbury: I'm so sorry that my english isn't as good as yours.
     

     

    Huh?  I don't recall replying to you or saying anything about English :)

    Cheers,
    Mark

     

    Friday, June 12, 2009 12:03 PM
  • sorry for late reply. I found solution for that. write a function that accepts the column to be added as a parameter and then using static resource converter function we'll get the value of that property(column) that will be binded to column of data grid.

    Tuesday, June 16, 2009 12:56 AM
  •  public void txtclmnMethod(string txtClmn)
     {
        dgtxtclmnExtra = new DataGridTextColumn();
        dgtxtclmnExtra.Visibility = Visibility.Visible;
        dgtxtclmnExtra.Header = txtClmn;
        RowIndexConverter convert = new RowIndexConverter();
        dgtxtclmnExtra.Binding = new Binding { Converter = convert, ConverterParameter = txtClmn };
        _dataGrid.Columns.Add(dgtxtclmnExtra);
     }

     


            public object Convert(object value, Type targetType,
                object parameter, CultureInfo culture)
            {
                // obtain the 'bound' property via the Row string indexer
                Row row = value as Row;
                string index = parameter as string;
                object propertyValue = row[index];

                // convert if required
                if (_valueConverter != null)
                {
                    propertyValue = _valueConverter.Convert(propertyValue, targetType, parameter, culture);
                }

                return propertyValue;
            }

    Tuesday, June 16, 2009 1:14 AM