C#问题,在winform里添加了Datagridview控件,运行后不显示,只有点击按钮后显示并加载数据库中的数据
//先设置datagridview的visible设置成false
public static string 连接字符串 = "Data Source=LocalHost实例名;Initial Catalog=数据库名;Integrated Security=True";
SqlConnection sqlConnection = new SqlConnection(连接字符串);
string cmdText = "SELECT 。。。。。。。";
SqlCommand selectCommand = new SqlCommand(cmdText, sqlConnection);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommand);
DataTable dt = new DataTable();
sqlConnection.Open();
sqlDataAdapter.Fill(dataTable);
sqlConnection.Close();
dataGridView1.DataSource=dt;
dataGridView1.Visible = true;
能不能留个QQ,我还有问题想请教。。。
29077384
2013-12-27
先把datagridview的visible设置成false。
双击button生成事件
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Visible = true;
}
连接数据库的话,你要是要直接绑定数据源还是代码实现?
代码实现的
string strcon="server=.;database=xx;uid=xx;pwd=xx";
private void button1_Click(object sender, EventArgs e)
{
DataTable dt=new DataTable();
using (SqlConnection conn = new SqlConnection(strcon))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("查询数据库语句", conn))
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
}
}
dataGridView1.DataSource=dt;
dataGridView1.Visible = true;
}