Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DropDownList in GridView Row Edit Issue 4

Status
Not open for further replies.

LittlBUGer

Programmer
Apr 26, 2006
81
US
Hello. I've been struggling with an issue where I have a gridview, bound to a SQL table, where I have one column as a template column so I can put in a dropdownlist in the gridview's edit mode for that column. I've gotten all of this to work great, except for when data coming from the database is NULL for that column. Nothing I've tried has gotten around it.

So the DDL in the gridview is getting data from the DB such as:

Text, Value
-----------
Text1, 1
Text2, 2
Text3, 3
Text4, 4

So the DDL text field is the Text column while the value field is the Value column--simple enough. I then have a OnDataBound event for the DDL where I then insert a "-" into the 0 item. So now the DDL has the following data:

Text, Value
-----------
-, -
Text1, 1
Text2, 2
Text3, 3
Text4, 4

I also have the SelectedValue property set to something like:
Code:
SelectedValue='<%# Bind("Value") %>'

so that when it goes into the Edit mode, it selects the current/correct value. For the Select part of the SQL query the gridview is bound to, if there's a Null value, I set both the text column and the value column to '-', so something like:
Code:
SELECT ..., CASE WHEN [Text] IS NULL THEN '-' ELSE [Text] END AS Text, CASE WHEN [Value] IS NULL THEN '-' ELSE [Value] END AS Value FROM [MyTable]

Now, like I said, everything works fine, even the drop down, unless the value in the database is null, in which it doesn't work and I can't figure out why. I've tried doing different things in different events, but nothing seems to work. Am I missing something obvious? Any suggestions please? Thank you. :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
Thanks so much! I think I finally go it working with the following code (with your additions in red, my additions in blue):

Code:
Protected Sub MyGV_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles MyGV.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        If e.Row.RowState = DataControlRowState.Edit Then
            Dim view As DataView = CType(e.Row.DataItem, DataView)
            Dim theDDL As DropDownList = CType(e.Row.FindControl("MyDDL"), DropDownList)
            If Not theDDL Is DBNull.Value Then
                theDDL.SelectedIndex = theType.Items.IndexOf(theDDL.Items.FindByValue(view("Value").ToString()))
            End If
            
            [b][COLOR=blue]Dim theValue = DataBinder.Eval(e.Row.DataItem, "Value")
            theDDL.SelectedValue = theValue[/color]
        [COLOR=red]ElseIf (e.Row.RowState > DataControlRowState.Edit And e.Row.RowState > DataControlRowState.Alternate) Then
            Dim view As DataView = CType(e.Row.DataItem, DataView)
            Dim theDDL As DropDownList = CType(e.Row.FindControl("MyDDL"), DropDownList)
            If Not theDDL Is DBNull.Value Then
                theDDL.SelectedIndex = theType.Items.IndexOf(theDDL.Items.FindByValue(view("Value").ToString()))
            End If[/color]
            
            [COLOR=blue]Dim theValue = DataBinder.Eval(e.Row.DataItem, "Value")
            theDDL.SelectedValue = theValue[/color][/b]
        End If
    End If
End Sub

It seems that the way you suggested of setting the DDL by the selectedindex wasn't working, hence my added code. Thanks again! :D

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top