locked
Update record with DataGridView RRS feed

  • Question

  • User-960441985 posted

    I am not expert in DataGridView (DGV). I need to update existing records contains dropdown, checkbox and textbox values. How I could quickly write code to update those records

    wrt to boundcolumn or templatecolumns.

    Could you share code?

    which fields and code behind events are required to update records?

    TemplateColumn or BoundedColumns for "DropDown"

    Thanks a lot

    Monday, August 14, 2017 12:39 PM

All replies

  • User2103319870 posted

    for "DropDown"

    For Dropdownlist or to add any other controls you need template columns. however if you just want to display values you can use Bounded Columns

    which fields and code behind events are required to update records?

    It depends your design , usually a button click will trigger the update operations. You can take a look at this article for more details

    Also the recommended control for Asp.net is Gridview not DataGridView. Please see if you can change the datagrid to Gridview control

    Monday, August 14, 2017 12:53 PM
  • User-960441985 posted

    Thanks A2H for your response. I need to populate DGV DropDown on row edit. 

    Private Sub dgvSearchResults_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles dgvSearchResults.RowEditing

    dgvSearchResults.EditIndex = e.NewEditIndex
    BindGridViewData()

    Dim dp123 As DropDownList = CType(dgvSearchResults.Rows(e.NewEditIndex).Cells(2.Controls(7), DropDownList)
    'dp123.items.add("dfsdf")

    dp123.Items.Add(" ")
    dp123.Items.Add("Cloudy")
    dp123.Items.Add("Overcast")
    dp123.Items.Add("Raining")
    dp123.Items.Add("Sunny")

    It doesn't work


    End Sub

    Monday, August 14, 2017 9:10 PM
  • User2103319870 posted

    You need to use RowDatabound event to populate dropdownlist in GridView Edit mode

    Please try with the below code

     Protected Sub dgvSearchResults_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles dgvSearchResults.RowDataBound
            'Check if Gridview is in edit more
            If e.Row.RowType = DataControlRowType.DataRow AndAlso dgvSearchResults.EditIndex = e.Row.RowIndex Then
                'Find the Dropdownlist control
                Dim dp123 As DropDownList = DirectCast(e.Row.FindControl("dpGridWeather"), DropDownList)
                dp123.Items.Add(" ")
                dp123.Items.Add("Cloudy")
                dp123.Items.Add("Overcast")
                dp123.Items.Add("Raining")
                dp123.Items.Add("Sunny")
            End If
        End Sub

    Monday, August 14, 2017 9:59 PM