Hi,
Does anyone know of an easy way to make a databound gridview display multiline textboxes when editing rows instead of the standard singleline ones?
I can't use predefinded edit templates, since the gridview is to be absolutely dynamic. That is, I never know how many columns wil be in it, or any of the column names.
Sure, I can add custom templates programmatically, but that seems to disable the automatic editing feautures of the gridview.
Smth like:
Private Class GridViewTemplate
Implements ITemplate
Private _templateType As ListItemType
Private _columnName As String
Public Sub New(ByVal type As ListItemType, ByVal colname As String)
_templateType = type
_columnName = colname
End Sub
Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
Select Case _templateType
Case ListItemType.Header
Dim lbl As Label = New Label
lbl.Text = _columnName
container.Controls.Add(lbl)
' break
Case ListItemType.Item
Dim tb1 As TextBox = New TextBox
AddHandler tb1.DataBinding, AddressOf tb1_DataBinding
tb1.Columns = 4
container.Controls.Add(tb1)
' break
Case ListItemType.EditItem
Dim tb2 As New TextBox
tb2.TextMode = TextBoxMode.MultiLine
tb2.Rows = 5
container.Controls.Add(tb2)
' break
Case ListItemType.Footer
Dim chkColumn As CheckBox = New CheckBox
chkColumn.ID = "Chk" + _columnName
container.Controls.Add(chkColumn)
' break
End Select
End Sub
Sub tb1_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim txtdata As TextBox = CType(sender, TextBox)
Dim container As GridViewRow = CType(txtdata.NamingContainer, GridViewRow)
Dim dataValue As Object = DataBinder.Eval(container.DataItem, _columnName)
If Not (dataValue.ToString = DBNull.Value.ToString) Then
txtdata.Text = dataValue.ToString
End If
End Sub
End Class
Private Sub loadDynamicGridWithTemplateColumn()
GridView1.Columns.Clear()
Dim dt As DataTable = New DataTable
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = " & Request.PhysicalApplicationPath & "\database.mdb") : conn.Open()
Dim da As New OleDbDataAdapter
da.SelectCommand = New OleDbCommand("SELECT * FROM dnmUser", conn)
da.Fill(dt)
da.Dispose() : da = Nothing
conn.Close() : conn = Nothing
For Each col As DataColumn In dt.Columns
Dim bfield As TemplateField = New TemplateField
bfield.HeaderTemplate = New GridViewTemplate(ListItemType.Header, col.ColumnName)
bfield.ItemTemplate = New GridViewTemplate(ListItemType.Item, col.ColumnName)
bfield.EditItemTemplate = New GridViewTemplate(ListItemType.EditItem, col.ColumnName)
GridView1.Columns.Add(bfield)
Next
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
So, I'd like to replace the editing singleline textboxes with multiline ones, but preserving the gridview editing capabilities, so I don't have to rewrite the entire edit/update processes.
Any ideas...?