nemo_87
nemo_87

Reputation: 4791

Adding string value to DataGrid in C#?

I'm having a method that takes a xml content from some file, parse it and after that putting some substrings into variables result and resultm. I want to write each value I got in result and resultm during foreach loops. Code that goes through all values and gets substrings looks like this:

var tipList = registers.Select(x => x.Attribute("type").Value);
var mapToList = registers.Select(x => x.Attribute("mapTo").Value);

foreach (var reg in registers)
{
  foreach (var tpl in tipList)
  {
    var end = tpl.IndexOf(',');
    var start = tpl.LastIndexOf('.', (end == -1 ? tpl.Length - 1 : end)) + 1;
    var result = tpl.Substring(start, (end == -1 ? tpl.Length : end) - start);
  }

  foreach (var mpl in mapToList)
  {
    var endm = mpl.IndexOf(',');
    var startm = mpl.LastIndexOf('.', (endm == -1 ? mpl.Length - 1 : endm)) + 1;
    var resultm = mpl.Substring(startm, (endm == -1 ? mpl.Length : endm) - startm);
  }
}

Can anyone help me with an idea how to add values of result and resultm strings in some DataGrid in C#?

Upvotes: 0

Views: 243

Answers (1)

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13794

Here how you can achieve this

/// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ObservableCollection<Tuple<string,string>>  _observableCollection = new ObservableCollection<Tuple<string, string>>();
        public MainWindow()
        {
            InitializeComponent();
        }
        private void MyMethod()
        {
            var tipList = registers.Select(x => x.Attribute("type").Value);
            var mapToList = registers.Select(x => x.Attribute("mapTo").Value);
            List<string> listresult =  new List<string>();
            List<string> listresultm = new List<string>();


            foreach (var reg in registers)
            {
                foreach (var tpl in tipList)
                {
                    var end = tpl.IndexOf(',');
                    var start = tpl.LastIndexOf('.', (end == -1 ? tpl.Length - 1 : end)) + 1;
                    var result = tpl.Substring(start, (end == -1 ? tpl.Length : end) - start);
                    listresult.Add(result);
                }

                foreach (var mpl in mapToList)
                {
                    var endm = mpl.IndexOf(',');
                    var startm = mpl.LastIndexOf('.', (endm == -1 ? mpl.Length - 1 : endm)) + 1;
                    var resultm = mpl.Substring(startm, (endm == -1 ? mpl.Length : endm) - startm);
                    listresultm.Add(resultm);
                }
                int maxLenList = Math.Max(listresult.Count, listresultm.Count);
                for (int i = 0; i <maxLenList; i++)
                {
                    if (i < listresult.Count && i<listresultm.Count)
                    {
                        _observableCollection.Add(new Tuple<string, string>(listresult[i],listresultm[i]));
                    }
                    else if(i>=listresult.Count)
                    {
                        _observableCollection.Add(new Tuple<string, string>(string.Empty, listresultm[i]));
                    }
                     else if(i>=listresultm.Count)
                    {
                        _observableCollection.Add(new Tuple<string, string>( listresult[i],string.Empty));
                    }
                }
            }
            dataGrid1.ItemsSource= _observableCollection;  
        }

Upvotes: 1

Related Questions