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
Sam HobbsRank #107 · 20.5K reputationPosted Oct 31, 2010, 6:14 PM EDT
Suthish NairRank #92 · 22.8K reputationPosted Oct 31, 2010, 10:05 PM EDT
Carlos Martinez12 reputationPosted Oct 31, 2010, 10:25 PM EDT
Bryian TanRank #396 · 6.3K reputationPosted Oct 31, 2010, 10:29 PM EDT
Change dataGridView1.DataSource = query;
to
dataGridView1.DataSource = query.ToList(); //convert the Enumerable to List
Carlos Martinez12 reputationPosted Oct 31, 2010, 11:00 PM EDT
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 TanRank #396 · 6.3K reputationPosted Nov 1, 2010, 3:31 PM EDT
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 Martinez12 reputationPosted Nov 1, 2010, 7:29 PM EDT
Carlos Martinez12 reputationPosted Nov 1, 2010, 7:31 PM EDT
Bryian TanRank #396 · 6.3K reputationPosted Nov 1, 2010, 7:33 PM EDT
What is the error message?
Carlos Martinez12 reputationPosted Nov 1, 2010, 7:51 PM EDT
Bryian TanRank #396 · 6.3K reputationPosted Nov 1, 2010, 8:53 PM EDT
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
Howard Schuchman5 reputationPosted Jun 15, 2015, 12:40 PM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts