943,963 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 22007
  • ASP.NET RSS
Jul 19th, 2006
0

Controls in GridView

Expand Post »
hi

how can i add controls to a gridview at runtime? anyone knows?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
infinity4ever is offline Offline
22 posts
since Jun 2006
Jul 19th, 2006
0

Re: Controls in GridView

I think you can do this in the same way that you would any table-related collection. If you simply want to add a control, you user GridView1.Rows(y).Cells(x).Controls.Add(ControlToAdd) If you want to add a complete row of controls, you can create a table row object at runtime: Dim x as new TableRow and similarly create table cells. Then you will add each table cell to the row: TR.Cells.Add(TC) (TR = table row/TC = table cell). Finally, you would add the row to the gridview. GridView1.Rows.Add(TR)

...assuming you are using VB for coding
Reputation Points: 10
Solved Threads: 0
Light Poster
ChimpusDupus is offline Offline
47 posts
since Jul 2006
Jul 23rd, 2006
0

Re: Controls in GridView

hi ChimpusDupus

am using vb.net.. i didnt try your method yet but i will and i'll tell you the results

thank you very much
Reputation Points: 10
Solved Threads: 0
Newbie Poster
infinity4ever is offline Offline
22 posts
since Jun 2006
Jul 26th, 2006
0

Re: Controls in GridView

it didnt work i dont know why.. always give me an error out of range whats the problem?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
infinity4ever is offline Offline
22 posts
since Jun 2006
Jul 26th, 2006
0

Re: Controls in GridView

Out of range means you are trying to access an item in a collection that doesn't exist.

Take Cells for example, Cells(0) is the first cell in the Cells collection, if there is only 1 cell in the collection and you try referencing Cells(1) then you get an out of range error.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jul 29th, 2006
0

Re: Controls in GridView

Finally its done.. PARTIALLY

seems like there is one problem left... can anyone help me with?

i did this:

Dim LB as new Label
Dim TR as new TableRow
Dim TC as new TableCell
Dim DT as new Data.DataTable
Dim DC as new Data.DataColumn

LB.Text = "Hello"
TC.Controls.Add(LB)
TR.Cells.Add(TC)
DC.ColumnName = "Labels"
DT.Columns.Add(DC)
DT.Rows.Add(TR)
GridView.DataSource = DT
GridView.DataBind()


this is the code... but when i click a button to run it the grid view display a column with the name "Labels" as i want but in the cell it only writes this:

System.Web.UI.WebControls.TableRow

instead of displaying the label i created

whats the problem? anyone knows?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
infinity4ever is offline Offline
22 posts
since Jun 2006
Jul 29th, 2006
0

Re: Controls in GridView

all i want is to load that label into the grid view
Reputation Points: 10
Solved Threads: 0
Newbie Poster
infinity4ever is offline Offline
22 posts
since Jun 2006
Sep 9th, 2010
0

To add control to gridview

ASP.NET Syntax (Toggle Plain Text)
  1. protected void Button1_Click(object sender, EventArgs e)
  2.  
  3. {
  4.  
  5. DropDownList ddl = new DropDownList();
  6.  
  7. ddl.Visible = true;
  8.  
  9. ddl.ID = "ddl1";
  10.  
  11. ddl.Items.Add("Item1");
  12.  
  13. TableCell cell = new TableCell();
  14.  
  15. gv.Rows[0].Cells.Add(cell);
  16.  
  17. gv.Rows[0].Cells[0].Controls.Add(ddl);
  18.  
  19. }
Last edited by Ezzaral; Sep 9th, 2010 at 1:29 pm. Reason: Added code tags. Please use them to format any code that you post.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
venu.net143 is offline Offline
2 posts
since Sep 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC