If you want to use the same approach, this is how you could do it in C#:
static char[] splitChars = new char[] { ',' };
string[] rows = new string[] { "name2,1,2,3", "name1,3,4,5" };
var query = from row in rows
let columns = row.Split(splitChars, StringSplitOptions.None)
select new
{
name = columns[0],
points = Int32.Parse(columns[1]),
}
into results
orderby results.name
select results;
foreach (var result in query)
{
Console.WriteLine(String.Format("Name: {0}, Points: {1}", result.name, result.points));
}
But you might consider using a CSV reader instead. Here's an excellent one:
A Fast CSV Reader[
^]
Edit:
I missed the part that you want to display the result in a DataGridView. The fastest way to get there would be this:
MydataGridView.DataSource = query.ToList();
However, if you want to modify the values in the DGV and then read them back into somewhere I would recommend using either a DataTable or a regular class for the results instead of the anonymous type constructed by the compiler in my sample above - so something like this instead:
public class ResultRow
{
public string Name { get; private set; }
public int Points { get; set; }
public ResultRow(string name, int points)
{
Name = name;
Points = points;
}
}
List<ResultRow> ResultsList;
var query2 = from row in rows
let columns = row.Split(splitChars, StringSplitOptions.None)
select new ResultRow(columns[0], Int32.Parse(columns[1])) into results
orderby results.Name
select results;
ResultsList = query2.ToList();
MyDataGridView.DataSource = ResultsList;
Edit 2 (after comment):
Sample code to add cell coloring based on values read from your source data - maybe not 100% what you want to achieve but I think you will get the idea:
public partial class Form2 : Form
{
static char[] splitChars = new char[] { ',' };
BindingList<ResultRow> ResultsBindingList;
public class ResultRow
{
public string Name { get; private set; }
public int Points { get; set; }
public Color CellColor { get; private set; }
public ResultRow(string name, int points, Color cellColor)
{
Name = name;
Points = points;
CellColor = cellColor;
}
}
public Form2()
{
InitializeComponent();
string[] rows = new string[] { "name2,1,2,3,#cc0000", "name1,3,4,5,#00cc00" };
var query = from row in rows
let columns = row.Split(splitChars, StringSplitOptions.None)
let name = columns[0]
let points = Int32.Parse(columns[1])
let color = System.Drawing.ColorTranslator.FromHtml(columns[4])
select new ResultRow(name, points, color) into results
orderby results.Name
select results;
ResultsBindingList = new BindingList<ResultRow>(query.ToList());
MyDataGridView.DataSource = ResultsBindingList;
MyDataGridView.Columns["CellColor"].Visible = false;
MyDataGridView.CellFormatting += MyDataGridViewCellFormatting;
}
private void MyDataGridViewCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
e.CellStyle.BackColor = (Color)MyDataGridView["CellColor", e.RowIndex].Value;
}
private void AddRowButtonClick(object sender, EventArgs e)
{
ResultsBindingList.Add(new ResultRow("", 0, Color.Yellow));
}
}