开发者

Dynamically adding rows to the Gridview in ASP.NET(C#)

        <asp:GridView ID="GridView1" runat="server">
        <开发者_Python百科Columns>
            <asp:TemplateField HeaderText="FileName">
                <ItemTemplate>
                    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="" Text=""></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
     </Columns>
    </asp:GridView>

aspx.cs file

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

I need to print FileName(As Hyperlink),SharedBy,Subject in the Grid how can I do that?

I need to do something like this...But below approach not working

        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++;

        }


Try this:

<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>

In the cs you can bind the grid view with your query like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜