[讨论]DataGridView与GridView的思考
首先看一下Win窗体的DataGridView private void Form1_Load(object sender, EventArgs e)
{
string conStr = "SERVER=(local);DATABASE=Northwind;INTEGRATED SECURITY=True;";
SqlConnection con = new SqlConnection(conStr);
try
{
con.Open();
string cmdText = @"Select * From Customers";
SqlCommand cmd = new SqlCommand(cmdText, con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
this.dataGridView1.DataSource = ds.Tables [0];
} catch (Exception)
{ throw;
}
finally
{
con.Close();
}
}
{
string conStr = "SERVER=(local);DATABASE=Northwind;INTEGRATED SECURITY=True;";
SqlConnection con = new SqlConnection(conStr);
try
{
con.Open();
string cmdText = @"Select * From Customers";
SqlCommand cmd = new SqlCommand(cmdText, con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
this.dataGridView1.DataSource = ds.Tables [0];
} catch (Exception)
{ throw;
}
finally
{
con.Close();
}
}
这样DataGridView就会正常把数据读出
[图片点击可放大观看]
再看一下Web窗体的GridView
protected void Page_Load(object sender, EventArgs e)
{
string conStr = "SERVER=(local);DATABASE=Northwind;INTEGRATED SECURITY=True;";
SqlConnection con = new SqlConnection(conStr);
try
{
con.Open();
string cmdText = @"Select * From Customers";
SqlCommand cmd = new SqlCommand( cmdText,con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
string cmdText1 = @"Select * From Employees";
SqlCommand cmd1 = new SqlCommand(cmdText1, con);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
da1.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
catch (Exception)
{ throw;
}
finally
{
con.Close();
}
}
{
string conStr = "SERVER=(local);DATABASE=Northwind;INTEGRATED SECURITY=True;";
SqlConnection con = new SqlConnection(conStr);
try
{
con.Open();
string cmdText = @"Select * From Customers";
SqlCommand cmd = new SqlCommand( cmdText,con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
string cmdText1 = @"Select * From Employees";
SqlCommand cmd1 = new SqlCommand(cmdText1, con);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
da1.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
catch (Exception)
{ throw;
}
finally
{
con.Close();
}
}
这个时候显示第一个Sql语句里的数据正常,但是第二个Sql语句只有字段而没有内容,这是为什么呢?
[图片点击可放大观看]
[图片点击可放大观看]
[此贴子已经被作者于2007-5-17 10:18:14编辑过]