日期:2014-05-18  浏览次数:20418 次

向高手求救,在线等。。为什么给girdview绑定数据后,不显示数据,代码如下:
为什么不显示数据呢?
前台:<tr><td colspan="4"><asp:GridView ID="My_List" runat="server" EmptyDataText="暂无记录" 
  AutoGenerateColumns="False" AllowSorting="True" Width="655px">
  <Columns>
  <asp:TemplateField HeaderText="选择">
  <ItemTemplate><asp:CheckBox ID="CB_S" runat="server" />
  </ItemTemplate>
   
  </asp:TemplateField>
  <asp:BoundField DataField="Id" HeaderText="编号"/>
  <asp:BoundField DataField="Name" HeaderText="阶段"/>
  <asp:BoundField DataField="Btime" HeaderText="开始时间"/>
  <asp:BoundField DataField="Etime" HeaderText="结束时间"/>
  <asp:TemplateField HeaderText="修改">
  <ItemTemplate>
  <asp:LinkButton ID="LB_alert" Text="修改" runat="server"></asp:LinkButton>
  </ItemTemplate>
  </asp:TemplateField>
  </Columns></asp:GridView></td></tr>
 后台:protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
   

  bind1();
  }

  }
  public void bind1()
  {
   
  string SqlConnStr = "";
  SqlConnStr = TDirect.Link_Sql_Dir();
  this.My_List.Columns.Clear();
  string mysql = "select id,name,begining,ending from TPeriod ";
  SqlConnection myconnection = new SqlConnection(SqlConnStr);
   
  SqlDataAdapter myda = new SqlDataAdapter(mysql, myconnection);
  DataSet myds = new DataSet();
  myconnection.Open();
  myda.Fill(myds, "TPeriod");
  My_List.DataSource = myds;
  My_List.DataKeyNames = new string[] { "id" };
  My_List.DataBind();
  myconnection.Close();
   
  }

------解决方案--------------------
字段都没对应怎么显示
 <asp:BoundField DataField="Id" HeaderText="编号"/>
<asp:BoundField DataField="Name" HeaderText="阶段"/>
<asp:BoundField DataField="Btime" HeaderText="开始时间"/>
<asp:BoundField DataField="Etime" HeaderText="结束时间"/>

 string mysql = "select id,name,begining,ending from TPeriod ";
------解决方案--------------------
1.绑定字段要对应起来.
2.this.My_List.Columns.Clear();这一个去掉.