Click here to Skip to main content
14,930,311 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi Geeks,

I've a gridview with webcontrols(Textbox,Button in footer). Dynamically I need to create any number of gridviews by the following code. But only 1 gridview is created. And also the problem is the button event is not firing

C#
public void btn_Click(object sender, EventArgs e)
    {
        GridView gv = new GridView();
        gv.ID = sender.ToString();
    }


inside the class. How to fire this event? This is the code I used to create gridview.
C#
protected void btnAddGrid_Click(object sender,EventArgs e)
{
    int count = Convert.ToInt32(Session["count"]);
    DataTable dt=(DataTable)ViewState["CurrentTableforCommonDetails"];
    GridView gv = new GridView();
    gv.ID = "GridView" + count;
    gv.AutoGenerateColumns = false;
    if (dt.Rows.Count > 0)
    {
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            TemplateField tmpfld = new TemplateField();
            tmpfld.HeaderText = dt.Columns[i].ColumnName.ToString();
            tmpfld.ItemTemplate =new DynamicTemplateField();
            if (i == 0)
            { 
                tmpfld.FooterTemplate = new DynamicTemplateField1();
            }
            gv.Columns.Add(tmpfld);
        }
    }
    gv.Visible = true;
    gv.ShowHeaderWhenEmpty = true;
    gv.ShowFooter = true;
    placegridview.Controls.Add(gv);
    gv.DataSource = dt;
    gv.DataBind();

    count++;
    Session["count"] = count;
}

public class DynamicTemplateField : ITemplate
{
    public void InstantiateIn(System.Web.UI.Control container)
    {
        //define the control to be added , i take text box as your need
        System.Web.UI.WebControls.TextBox txt1 = new System.Web.UI.WebControls.TextBox();
        txt1.ID = "txt1";
        txt1.Width = 50;
        container.Controls.Add(txt1);
    }
}

public class DynamicTemplateField1 : ITemplate
{
    public void InstantiateIn(System.Web.UI.Control container)
    {
        //define the control to be added , i take text box as your need
        System.Web.UI.WebControls.Button btn = new System.Web.UI.WebControls.Button();
        btn.ID = "btn1";
        btn.Click += new EventHandler(btn_Click);
        btn.UseSubmitBehavior = false;
        btn.Text = "Add New";
        container.Controls.Add(btn);
    }
    public void btn_Click(object sender, EventArgs e)
    {
        GridView gv = new GridView();
        gv.ID = sender.ToString();
    }

}
Posted
Comments
   
Why would you ever need to fire an event? You should only handle them. The event is fired by the user pressing on your button.
—SA
Sriram Ramachandran 30-Jul-14 2:44am
   
Yeah I've handled the code too. But that function is not called when I click the button. Thats my problem..

   
Comments
Sriram Ramachandran 30-Jul-14 2:41am
   
I know this already. This doesn't solve my problem...
prasanna.raj 30-Jul-14 3:34am
   
oh i ll try and tell u..
Instead of using the button click event,try using CellContentClick
C#
this.dataGrid1.CellContentClick += new DataGridViewCellEventHandler(dataGrid1_CellContentClick);


And then in the event handler,ask if it's your button cell

C#
void dataGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
            if (e.ColumnIndex == this.dataGrid1.Columns["ButtonColumn"].Index )
            {
                //Do your staff
            }
        }



EDIT: That was for winforms. In asp.net refer this http://msdn.microsoft.com/en-us/library/vstudio/bb907626(v=vs.90).aspx[^] Hope it helps.
   
v2
Comments
Sriram Ramachandran 30-Jul-14 3:10am
   
The button is at footerrow..
Pikoh 30-Jul-14 3:35am
   
Sorry,i thought it was winforms but it's asp.net isn't it?
Pikoh 30-Jul-14 3:38am
   
See my improved solution.
Sriram Ramachandran 30-Jul-14 3:44am
   
yeah its ASP.NET and its GRIDVIEW and not DataGrid

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900