0

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#?

4
  • how you would like to see the output side by side or in the some column Commented Feb 4, 2014 at 10:20
  • I was thinking that side by side would be cooler, but same column will also be good. Commented Feb 4, 2014 at 10:22
  • take a look at my answer Commented Feb 4, 2014 at 10:33
  • sorry it's not datacontext it's itemsSource Commented Feb 4, 2014 at 10:35

1 Answer 1

1

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;  
        }
Sign up to request clarification or add additional context in comments.

5 Comments

One question, in: public partial class MainWindow : Window Window is from namespace System.Windows.Window, right?
right you should not copy all the code just the code between the the first { and the last }
Mine xaml window is define like this: public partial class CreateAreaDialogWindow : System.Windows.Window { ObservableCollection<Tuple<string, string>> _obsCollection = new CreateAreaDialogWindow(); But I get an error for new CreateAreaDialogWindow(); Error 1 Cannot implicitly convert type 'SEDMS.VisualStudioPackage.Dialogs.CreateAreaDialogWindow' to 'System.Collections.ObjectModel.ObservableCollection<System.Tuple<string,string>>' Will your code work for Area?
it still has same error. Error 1 Cannot implicitly convert type 'SEDMS.VisualStudioPackage.Dialogs.CreateAreaDialogWindow.MainWindow' to 'System.Collections.ObjectModel.ObservableCollection<System.Tuple<string,string>>' C:\Users\nemanja.mosorinski\Downloads__Research-master__Research-master\SEDMSVSPackage\VisualStudioPackage\Dialogs\CreateAreaDialogWindow.xaml.cs 31 74 VisualStudioPackage
Damn I'm so stupid...Rookie mistake :( I will try it now. Sorry for being stupid, I'm a beginner :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.