private void Form1_Load(object sender, EventArgs e) //这个我写对了, 启动的时候绑定数据!
{
string sql = "select * from selectFly";
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=fly;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter(sql, con);
//DataSet ds = new DataSet();
da.Fill(ds);
dgv.DataSource = ds.Tables[0];
} //下面这里我是想写个搜索,但是做不出来。实现查询从某地到某地的一个搜索,搜到数据再把搜索到的数据绑到DataGridView里显示出来就行!搞不定啊,请各位大哥帮帮我这只小菜鸟吧!自学好辛苦~~ private void button1_Click(object sender, EventArgs e)
{
try
{
ds.Clear();
string aaa = textBox1.Text.Trim();
string bbb = textBox2.Text.Trim();
string sql = string.Format("select * from selectFly where FlyGo = {0} and FlyTo = {1}", aaa, bbb);
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=fly;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter(sql, con);
//DataSet ds = new DataSet();
da.Fill(ds);
dgv.DataSource = ds.Tables[0];
}
catch
{
MessageBox.Show("数据库打开错误");
}
} }
}
da.Fill(ds);
ds.Tables[xx].Rows.Find("xx");//使用find方法查找
DataGridView.Rows.Add(the return Row);
这样试一下,或许可以,不可以的话就在单击事件里面设断点,调试一下,看哪 里出错
{
try
{
ds.Clear();
string aaa = textBox1.Text.Trim();
string bbb = textBox2.Text.Trim();
string sql = string.Format("select * from selectFly where FlyGo = '{0}' and FlyTo = '{1}'", aaa, bbb);
da = new SqlDataAdapter(sql, con); //把SqlDataAdapter设置为全局变量
ds = new DataSet(); //把DataSet 设置为全局变量,也就是说,你前面那些也改
da.Fill(ds,"temp");//注意保存在临时表格里
dgv.DataSource = ds.Tables[0];
}
catch
{
MessageBox.Show("数据库打开错误");
}
}
}
}