简体   繁体   中英

How add control programmatically in gridview template?

I want add control label in my gridview, is it possible add with datatable? here my code:

 <asp:GridView ID="reportScheduleDetailsGridView" 
               runat="server" 
               AutoGenerateColumns="False">
 </asp:GridView>

I try use tag html span, but it not render:

string queryString = @"SELECT * FROM [table1]";
SqlCommand cmd = new SqlCommand(queryString, connOkto);
using (SqlDataReader sdrMaster = cmd.ExecuteReader())
{
    while (sdrMaster.Read())
    {
        DataRow rows = dataTable.NewRow();
        rows[0] = sdrMaster["name"].ToString();
        for (var x = 1; x < maxCol; x++)
        {
            queryString = @"SELECT * FROM table2";
            cmd = new SqlCommand(queryString, connOkto);
            using (SqlDataReader sdrRev = cmd.ExecuteReader())
            {
                while (sdrRev.Read())
                {
                    blok = "<span></span>";
                    no = (int)Int16.Parse(sdrRev["no"].ToString());
                }
            }
            rows[x] = blok;
            if (no > 1)
            {
                no--;
            }
            else
            {
                blok = "";
            }

        }
        dataTable.Rows.Add(rows);
    } }

I don't know, how can add control asp in gridview, like label. Please help, thanks.

One way to add controls is using OnRowDataBound event of GridView. Add a placeHolder to say, inside <ItemTemplate> of your grid view.

<asp:GridView ID="EmpGridView" OnRowDataBound="EmpGridView_RowDataBound"
     <ItemTemplate>
     <asp:PlaceHolder ID="placeholder1" runat="server"></asp:PlaceHolder>
     </ItemTemplate>
...></asp:GridView>

Ang your code behind file will have:

protected void EmpGridView_RowDataBound(object sender, GridViewRowEventArgs e)
  {
       if (e.Row.RowType == DataControlRowType.DataRow)
        { 
          // Create a label control  
          Label lbl = new Label();
          lbl.Text="MyDynamic Label";
          lbl.ID="lbl1"; // use ID values you prefer 

         // lets create one more control for example      
          LinkButton btnlink = new LinkButton();
          btnlink.Text = "Delete";
          btnlink.ID = "btnDelete";
          linkb.Click += new EventHandler(btnlink_Click);

        // add the controls to your placeholder inside <ItemTemplate>
        PlaceHolder phld = e.Row.FindControl("Placeholder1") as PlaceHolder;
        phld.Controls.Add(btnlink);
        phld.Controls.Add(lbl);

        //code to add the control to only a specific COLUMN/ Cell
        e.Row.Cells[1].Controls.Add(btnlink); // adding to 2nd Column
        // adding to last column..
        e.Row.Cells[EmpGridView.Columns.Count - 1].Controls.Add(btnlink);
            }
        }

Hope this various ways of adding controls to Templates as well as in a cell of GridView helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM