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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.