Adding array List into a Grid RRS feed

  • Question

  • Hi: I have a project in WPF C#. In my code, I am creating a new object(string object) from a loop and adding that string object in a List. Now in my XAML code I have added a Grid(myGrid). How Can I show the List of my Array objects in this Grid(Like a table). The Grid has 3 columns with heading: "Name", "Old Text", "New Text".

    Some thing Like:

    Public Class myObj


    private string mname,mOldV, mNewV;

    Class myObj(string name, string OldV, NewV)


    mname =name;


    mNewV= NewV;


    }//end of myObj

    Public class Mainclass


    private List<MyObj> lObj=Null;

    public sub asub


    for(i=0, i<3,i++)


    mObj = new MyObj("a",b","c"); //I will change this value

    lObj[i].add =mObj;


    myGrid.DataContext=mObj;//I dont know this one

    }//end of asub

    Qustion: I need the code part of the Grid in XAML to bind the List of myObj and also correct code(above) in C#.

    Advance thanks


    Wednesday, July 24, 2013 2:18 AM


  • <Window x:Class="WpfApplication1.Window1"
            Title="Window1" Height="300" Width="300">    
            <DataGrid x:Name="myGrid" AutoGenerateColumns="True"></DataGrid>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    namespace WpfApplication1
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
            public Window1()
                this.Loaded += new RoutedEventHandler(Window1_Loaded);
            public List<MyObj> lObj = null;
            void Window1_Loaded(object sender, RoutedEventArgs e)
                lObj = new List<MyObj>();
                for (var i = 0; i < 3; i++)
                    var mObj = new MyObj("a", "b", "c");
                myGrid.ItemsSource = lObj;
        public class MyObj
            public string mname { get; set; }
            public string mOldV { get; set; }
            public string mNewV { get; set; }
            public MyObj(string name, string OldV, string NewV)
                mname = name;
                mOldV = OldV;
                mNewV = NewV;

    i modified your code little bit so rewrite your code as given above and let me know if you need any more help.

    you can understand what are the mistakes you did when you compare my code, if you want me to highlight those issues let me know.

    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    • Marked as answer by Lisa Zhu Monday, July 29, 2013 6:30 AM
    Wednesday, July 24, 2013 8:03 AM