第一步:在窗体的设计窗口;拖拉一个DataGridView进来;
第二步:对数据表的列的设计,如下图:
----------下面是后台代码
/// <summary>
/// 窗口加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
BindData();
}
/// <summary>
/// 绑定数据方法
/// </summary>
private void BindData()
{
string strConn = "Data Source=114.113.226.76;Initial Catalog=ChinaEduSpV3;Persist Security Info=True;User ID=sa;Password=[]197897shunzimm;MultipleActiveResultSets=True";
string sql = "select * from Vw_RankingList";
#region //DataSet
//SqlDataAdapter ada = new SqlDataAdapter(sql, strConn);
//DataSet ds = new DataSet();
//ada.Fill(ds);
//dataGridView1.DataSource = ds.Tables[0];
#endregion
DataTable dt = new DataTable();
SqlDataAdapter ada1 = new SqlDataAdapter(sql, strConn);
ada1.Fill(dt);
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = dt;
}