Click here to Skip to main content
16,018,525 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have load event as follow:
protected void Page_Load(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=testdb;Integrated Security=True;Pooling=False");
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from emp",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GV_DataByGroupAct.DataSource =ds.Tables[0]; 
        GV_DataByGroupAct.DataBind();
    }

Have look of source view:
XML
<asp:GridView ID="GV_DataByGroupAct" runat="server" AutoGenerateColumns="true"
        onrowdatabound="GV_DataByGroupAct_RowDataBound">
        <Columns>

</Columns>
    </asp:GridView>

And GV_DataByGroupAct_RowDataBound event:
C#
protected void GV_DataByGroupAct_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (TableCell tc in e.Row.Cells)
            {
                dl = new DropDownList();
                dl.Items.Add("one");
                dl.Items.Add("two");
                dl.Items.Add("three");

                tc.Controls.Add(dl);
            }
        }

    }

As i run this it adds only dropdownlist control to the gridview.
I am just trying to add data from database as well as dropdownlist to each rows in a new column. Please suggest.
Posted
Updated 11-Nov-13 22:12pm
v3

First of all, you should check IsPostBack property on Page_Load.
C#
protected void Page_Load(object sender, EventArgs e)
{
     If(!IsPostBack)
     {
        SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=testdb;Integrated Security=True;Pooling=False");
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from emp",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GV_DataByGroupAct.DataSource =ds.Tables[0]; 
        GV_DataByGroupAct.DataBind();
     }
}

In RowDataBound, you are looping for all cells, but you should only loop for the rows.
That may be the reason it is showing on all cells.
 
Share this answer
 
Comments
Brijesh Kr 12-Nov-13 4:10am    
Would you give on sample?
Which sample? Do a Google search, you will find many.
Brijesh Kr 12-Nov-13 5:11am    
Give any one which works?
As I said you the problem was " you were adding controls by looping for each cell". And that is wrong.
In your answer, you have eliminated it and added the control to a particular cell that is Cell 2.

That is what I was telling you. :)
Well do some thing like follow:
Only problem i think would be is performance degrade, isn't? anyone has some solution?

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
           DataKeyNames="EmpId" DataSourceID="SqlDataSource1"
           ondatabound="GridView1_DataBound">
           <Columns>
               <asp:CommandField ShowSelectButton="True" />

               <asp:BoundField DataField="EmpId" HeaderText="EmpId" ReadOnly="True"
                   SortExpression="EmpId" />
                   <asp:BoundField  HeaderText="Category" />
               <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
           </Columns>
       </asp:GridView>
       <asp:SqlDataSource ID="SqlDataSource1" runat="server"
           ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>"
           SelectCommand="SELECT * FROM [employee]"></asp:SqlDataSource>


C#
protected void Page_Load(object sender, EventArgs e)
   {
       if(IsPostBack)
       addControl();
   }
   void addControl()
   {

       foreach (GridViewRow row in GridView1.Rows)
       {
           if (row.RowType == DataControlRowType.DataRow)
           {
               DropDownList dl = new DropDownList();
               dl.Items.Add("One");
               dl.Items.Add("Two");
               row.Cells[2].Controls.Add(dl);
           }
       }
   }
   protected void GridView1_DataBound(object sender, EventArgs e)
   {

       addControl();
   }
 
Share this answer
 

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