如何把LINQ查询的数据放到dataGridView中
展开全部
jobsDataContext db = new jobsDataContext();//jobsDataContext为LinqToSql类
Table<jobs> job = db.GetTable<jobs>();
string sql = "select * from jobs";
dataGridView1.DataSource = db.ExecuteQuery<jobs>(sql).ToList();
private void Bind(int id)
{
try
{
id = int.Parse(tbId.Text);
jobsDataContext db = new jobsDataContext();
Table<jobs> job = db.GetTable<jobs>();
////拼接方法1
//string sql = "select * from jobs where job_id="+tbId.Text;
//dataGridView1.DataSource = db.ExecuteQuery<jobs>(sql).ToList();
//带参数的方法2
//string sqlText = "select * from jobs where job_id={0}";
//dataGridView1.DataSource = db.ExecuteQuery<jobs>(sqlText,id).ToList();
//方法3
var result = from info in job
where info.job_id == id
select info;
dataGridView1.DataSource = result;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Table<jobs> job = db.GetTable<jobs>();
string sql = "select * from jobs";
dataGridView1.DataSource = db.ExecuteQuery<jobs>(sql).ToList();
private void Bind(int id)
{
try
{
id = int.Parse(tbId.Text);
jobsDataContext db = new jobsDataContext();
Table<jobs> job = db.GetTable<jobs>();
////拼接方法1
//string sql = "select * from jobs where job_id="+tbId.Text;
//dataGridView1.DataSource = db.ExecuteQuery<jobs>(sql).ToList();
//带参数的方法2
//string sqlText = "select * from jobs where job_id={0}";
//dataGridView1.DataSource = db.ExecuteQuery<jobs>(sqlText,id).ToList();
//方法3
var result = from info in job
where info.job_id == id
select info;
dataGridView1.DataSource = result;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询