Textbox in gridview

jamie_pattison

Well-known member
Joined
Sep 9, 2008
Messages
116
Programming Experience
Beginner
I have a Gridview which has its datasource set to a DataTable. This has been done due to the data required which is not held in a database. Two columns are generated in this fashion. What i need is a third column to have a textbox, so users can enter their comments etc.

My code is

Dim dt As DataTable = New DataTable
Dim row As DataRow

' Generate Columns
dt.Columns.Add("Col One")
dt.Columns.Add("Col Two")
dt.Columns.Add("Textbox Col")

' Populate rows with required data
row = dt.NewRow
row.Item("Col One") = SomeData
row.Item("Col Two") = SecondaryData
row.Item("Textbox Col") = InsertTextbox("data")

' Add row to DataTable
dt.Rows.Add(row)

gvData.DataSource = dt
gvData.DataBind()

The above code does exactly what i require except when Inserting the textbox (InsertTextbox). The textbox code is

Private Function InsertTextbox(ByVal TBText As String) As TextBox
Dim tbox As TextBox = New TextBox

With tbox
.Width = 100
.Height = 25
.Text = TextboxText
.ID = TextboxText
End With

gvData.Controls.Add(txtbox)

Return txtbox
End Function

The above code has been changed a few times to see how i could have allowed textboxes to be inserted into a gridview cell.

Currently i get the data back as required but the Textbox Col shows as System.Web.UI.WebControls.TextBox. Adding .Text shows the text (in this case data) but its displayed in a label format meaning you cant enter anything inside it.

Could anyone advise how i could insert a textbox in this fashion?

Thanks
 
Back
Top