-1

Hello sir i am not native english, new here in stackoverflow and new in programming but i will try my best to share my problem with you: I added some comments in my code so i hope you can better see what the problems are i am trying to make something like a temporary datatable that gets informations (only the rows matter) from 1 specific datatable(there will be more see in the code) and the "temporarydatatable" gives these to a list<> i tried it with linq. ofc i have my own mind and tried to change it a way i understand (LINQ query on a DataTable this wasnt really helpful for me :X ) and i tried some other things as well but i dont want to smash 10 links here :P so here comes the code:

public MainWindow()
{
    InitializeComponent();
    datatable1();

}

public void datatable1()
{
    /*This Table should get the informations from datatable_1 or
    another one (there will be some more tables and the viewtable will 
    get the informations from the table where the 
    Type ==(i guess it will be a combobox) selected Type */
    DataTable viewtable = new DataTable();
    viewtable.Columns.Add("Typ", typeof(string));
    viewtable.Columns.Add("Name", typeof(string));
    viewtable.Columns.Add("Anzahl", typeof(string));
    viewtable.Columns.Add("Zeit", typeof(string));
    /*here is the main problem i have*/     
    viewtable.Rows =from _Row1 in datatable_1 where "Typ" =="Una";
    /*it "worked" like this so i get the informations in my list*/    
    viewtable.Rows.Add("Una", "Testschrank2", "9000", "0:20:30");

    //this table is a example table holding the informations 
     DataTable datatable_1 = new DataTable();
     datatable_1.Clear();
     datatable_1.Columns.Add("Typ");
     datatable_1.Columns.Add("Name");
     datatable_1.Columns.Add("Anzahl");
     datatable_1.Columns.Add("Zeit");
     DataRow _Row1 = datatable_1.NewRow();
     datatable_1.Rows.Add("Una", "Testschrank2", "9000", "0:20:30");
    // _Row1["Zeit"] = (4, 30, 0);
    datatable_1.Rows.Add(_Row1);`
}

well i guess i added too much code but like i said i am really new to this so its a bit difficult for me to point on my problem with little code excuse me sir and thanks for your help o/

Community
  • 1
  • 1

1 Answers1

3

To get the value from the first DataTable you have to pull all DataRows from it as Lei Yang suggested in his comment.

DataRow temp = datatable_1.Rows.OfType<DataRow>()
                          .SingleOrDefault(x=>x["Typ"].ToString() == "Una");

1: You cannot assign to the property Rows since it is readonly.

2: You cannot just use simply viewtable.Rows.Add(temp) because this row already belongs to another table. This will result in a System.ArgumentException

So you need to import the row:

if (temp != null)
{
    viewtable.ImportRow(temp);
}

EDIT:

If you intend to capture more than one row using the where clause you can use a List<DataRow> to save them temporarily and import each row afterwards in a loop:

List<DataRow> temp = datatable_1.Rows.OfType<DataRow>()
                                .Where(x => x["Typ"].ToString() == "Una").ToList();


if (temp.Count > 0)
{
    foreach (var row in temp)
    {
        viewtable.ImportRow(row);
    }
}

EDIT 2:

Here are some sources for further research:

How to: Locate a Specific Row in a DataTable

In this example you can use also the Select method to get the desired rows. This would look like this:

DataRow [] temp2 = datatable_1.Select("Typ ='Una'", "Name DESC", DataViewRowState.Added);

// or the short version:
DataRow [] temp2 = datatable_1.Select("Typ ='Una'");

the outcome will be the same. This version is from a an answer to a similar question.

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • If you expect multiple matches, use `Where` instead of `SingleOrDefault` use `ImportRow` in a loop over the matches. – wkl May 18 '17 at 06:47
  • thanks @ Mong Zhu & Lei Yang it works for now so finally i can continue :P – Pr0gr4mm3r May 18 '17 at 06:52
  • @wkl valid point, thank you, I made an edit to accommodate for this – Mong Zhu May 18 '17 at 06:52
  • oh one more thing @MongZhu have you created this on your own or is there a source you can link? – Pr0gr4mm3r May 18 '17 at 06:59
  • @Pr0gr4mm3r I had the same Idea as Lei Yang, for the import example I did a little research, so I posted the link. There is definitely some source on the internet for this answer, many probably. You just need to search for it – Mong Zhu May 18 '17 at 07:30
  • @Pr0gr4mm3r I edited my answer and included a second option to filter the `DataTable` with some links for further research. – Mong Zhu May 18 '17 at 07:36
  • thanks a lot :) well the problem is that i am really really new and often i dont get it what they try to explain or cant Convert.To myCode :P anyway thanks a lot again you helped me a lot ;) – Pr0gr4mm3r May 18 '17 at 07:51
  • @Pr0gr4mm3r It is difficult in the beginning to find the right keywords for the research. But as you advance in programming you will be able to better articulate what problem you have at hand and especially what you want to do. In this case I would start researching with "c# datatable get specific row " and if your code throws exceptions check the details and search for exactly this text from the exception. Good fortune. – Mong Zhu May 18 '17 at 08:18