Thursday 17 March 2011

Silverlight: Dynamically binding the converter through code behind


Have a grid in the xaml/code behind.

Then proceed as shown below....!

Caution: DO NOT SEND PARAMETER WHEN CREATING BINDING OBJECT. IF YOU DO IT THE DYNAMIC BINDING WITH COLLECTIONS WILL NOT WORK.




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

HOPE IT IS HELPFUL!!!....

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!! 











Friday 11 March 2011

Failed to access IIS metabase


You get this error when running your application or trying to view the web service in browser???

Reason: Your 
Microsoft .NET Framework is not properly installed, not properly configured or not registered with the Microsoft IIS web server.(This happens when your IIS is installed after the installation of Visual Studio development environment or When you have uninstalled and re installed your IIS after installing .net framework)

Solution: Open visual studio command prompt, Goto  C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 and type aspnet_regiis -u then aspnet_regiis -i. (Try doing for all the installed versions)

The XML page cannot be displayed.

Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.

 A name was started with an invalid character.Error processing resource...



You get this error when running your application or trying to view the web service in browser???


Reason: You have not set the asp.net version to run your application in your IIS virtual directory.


Solution:  
run->inetmgr
Goto the virtual directory where you have published the service
Right click ->properties
Goto ASP.NET tab
Select asp.net version appropriately 
Restart the website
Its done