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...?

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;
            BindGrid();
            if (UseMultiLineEditTextBox)
            {
                DataSet ds = new DataSet();
                ds.ReadXml(HttpContext.Current.Server.MapPath(xmlFile));
                try
                {
                    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) { }
            }
        }

James

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.