Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Generally speaking I created a code that accesses multiple servers and reads the data from those servers.
Currently I have extracted all the data as a string from the DataTables and display it using ListView, however I was wondering if there was a way to store the data in a array so that I can do basic math operations later.

What I have tried:

for (int i = 0; i < DataTable1.Rows.Count; i++)
{
    var resultTest = DataTable1.AsEnumerable().Select(r => r.Field<string>("Name")).ToArray();
    listView1.Items.Add(resultTest[i].ToString());
}
Posted
Updated 15-Oct-18 23:50pm
Comments
Graeme Cooper 16-Oct-18 5:25am    
Just so you know I'm aware that the code above displays a single column instead of the entire dataset. I'm just having trouble thinking of a possible solution.
Graeme Cooper 16-Oct-18 5:44am    
I'm sorry My follow codeproject user, I'm a very impatient person these days. I have solved my own problem 5mins after posting this question.

Um.
resultTest is an array: you will get exactly the same results if you do this:
string[] resultTest = DataTable1.AsEnumerable().Select(r => r.Field<string>("Name")).ToArray();
So why is this giving you a problem?

But please, start by looking at your code: why are you looping through all rows and converting the whole DataTable each time you go round the loop, only to use a single row, and throw away the rest?
You could just say this:
for (int i = 0; i < DataTable1.Rows.Count; i++)
{
    listView1.Items.Add(DataTable1.Rows[i]["Name"].ToString());
}
and get the same results.

If you want an array of numeric values, then you can just discard the loop, modify the single line of Linq you have and TryParse or Parse the column to a number - but columns called "Name" do not generally contain numeric data!
 
Share this answer
 
Comments
Graeme Cooper 16-Oct-18 6:21am    
Relatively new to c# and linq, so I don't really know the in's and out's. Erm...with regards to: for (int i = 0; i < DataTable1.Rows.Count; i++)
{
listView1.Items.Add(DataTable1.Rows[i]["Name"].ToString());
}
Yes that was the result of one of my attempts however when it came to displaying it using listview, it's appearance wasn't favourable.(wanted the structure to be as close to original sql table as possible)

Apart from that Yes, I will indeed analyse my code better from now on . And Thank you for the help.
OriginalGriff 16-Oct-18 6:31am    
You're welcome!
The way I would do it is to create a class with properties for each of the relevant columns in your DT, and add a constructor that accepts a DataRow. The constructor then converts column data as necessary to the properties, and you can create an array (or better a List) of the class instances. Much more flexible, easier to read the code, and the correct datatypes are ready to use when you need them.
Instead of messing around with trying to shortcut it, move the data into a List of objects. At that point, it will be easier to manipulate. You're spending so much time trying to use the data directly from a DataTable that you're loosing your way.

C#
public class ItemList : List<MyItem>
{
    public ItemList(DataTable dt)
    {
        foreach(DataRow row in dt.Rows)
        {
            this.Add(new MyItem(row));
        }
    }
}

public class MyItem
{
    // properties for each row column go here

    public MyItem(DataRow row)
    {
        // populate the properties from the row
    }
}

// usage

ItemList myItems = new ItemList(DataTable1);


If you want to get a little fancier, you could retrieve the data (from a database, I'm assuming) in the List constructor, which would simplify the outward facing code even more.
 
Share this answer
 
for (int i = 0; i < DataTable1.Rows.Count; i++)
{
    var resultTest1 = DataTable1.AsEnumerable().Select(r => r.Field<string>("column Name")).ToArray();
    var resultTest2 = DataTable1.AsEnumerable().Select(r => r.Field<string>("Column Name")).ToArray();
    var resultTest3 = DataTable1.AsEnumerable().Select(r => r.Field<string>("Column Name")).ToArray();
    var resultTest4 = DataTable1.AsEnumerable().Select(r => r.Field<string>("Column Name")).ToArray();
    listView1.Items.Add(resultTest1[i].ToString() + " " + resultTest2[i].ToString() + " " + resultTest3[i].ToString() + " " + resultTest4[i].ToString());
}


Note:
var resultTest2 = DataTable1.AsEnumerable().Select(r => r.Field<int?>("Column Name")).ToArray();
read column integer values
 
Share this answer
 
v2
Comments
Richard Deeming 16-Oct-18 11:02am    
You're creating an array containing every value of a column for every row, just to extract the value for one row. That's horrendously inefficient!

Try:
for (int i = 0; i < DataTable1.Rows.Count; i++)
{
    var row = DataTable1.Rows[i];
    object value1 = row["Column1"];
    object value2 = row["Column2"];
    object value3 = row["Column3"];
    object value4 = row["Column4"];
    listView1.Items.Add(string.Format("{0} {1} {2} {3}", value1, value2, value3, value4));
}
Graeme Cooper 16-Oct-18 11:31am    
Yes, Thank you that works as well however I don't see how I can use the columns values later using this method. I attempted something hauntingly similar where value1-4 was just value[0-4] (generally speaking it put each element from the datatable into an array location)(wasn't a good idea).

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