datagridview数据邦定的问题
现在数据库里有两个字段 tname和tage.请问如何帮这两个字段与datagridview中的两列邦定,我现在邦定后什么都不出现,不邦定直接通过sql可以查询出来.请问怎么设置数据库列字段的邦定和显示.
------解决方案--------------------try:
SqlConnection conn = new SqlConnection( "server=(local);database=yourtable;Integrated Security=SSPI "); conn.Open();
SqlCommand cmd = new SqlCommand( "select tname,tage from yourtable ", conn);
SqlDataReader dr = cmd.ExecuteReader();
this.GridView1.DataSource=dr;
this.DataView1.DataBind();
或者直接简单绑定也可以的 直接拖拽绑定》》》》
------解决方案--------------------for example:
private void Form1_Load(object sender, EventArgs e)
{
DataBind();
}
private void DataBind()
{
//初始化DataGridView
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.Name = "sno ";
column.HeaderText = "学号 ";
//设置绑定数据源的sno字段
column.DataPropertyName = "sno ";
this.dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.Name = "sname ";
column.HeaderText = "姓名 ";
//设置绑定数据源的sname字段
column.DataPropertyName = "sname ";
this.dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.Name = "sage ";
column.HeaderText = "年龄 ";
//设置绑定数据源的 "sage "字段
column.DataPropertyName = "sage ";
this.dataGridView1.Columns.Add(column);
//设置不自动产生列
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.AllowUserToAddRows = false;
//以下进行数据绑定
SqlConnection con = new SqlConnection( "server=.;database=student;uid=sa;pwd=0421 ");
SqlDataAdapter sda = new SqlDataAdapter( "select * from studentDetails ", con);
DataSet ds = new DataSet();
sda.Fill(ds, "student ");
this.dataGridView1.DataSource = ds.Tables[ "student "];
}