Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programmatically creating a textbox inside a datagrid

Status
Not open for further replies.

ac11nyc

Programmer
Oct 1, 2003
94
US
Is there a way to programmatically create a textbox inside a datagrid?? What i have is:

Dim bc As BoundColumn = New BoundColumn

bc.HeaderText = "Task ID"
bc.DataField = "TaskId"
bc.ItemStyle.Wrap = False
TasksDG.Columns.Add(bc)

bc = New BoundColumn
bc.HeaderText = "Task Name"
bc.DataField = "TaskTitle"
bc.ItemStyle.Wrap = False
TasksDG.Columns.Add(bc)

bc = New BoundColumn
bc.HeaderText = "Estimated Hours"
bc.DataField = "EstimatedHours"
bc.ItemStyle.Wrap = False
TasksDG.Columns.Add(bc)

Now i need the estimated hours colum to be a textbox so that it may be edited. Any help from anyone would be GREATLY aprreciated!! thanx ~AC
 
You'd want to use a TemplateColumn (which you can programmatically create on the fly). I suspect you have a good reason for what you're doing, but I must ask: Do you necessarily have to create these columns in code/ can you use the designer?

About creating TemplateColumns on the fly:




Get a FREE iPod by helping me get mine! Click my referrer link:

More about the company and deal:
 
well what im doing is that im creating a datagrid in the design which holds all the information of a project but once that is loaded i have another grid being created dynamically that holds all the tasks for that project. If there is a more simple way of doing this please let me know but i dont know one off the top of my head. I also need to know how to bind data to these textboxes once they are dynamically created. Ive created them but do not know how to bind data to them. Thanx
 
well what im doing is that im creating a datagrid in the design which holds all the information of a project but once that is loaded i have another grid being created dynamically that holds all the tasks for that project."

Will the tasks grid always have the same structure?

[COLOR=blue gainsboro]
Get a FREE iPod by helping me get mine! Click my referrer link:

More about the company and deal:
[/color]
 
The tasks grid will always has the same amount of columns but not neccessarily the same amount of rows. The grid will contain the task id, task name, a textbox for every day of the week, and a total hrs worked.
 
Okay, what you want to do is have a template column for each column you want to create, then bind your data to certain properties of the control. It sounds like you can just use the designer (not the code-behind) to do so.

To start, go to the designer and create a bunch of template columns (via the Columns property in the Properties Window. After doing so, right-click the grid in the designer and go to "Edit Templates" and select one column at a time. Drag a TextBox into the "ItemTemplate" section of the template designer.

Once you have the structure of your grid, go into the .aspx markup and locate one of your TextBoxes. It should look something like:

<asp:TemplateColumn>
<asp:ItemTemplate>
<asp:TextBox id=...
</asp:ItemTemplate>
</asp:TemplateColumn>

What you want to do is tweak the properties of the TextBox within the item template. Within the markup, the way to do this is to declare:

<asp:TextBox id='blah' runat='server' Text='<%# DataBinder.Eval( Container.DataItem, "FieldName" ) %>'></asp:TextBox>

Where "FieldName" is the name of the field you want to bind to.

Once you've bound the properties accordingly, all you have to do to populate each of the rows is to make a simple call to: myDataGrid.DataBind() as you would normally. The data-binding framework will fill in all the values for you automatically for each row.



[COLOR=blue gainsboro]
Get a FREE iPod by helping me get mine! Click my referrer link:

More about the company and deal:
[/color]
 
this wont work cause the grid is being created for every row in the datagrid so therefore it has to be created dynamically once the first grid is loaded. For example

The outside grid contains

Project Name | Weekly Hours
---------------------------------------------------------
Project 1 |(the grid created dynacially will be here)
|
Date |TaskId | Sun|Mon|Tue|Wed|Thu|Fri|Sat|Total
|------------------------------------------
| 1 | 5 | 4 | 3 | 2 | 1 | 0 | 0 | 15 hrs
| 2 | 0 | 3 | 2 | 1 | 0 | 1 | 0 | 7 hrs
|
---------------------------------------------------------
Project 2 |
Date |TaskId | Sun|Mon|Tue|Wed|Thu|Fri|Sat|Total
|------------------------------------------
| 1 | 5 | 4 | 3 | 2 | 1 | 0 | 0 | 15 hrs
| 2 | 0 | 3 | 2 | 1 | 0 | 1 | 0 | 7 hrs
|

Each day of the week will have to be a textbox so that it may be edited and save the information.
 
this wont work cause the grid is being created for every row in the datagrid so therefore it has to be created dynamically once the first grid is loaded."

It shouldn't be too tricky, hopefully. Simply define the DataGrid's structure within the ItemTemplate of the master grid (as you'd define the structure of any normal grid).

Then, in the ItemDataBound event you can get a reference to the sub-Grid and simply set up the binding (not the structure).

[COLOR=blue gainsboro]
Get a FREE iPod by helping me get mine! Click my referrer link:

More about the company and deal:
[/color]
 
unfortunately you cannot do that cause the app doesnt recognize the second grid. I thought this might work also but when i try to refer to the sub-Grid, it does not recognize it being in the form.
 
Really?! That's a bummer. I wonder what's going on with that.

Anyway, I suppose you can just create a TemplateColumn in code. Here's a good reference:


[COLOR=blue gainsboro]
Get a FREE iPod by helping me get mine! Click my referrer link:

More about the company and deal:
[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top