Skip to main content

Silverlight Binding DataGrid with two dimensional array

Set  DataGrid AutoGenerateColumns="False" 



public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            BindDataGrid(GetTwoDimensinalArray());
        }
 
        string[,] GetTwoDimensinalArray()
        {
            //Generating two dimensional array
            string[,] returnValue = new string[5, 5];
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    returnValue[i,j] = "cell [" + i.ToString() + "," + j.ToString() + "]";
                }
            }
 
            return returnValue;
        }
 
        void BindDataGrid(string[,] _array)
        {
            ObservableCollection<ObservableCollection<string>> dataSource = new ObservableCollection<ObservableCollection<string>>();
            //converting two dimensional array into ObservableCollection
            for (int i = 0; i < 5; i++)
            {
                ObservableCollection<string> row = new ObservableCollection<string>();
                for (int j = 0; j < 5; j++)
                {
                    row.Add(_array[i, j]);
                }
                dataSource.Add(row);
            }
 
            for (int i = 0; i < 5; i++)
            {
                //Creating Datagrid columns and binding them to their apropriate columns
                DataGridTextColumn dataColumn = new DataGridTextColumn();
                dataColumn.Header = "Column "+i.ToString();
                dataColumn.Binding = new Binding("[" + i.ToString() + "]");
                dataGridSample.Columns.Add(dataColumn);
            }
            //Binding DataGrid with collection
            dataGridSample.ItemsSource = dataSource;
        }
    }

Comments

  1. How can I add the "PresentationFrameword.dll" to the Silverlight application to use the "Binding" method ?

    ReplyDelete
    Replies
    1. Here you have your answer
      http://social.msdn.microsoft.com/Forums/silverlight/en-US/d884e231-9ee9-4371-89f4-627569515545/agclr-and-presentationframework-clashing-over-building-or-designer-working?forum=silverlightdevtools

      Delete

Post a Comment

Popular posts from this blog

How to Close/Resolve Case in Power Automate Flow

 How to Close/Resolve case from Power automate flow from your DataVerse Connector Add a New Row Table Name: Case Resolutions Subject: Add your subject here Case (Cases): incidents({Case Id Guid})

Azure DevOps Fork Repos between two Organization

  Azure DevOps Fork Repos between two Organization This weekend I embarked on a topic a bit foreign to myself, on the surface seemed a bit simple. The idea is to fork my Azure DevOps Repo to a different organization and push changes between them. Bad news DevOps doesn’t allow to fork Repos to a different organization, you have only the capability to fork to a different Projects within the same Organization. I’m pretty much sure there are so many other ways to solve this issue, but this is the one that I found to be easy to implement. Import Repo to the new Organization Remember here you have to generate a personal token to be able to import the Repo you have to be a member of the new organization as well This might take a few minutes depending on the size of your project, once its complete you will receive a confirmation email, or just refresh it after few minutes So far all looks good, you can clone the solution in your new Organizati...

Using Adaptive Cards on Power Virtual Agent

  Adaptive Card for Power Virtual Agent its on preview, this feature will enable you to enhance your bot with a better visual responses. Here is a demo of a Power Virtual Agent I have embedded on Teams Its quite easy to implement you can use Adaptive Card Designer to generate JSON for your Adaptive Card. You can start by selecting "Ask with Adaptive Card" Here will be able to configure You can Use Adaptive Card Designer for designing your Card, its quite easy to work with. If you are skilled developer you can take advantage of the JSON. This is your JSON template: {     "type" : "AdaptiveCard" ,     "$schema" : "http://adaptivecards.io/schemas/adaptive-card.json" ,     "version" : "1.5" ,     "body" : [] } T