Hi guys I need help with my C# winform app
below/attached the code
Thanks.
--------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ListOfRunners
{
public partial class Form1 : Form
{
//protected Runner runner;
//protected List listRunner;
public Form1()
{
InitializeComponent();
//runner = new Runner();
//listRunner = new List();
}
private void Form1_Load(object sender, EventArgs e)
{
//Runner runner = new Runner();
List listRunner = new List();
StreamReader myReader = new StreamReader("TextFile1.txt");
string line = "";
do
{
line = myReader.ReadLine();
if (line != null)
{
listRunner.Add(GetRunnerDetails(line));
}
} while (line != null);
myReader.Close();
var query = from r in listRunner
//orderby r.Time
//select r;
select new { r.Name, r.Time, r.Age };
dataGridView1.DataSource = query;
foreach (var item in query)
{
MessageBox.Show("Name:" + item.Name);
}
}
//Process each line of data to get the Participant object
private Runner GetRunnerDetails(string line)
{
Runner runner = new Runner();
line = line.Trim();
//to get the name
int delimiterIndex = line.IndexOf(" ");
runner.Name = line.Substring(0, delimiterIndex);
line = line.Replace(runner.Name, "");
line = line.Trim();
//to get age
delimiterIndex = line.IndexOf(" ");
runner.Time = int.Parse(line.Substring(0, delimiterIndex));
line = line.Replace(runner.Time.ToString(), "");
line = line.Trim();
//to get the time
runner.Age = int.Parse(line);
return runner;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListOfRunners
{
class Runner
{
public string Name { get; set; }
public int Time { get; set; }
public int Age { get; set; }
public int Ranking { get; set; }
public void Print()
{
Console.WriteLine("{0} {1} {2}", Name, Time, Age);
}
}
}
Jay 12 33
Hem 34 28
Umesh 22 37
Tom 21 30
Cliff 13 33
Vini 17 28
Matt 10 28
Ben 9 29
Brandon 15 14
Howard SchuchmanPosted Jun 15, 2015, 4:40 PM
Bryian TanPosted Nov 2, 2010, 12:53 AM
There is an extra right curly bracket (}) on line 91 return Group; }. remove the } . Then add the right curly bracket at the end of the AssignRanks method
Carlos MartinezPosted Nov 1, 2010, 11:51 PM
Bryian TanPosted Nov 1, 2010, 11:33 PM
What is the error message?
Carlos MartinezPosted Nov 1, 2010, 11:31 PM
Carlos MartinezPosted Nov 1, 2010, 11:29 PM
Bryian TanPosted Nov 1, 2010, 7:31 PM
The DatGridView is expecting a typed object. The article mentioned "The
ObjectQueryclass represents a typed query...". On the other hand, your query return anonymous type and listRunner is defined as ListYou can also use BindingSource. See below example.
....
var query = from r in listRunner
//orderby r.Time
//select r;
select new { r.Name, r.Time, r.Age };
BindingSource bs = new BindingSource();
bs.DataSource = query;
dataGridView1.DataSource = bs;
.....
Carlos MartinezPosted Nov 1, 2010, 3:00 AM
But can you explain me why regarding this part of the code, there is no need for query.ToList(); //convert the Enumerable to List
----------------------------------------------------
private void Form1_Load(object sender, EventArgs e)
{
ObjectQuery
dgvPersonName.DataSource = personNameQuery;
dgvPersonName.Columns["ID"].Visible = false;
}
----------------------------------------------------
link to the code above -->http://www.codeproject.com/KB/databa...ramework3.aspx
Bryian TanPosted Nov 1, 2010, 2:29 AM
Change dataGridView1.DataSource = query;
to
dataGridView1.DataSource = query.ToList(); //convert the Enumerable to List
Carlos MartinezPosted Nov 1, 2010, 2:25 AM
Suthish NairPosted Nov 1, 2010, 2:05 AM
Sam HobbsPosted Oct 31, 2010, 10:14 PM