1

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.

2
  • 1
    You can create a user control (.ascx) file and extend it to your asp file to create a custom GridView. Commented Sep 4, 2013 at 1:36
  • @hendrathings - not sure why you are not using template field and binding the datatable with the gridview. Commented Sep 4, 2013 at 3:36

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.