关于vs中Windows窗体GridView控件的使用 最后运行依然不显示数据,不知道哪里错了
privatevoidForm1_Load(objectsender,EventArgse){SqlConnectionconn=newSqlConnection(Con...
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConnectionString);
string sql = "select * from Empinfo";
string sql2 = "select count(*) from Empinfo";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlCommand cmd2 = new SqlCommand(sql2, conn);
conn.Open();
DataTable table = new DataTable();
table.Columns.Add("序号");
table.Columns.Add("姓名");
table.Columns.Add("性别");
table.Columns.Add("职务");
table.Columns.Add("工资");
int index=Convert.ToInt16( cmd2.ExecuteScalar());
conn.Close();
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
DataRow row = table.NewRow();
row["序号"] = (++index).ToString();
row["姓名"] = reader["Emp_name"].ToString();
row["性别"] = reader["Sex"].ToString();
row["职务"] = reader["Title"].ToString();
row["工资"] = reader["Wage"].ToString();
table.Rows.Add(row);
}
dataGridView1.DataSource = table;
reader.Close();
conn.Close();
} 展开
{
SqlConnection conn = new SqlConnection(ConnectionString);
string sql = "select * from Empinfo";
string sql2 = "select count(*) from Empinfo";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlCommand cmd2 = new SqlCommand(sql2, conn);
conn.Open();
DataTable table = new DataTable();
table.Columns.Add("序号");
table.Columns.Add("姓名");
table.Columns.Add("性别");
table.Columns.Add("职务");
table.Columns.Add("工资");
int index=Convert.ToInt16( cmd2.ExecuteScalar());
conn.Close();
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
DataRow row = table.NewRow();
row["序号"] = (++index).ToString();
row["姓名"] = reader["Emp_name"].ToString();
row["性别"] = reader["Sex"].ToString();
row["职务"] = reader["Title"].ToString();
row["工资"] = reader["Wage"].ToString();
table.Rows.Add(row);
}
dataGridView1.DataSource = table;
reader.Close();
conn.Close();
} 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)+提问者悬赏5(财富值+成长值)
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询