0

Im working on a project just to add few things and one of thoose is to add a DropDownList on a GridView when they press the edit button of a row... Sadly the columns are added on runtime before the database binding, not on the aspx page as all the examples i found, i have it here like this:

private void SetColumnsGrid(GridView Grid)
    {
        BoundField Col = new BoundField();//1
        Col.HeaderText = "Name";
        Col.DataField = "Name";
        Col.HeaderStyle.Width = Unit.Pixel(100);
        Col.ReadOnly = true;
        Grid.Columns.Add(Col);

        Col = new BoundField(); //2
        Col.HeaderText = "User Type";
        Col.DataField = "UserType";
        Col.HeaderStyle.Width = Unit.Pixel(100);
        Grid.Columns.Add(Col);

        //Is ddl spected to be here as the TemplateField with the EditItemTemplate?
}

So, how can i do it? I just dont find the proper way. Whitch events should i handle?

Thank you very much

1 Answer 1

1

There are several options you can use. One is to use a template, the other is to manually add the control when the row is created. Template example (this uses a checkbox but can easily be switched):

Public Class CheckBoxTemplate
    Implements ITemplate   

    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
        Dim cb As CheckBox = New CheckBox()
        cb.ID = "someId"
        cb.AutoPostBack = True

        container.Controls.Add(cb)


    End Sub
End Class

In your app code, where you create the gridview control:

    Dim gv As New GridView
    With gv
        .ID = "myGridView"
        .AutoGenerateColumns = False
        .DataKeyNames = New String() {"somePKID"}
        .GridLines = GridLines.Both
        .AllowSorting = False
        .AllowPaging = False
        .PageSize = numRows
        .Width = tableWidth
        .BorderColor = Drawing.ColorTranslator.FromHtml("#808080")
        .PagerSettings.Mode = PagerButtons.NextPrevious
        .PagerSettings.NextPageText = "Next"
        .PagerSettings.PreviousPageText = "Prev"
        .HeaderStyle.CssClass = foundUserHeadStyle
        .RowStyle.CssClass = foundUserEvenRows
        .AlternatingRowStyle.CssClass = foundUserOddRows
        .Columns.Clear()

        Dim SelectUserTF As New TemplateField
        With SelectUserTF
            .HeaderText = "Add"
            .ItemStyle.Wrap = False
            .ItemTemplate = New CheckBoxTemplate()
        End With
       .Columns.Add(SelectUserTF)

   End With

Another option is to do this in the create row event:

     Protected Sub gv_rowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles myGridView.RowCreated
    Try
        Dim myDDL As New DropDownList
        Dim myCollection As New ListItemCollection
        With myCollection
            Dim newItem As New ListItem
            newItem.Text = "item 1"
            newItem.Value = "1"

            .Add(newItem)
        End With
        e.Row.Cells(0).Controls.Add(myDDL)
  Catch ex As Exception

    Finally

    End Try
end sub

Let me know if this helps or if you have a question about it.

Sign up to request clarification or add additional context in comments.

1 Comment

At the end i convinced my boss to do it on the old way. Anyways thanks for the help, we will have it in mind for the next time this issue comes out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.