2009-12-14 27 views
0

當u在aspx頁面使用SQL數據源填充GridView中排序工作正常...如何在GridView控件使用模板字段

但我現在用的模板字段進行排序,列在代碼隱藏單獨填充和排序是不工作...

我的代碼是

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
          AutoGenerateColumns="False" 
          ondatabound="GridView1_DataBound" 
         onrowdatabound="GridView1_RowDataBound"> 


       <Columns> 
       <asp:TemplateField HeaderText="File Name" ItemStyle-Width="40%" > 
           <EditItemTemplate> 
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="Label1" runat="server"></asp:Label> 
           </ItemTemplate> 
           <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" /> 
          </asp:TemplateField> 

          <asp:TemplateField HeaderText="Failure Count" ItemStyle-Width="10%" > 
           <EditItemTemplate> 
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="Label3" runat="server"></asp:Label> 
           </ItemTemplate> 
           <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" /> 
           <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> 
          </asp:TemplateField> 
       </Columns></GridView> 

和我隱藏的是:

DataTable dt = new DataTable(); 
     SqlConnection connection = new SqlConnection(); 
     connection.ConnectionString = ConfigurationManager.ConnectionStrings["SumooHAgentDBConnectionString"].ConnectionString; 
     connection.Open(); 
     SqlCommand sqlCmd = new SqlCommand("SELECT FileName,FailureCount from Files where [email protected] , connection); 
     SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); 
     sqlCmd.Parameters.AddWithValue("strID", strID); 

     sqlDa.Fill(dt); 

     if (dt.Rows.Count > 0) 
     { 
      for (int i = 0; i < dt.Rows.Count; i++) 
      { 
       string nameoffiles = dt.Rows[i]["FileName"].ToString(); 
       buFailureCode.Add(code); 
       string count = dt.Rows[i]["BuFailureCount"].ToString(); 
       buFailureCount.Add(count); 
      } 

     } 
     connection.Close(); 
    } 


protected void GridView1_DataBound(object sender, EventArgs e) 
    { 
     if (namesOfFiles.Count != 0) 
     { 
      for (int i = 0; i < namesOfFiles.Count; i++) 
      { 
       GridViewRow myRow = GridView1.Rows[i]; 
       Label Label1 = (Label)myRow.FindControl("Label1"); 

       Label Label3 = (Label)myRow.FindControl("Label3"); 
       Label1.Text = namesOfFiles[i].ToString(); 

       Label3.Text = buFailureCount[i].ToString(); 

      }}} 

回答

相關問題