1

i have a table in my database that indicate a "rendez-vous". I have a MA and Hours columns. The MA's column is used to indicate if it's the morning or the afternoon: 1 or 2, 1 for morning, 2 for afternoon. (I can't change the database).

Here is a sample of a result:

Hours | MA |Name
10:00:00 | 1 | Robert
10:30:00 | 1 | Jane
11:15:00 | 1 | John
14:00:00 | 2 | Stewart
14:15:00 | 2 | Elton

I put it in a datatable:

dt = myCon.SelectDataTable(query);

Then i binded it to the list:

lstGCCCandidatCreneau.ItemsSource = dt.DefaultView;

But before to do that i would like to insert a blank row between the morning and the afternoon. I would like something like this displayed in the list:

Hours | MA |Name
10:00:00 | 1 | Robert
10:30:00 | 1 | Jane
11:15:00 | 1 | John

14:00:00 | 2 | Stewart
14:15:00 | 2 | Elton

Can i have your help please?

Thanks a lot.

Alex

Alexking2005
  • 325
  • 1
  • 4
  • 16

2 Answers2

3

May be you dont want to add an empty row , and just use grouping in listbox. You can see a sample here

It may cause lot of other issues, in case you want to use this elsewhere !!!

adityaswami89
  • 573
  • 6
  • 15
  • i have a problem, in my case this is not a property. It's a column from a database. VS display an error: "An unhandled exception of type 'System.NullReferenceException' occurred in MyApp.exe". – Alexking2005 Oct 09 '14 at 09:36
  • Arent you creating a Model class which replicates your Database so that you can dump the data when you get it . So ideally you should be using MVVM pattern where in you have a Model class with all the properties similar to your DB and your ViewModel should have a list of Model objects. So you can fill in your List with this and then bind the listbox to this collection . – adityaswami89 Oct 09 '14 at 09:41
  • i really like the solution you give, but MVVM is totally unknown to me and i can't rewrite the app. It's a pity. – Alexking2005 Oct 09 '14 at 09:54
  • Ohh, still you dont need to change the entire app. Only for this scenario also it would work !!! You can get a brief idea about MVVM : http://adityaswami89.wordpress.com/2013/07/07/simple-mvvm-with-wpf/ Still if you want to use without MVVM: you can convert your datatable to list using :http://www.codeproject.com/Tips/784090/Conversion-Between-DataTable-and-List-in-Csharp and bind your listbox to this list . It would work !!! :) – adityaswami89 Oct 09 '14 at 10:05
  • Thanks, i'll watch these links. – Alexking2005 Oct 09 '14 at 10:21
0

Check this link

        DataTable dt = SampleData();
        int i = dt.Rows.Cast<DataRow>().TakeWhile(dr => dr != null && dr[1].ToString() != "2").Count();
        DataRow toInsert = dt.NewRow();
        dt.Rows.InsertAt(toInsert, i);

        public DataTable SampleData()
        {
            DataTable dt=new DataTable();
            dt.Columns.Add("Hours",typeof(string));
            dt.Columns.Add("MA",typeof(string));
            dt.Columns.Add("Name",typeof(string));
            dt.Rows.Add(new object[] {@"10:00:00", 1, "Robert"});
            dt.Rows.Add(new object[] {@"10:30:00", 1, "Jane"});
            dt.Rows.Add(new object[] {@"11:15:00", 1, "John"});
            dt.Rows.Add(new object[] {@"14:00:00", 2, "Stewart"});
            dt.Rows.Add(new object[] {@"14:15:00", 2, "Elton"});
            return dt;

        }
Community
  • 1
  • 1
Prasanth V J
  • 1,126
  • 14
  • 32
  • Thanks, it works for me, except when my DataTable don't contain rendez-vous in the afternoon, i have an empty row at the top of the list. – Alexking2005 Oct 09 '14 at 09:49
  • sorry i wrote an error: i mean: except when the datatable contain only rendez vous in the afternoon. – Alexking2005 Oct 09 '14 at 09:56