Gridview enable/disable customvalidator

How do I bind a lookup table to dropdownlist in an edit template in a gridview? The lookup table has user names and categories. I only want to bind one category to the dropdown. I have it so the dropdown only shows me the names in the category I want but I would also like it to show the correct name if one already exists for the record selected in the gridview.
Who is Participating?
lakhiConnect With a Mentor Author Commented:
This is what wound up working. I'm not sure if it's correct, but it worked
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
  meString = (CType(GridView1.Rows(e.NewEditIndex).Cells(2).FindControl("meSigLabel"), Label)).Text
End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
  If (e.Row.RowState = DataControlRowState.Edit) OrElse (e.Row.RowState = _
                         (DataControlRowState.Alternate Or DataControlRowState.Edit)) Then
      Dim ddl As DropDownList = CType(e.Row.FindControl("DropDownList1"), DropDownList)
      Dim tb As TextBox = CType(e.Row.FindControl("meDateTextBox"), TextBox)
      If meString <> String.Empty Then
        ddl.Text = meString       
      End If
    End If
  End Sub

Open in new window

On your gridview databound event , you can set the databind for the dropdown and set also the selected value for the dropdownlist. See sample code attached here:
Private Sub gvList_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvList.RowDataBound
	If e.Row.RowType = DataControlRowType.DataRow Then
		Dim row As ListDataSet.ListRow = DirectCast(DirectCast(e.Row.DataItem, DataRowView).Row, ListDataSet.ListRow)
		Dim ddlist As LinkButton = DirectCast(e.Row.FindControl("ddlist"), DropDownList )

 		With ddlist
            .Datasource = putlistdatasource
            .DataTextField = "Description"
            .DataValueField = "KeyValue"
            .SelectedValue = row.value 'This is a value that might be in the grid row
        End With

	End If
End Sub

Open in new window

lakhiAuthor Commented:
"ListDataSet" is not available to me. I'm using VS2005, is that why?

ListDataSet is supposed to be the dataset you used to populate the gridview, it is not a VS control or VS generic object.  However you declare to attain the row to get to the row properties do it here (replace my code declaration on of the row), in order to set the default selected value.  
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.