Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to var sql command variable convert to datatable variable.

How to query convert to datatable treeview item.
For example:
items[Name]
items[Surname]

I want to take the fields related to the query and keep them in the datatable and throw it in the treeview item. But I want to add the datatable, but it gets empty. How can I add.

Forexample:

Name Surname
Alex J
Michael K

items is 2 value. (Alex J and Michael K) But not set treeview item.object reference not instance of an object


Note: I want to this line DataTable but not convert to Datatable because error is red.
foreach(var items in query);

I want to

foreach(DataTable items in query);

What I have tried:

C#
VMStudent.cs

public void GetStudents(){
var query="";   //linq queries
DataTable newTbl=new DataTable();
newTbl.Columns.Add("Name");
newTbl.Columns.Add("Surname");
foreach(var items in query);   
{
   newTbl.Rows.Add(items);
   Students std=new Students();
   std.Name=items["Name"].ToString();   // items is error and red color.
   std.Surname=items["SurnameName"].ToString(); // items is error and red color.
   StudentList.Add(std);

2.way try 


   newTbl.Rows.Add(items);
   Students std=new Students();
   std.Name="Name".ToString();   
   std.Surname=items"Surname".ToString(); //  name and surname is equal to not values.Equals is Name=Name and Surname=surname 
   StudentList.Add(std);

}
}



StudentsCall.cs


StudentList=GetStudents(); object reference not instance of an object

Student.cs

private string Name;
private string Surname
Posted
Updated 30-Oct-19 10:04am
v3

1 solution

I would recommend using data binding with an ObservableCollection, see CodeProject article here: Simplifying the WPF TreeView by Using the ViewModel Pattern[^]
Also see: WPF : How to Fill TreeView from DataBase[^]

To convert a datatable see: c# - DataTable to observable collection - Stack Overflow[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900