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
                    ' break 
                Case ListItemType.Item
                    Dim tb1 As TextBox = New TextBox
                    AddHandler tb1.DataBinding, AddressOf tb1_DataBinding
                    tb1.Columns = 4
                    ' break 
                Case ListItemType.EditItem
                    Dim tb2 As New TextBox
                    tb2.TextMode = TextBoxMode.MultiLine
                    tb2.Rows = 5
                    ' break 
                Case ListItemType.Footer
                    Dim chkColumn As CheckBox = New CheckBox
                    chkColumn.ID = "Chk" + _columnName
                    ' 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()
        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.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.DataSource = dt
    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...?

Recommended Answers

All 2 Replies

add templet column with textbox in it.
set textbox multiline to true.

Old post - but here's what I do whenthe Edit button is clicked...

protected virtual void gv_RowEditing(object sender, GridViewEditEventArgs e)
            gv.EditIndex = e.NewEditIndex;
            if (UseMultiLineEditTextBox)
                DataSet ds = new DataSet();
                    for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                        if (FieldMultiLineCommaDelimit.ToUpper().Contains(ds.Tables[0].Columns[j].ColumnName.ToUpper()))
                            TextBox tb = (TextBox)gv.Rows[e.NewEditIndex].Cells[j + 2].Controls[0];
                            tb.TextMode = TextBoxMode.MultiLine;
                            tb.Width = Unit.Pixel(400);
                            tb.Height = Unit.Pixel(100);
                catch (Exception) { }


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.