2011-06-16 182 views
0
 <asp:GridView ID="GridView1" runat="server"> 
     <Columns> 
      <asp:TemplateField HeaderText="FileName"> 
       <ItemTemplate> 
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="" Text=""></asp:HyperLink> 
       </ItemTemplate> 
      </asp:TemplateField> 
    </Columns> 
    </asp:GridView> 


aspx.cs文件動態添加行到GridView在ASP.NET(C#)

SqlCommand comm = new SqlCommand(Select 
     FileUpload.FileName AS FileName, 
     FileUpload.FilePath AS PATH, 
     SubjectMaster.SubjectName AS Subject, 
     MemberPersonalInformation.FirstName As SharedBy 
    from FileUpload",conn) 

我需要打印FileName(As Hyperlink)SharedBySubject在網格我該怎麼辦呢?
我需要做這樣的事情......但低於做法不工作

 conn.Open(); 
     SqlDataReader rdr = comm.ExecuteReader(); 

     int i = 0; 
     while (rdr.Read()) 
     { 

      string fip = rdr["PATH"].ToString(); 
      HyperLink h1 = (HyperLink)GridView1.Rows[i].Cells[0].FindControl("Hyperlink1"); 
       h1.Text = rdr["FileName"].ToString(); 
       h1.NavigateUrl = "download.aspx?filepath=" + fip; 

       GridView1.Rows[i].Cells[1].Text = rdr["SharedBy"].ToString(); 
       GridView1.Rows[i].Cells[2].Text = rdr["Subject"].ToString(); 
       GridView1.Rows[i].Cells[3].Text = rdr["PATH"].ToString(); 
       //GridView1.Rows[i].Rows.Add(di); 

       i++; 

     } 

回答

0

試試這個:

<asp:GridView ID="GridView1" runat="server"> 
    <Columns> 
    <asp:TemplateField HeaderText="FileName"> 
     <ItemTemplate> 
     <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("PATH")%>' Text='<%#Eval("FileName")%>'></asp:HyperLink>, 
     <asp:Literal ID="Literal1" runat="server" Text='<%#Eval("SharedBy")+", "+ Eval("Subject")%>' /> 
     </ItemTemplate> 
    </asp:TemplateField> 
    </Columns> 
/asp:GridView> 

在CS您可以使用網格視圖和您的查詢像這樣綁定:

conn.Open(); 
SqlCommand comm = new SqlCommand("your query"); 
SqlDataReader rdr = comm.ExecuteReader(CommandBehavior.CloseConnection); 
GridView1.DataSource = rdr; 
GridView1.DataBind(); 
rdr.close(); 
+0

我不明白你在說什麼?實際上我需要在.cs文件中編碼 – Naresh 2011-06-16 07:55:55

+0

編輯我的響應.cs – Atzoya 2011-06-16 08:22:52

+0

爲什麼要在代碼後面設置? – 2011-06-16 08:29:34