Thursday 17 March 2011

Silverlight: Datagrid binding with Collections(list of dictionaries) dynamically

I am Binding a Datagrid to dynamic data via IDictionary.


It is very simple.


Have a grid in the xaml, 
  <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DataGrid  x:Name="dgBonus" VerticalAlignment="Top" HorizontalAlignment="Left" AutoGenerateColumns="False">
            
        </sdk:DataGrid>
    </Grid>
Bind the path and converter of each column dynamically through code behind as shown below...
 private void BindToGrid(ObservableCollection<Equipment> lstequips)
        {
            List<string> grdHeaders = new List<string>();

            grdHeaders = GenerateHeaders(LstEquipments);

            foreach (var columnName in grdHeaders)
            {
                dgBonus.Columns.Add(
                    new DataGridTextColumn
                    {
                        Header = columnName,
                        Binding = new Binding()
                        {
                            Converter = new RowIndexConverter(),
                            ConverterParameter = columnName
                        }
                    });
            }

        }
        private List<string> GenerateHeaders(ObservableCollection<Equipment> lstequips)
        {
            List<string> grdHeaders = new List<string>();

            foreach (var equip in lstequips)
            {
                foreach (var prop in equip.Property(equip).Keys)
                {
                    if (!grdHeaders.Contains(prop))
                    {
                        grdHeaders.Add(prop);
                    }
                }
            }
            return grdHeaders;
        }
My Data structure:
/////////////////////////////////////////////////
    public class Equipment
    {
        private Dictionary<string, string> _property = new Dictionary<string, string>();


        public Dictionary<string, string> Property(Equipment e) 
        {
            return e._property;
        }


        public string this[string index]
        {
            get 
            {
                try
                {
                    return _property[index];
                }
                catch
                {
                    return string.Empty;
                }
            }
            set { _property[index] = value; }
        }
    }
/////////////////////////////////////////////////
SOURCE CODE:
    public class Equipment
    {
        private Dictionary<string, string> _property = new Dictionary<string, string>();


        public Dictionary<string, string> Property(Equipment e) 
        {
            return e._property;
        }


        public string this[string index]
        {
            get 
            {
                try
                {
                    return _property[index];
                }
                catch
                {
                    return string.Empty;
                }
            }
            set { _property[index] = value; }
        }
    }
    public partial class MainPage : UserControl
    {
        ObservableCollection<Equipment> LstEquipments { get; set; }


        public MainPage()
        {
            InitializeComponent();


            LstEquipments = new ObservableCollection<Equipment>();


            Equipment e3 = new Equipment();
            e3["p1"] = "Val1";
            e3["p2"] = "Val2";
            e3["p3"] = "Val3";
            e3["p4"] = "Val4";
            e3["p5"] = "Val5";
            e3["p6"] = "Val6";
            e3["p7"] = "Val7";


            Equipment e1 = new Equipment();
            e1["p1"] = "Val1";
            e1["p2"] = "Val2";
            e1["p3"] = "Val3";
            e1["p4"] = "Val4";
            e1["p5"] = "Val5";


            Equipment e2 = new Equipment();
            e2["p1"] = "Val1";
            e2["p2"] = "Val2";
            e2["p3"] = "Val3";
            e2["p4"] = "Val4";
            e2["p5"] = "Val5";
            e2["p6"] = "Val8";


            LstEquipments.Add(e3);


            LstEquipments.Add(e1);
            LstEquipments.Add(e2);


            BindToGrid(LstEquipments);
            dgBonus.ItemsSource = LstEquipments;


        }
        private void BindToGrid(ObservableCollection<Equipment> lstequips)
        {
            List<string> grdHeaders = new List<string>();


            grdHeaders = GenerateHeaders(LstEquipments);


            foreach (var columnName in grdHeaders)
            {
                dgBonus.Columns.Add(
                    new DataGridTextColumn
                    {
                        Header = columnName,
                        Binding = new Binding()
                        {
                            Converter = new RowIndexConverter(),
                            ConverterParameter = columnName
                        }
                    });
            }


        }
       private List<string> GenerateHeaders(ObservableCollection<Equipment> lstequips)
        {
            List<string> grdHeaders = new List<string>();


            foreach (var equip in lstequips)
            {
                foreach (var prop in equip.Property(equip).Keys)
                {
                    if (!grdHeaders.Contains(prop))
                    {
                        grdHeaders.Add(prop);
                    }
                }
            }
            return grdHeaders;
        }
   public class RowIndexConverter:IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            Equipment row = value as Equipment;
            string index = parameter as string;
            return row[index];
        }


        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
Hope this helps u....!!!! 
Thank you!! 











2 comments:

  1. Hey, This is raghu from honeywell
    Nice article kamal, It helped me.
    Keep blogging

    ReplyDelete
  2. :) sure Raghu..!! thanks for your encouragement!!

    ReplyDelete