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
 
Sorry, I forgot to mention that the error I get is:

'MyDDL' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

I know what it's trying to say, but I don't see how it's possible. Please help if you can, thanks. :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
what code are you using to insert the -,-

when it does "work" does the - show in the list?

im thinking the 0 list item is not getting in there soon enough.

should be ddl.Items.Insert(0, new ListItem("-","-") not ddl.Item.Add right?


 
For inserting, I've tried both:
Code:
Dim aItem As ListItem = New ListItem("-", "-")
MyDDL.Items.Insert(0, aItem)

and
Code:
MyDDL.Items.Insert(0, "-")
which didn't work. I'm also thinking that the dash isn't getting inserted soon enough, before the gridview is bound or something, though I just don't know which event to use (if any) that will solve this.

If I choose to edit a row in the gridview that has values in the column that aren't NULL, then the DDL works just fine. The correct text is selected and all of the other options, including the "-", are there for the choosing.

Any other suggestions? Thanks.

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
well, when you populate the data to the DDL, thats when you should insert the new item. Im not so sure the OnDataBound event of the ddl is the right spot.

ddl.DataSource = ds.Tables(0)
ddl.DataBind()
ddl.Items.Insert(0, New ListItem("-","-"))

Make sure you add the text and the value

 
I don't populate the DDL like that. I'm using a SqlDataSource to populate it. The OnDataBound event is the only way in the past that I've done it before to add the "-". Is there a better way? I guess I could do away with the SqlDataSource, but damn it would be a pain to change a bunch of my code relating to that. Thanks for the suggestion though. :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
Also, if I remove the SqlDataSource and bind the data manually, and I bind it in, say, the gridview OnDataBound event, how do I find the DDL control so I can bind it? Everything I've tried has failed and it's not the same as it was in ASP.NET 1.1 (I'm using 2.0). Something like:

Code:
Dim theDDL As DropDownList = theGV.FindControl("MyDDL")
Don't work. Thanks again. :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
well, lets just eliminate this as the problem. Statically add the first item to your HTML, and append the items. Dont use the OnDataBound Event.

<asp:DropDownList id="MyDDL" runat="server" AppendDataBoundItems="true">
<asp:ListItem Value="-">-</asp:ListItem>
</asp:DropDownList>

If it works, then that was the problem, and you can leave it like this, if not, then we'll need to look further.
 
Ah, I forgot about the AppendDataBoundItem property, but unfortunately it didn't fix anything. I still get the same error. Any other suggestions? Thanks so much for your help. :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
It has be something with your SqlDataSources then. Try with two SqlDataSources???


The SqlDataSource was supposed to make things easier,but i continually see it causing issues, especially when additonal customizations are wanted. (not saying this is really a customization, as it should work)
 
since the list is bound at runtime you can't use this
Code:
<asp:DropDownlist .. SelectedValue='<%# Bind("Value") %>' />
try this instead
1. define the rowdatabound evnet
2. determine if the row is in edit mode
3. get data item
4. get drop down list
5. manually set the value
Code:
//aspx
<asp:Gridview OnRowDataBound="rowDataBound">
...
</asp:GridView>

//code behind
protected void rowDataBound(object source, RowDataBoundEventArgs e)
{
   if (is in edit mode)
   {
      DataView view = (DataView)e.Row.DataItem;

      DropDownList list = (DropDownList)e.Row.FindControl("mydropdownlist");
      if(list != null)
      {
         list.SelectedIndex = list.Items.IndexOf(list.Items.FindByValue(view["MyColumn"].ToString()));
      }
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks, but that doesn't seem to fully work. When I test it, and I choose to Edit a row that actually has a value and not the "-", it defaults to the "-". And then when I choose a row that has the "-", it works fine. But then I go back and choose a row with actual data again, and it blows up with the following error:

Unable to cast object of type 'System.Data.DataRowView' to type 'System.Data.DataView'.

...

Line 110: Dim view As DataView = CType(e.Row.DataItem, DataView)

Unless I converted your C# code to VB.NET incorrectly, this is what I have:

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
        End If
    End If

Any other suggestions? Thanks so much. :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
Oops, sorry, forgot to add the
Code:
End Sub
at the end of the code fragment above. :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
ok, so change the type and the cast will go away.
Code:
Dim view As DataRowView = CType(e.Row.DataItem, DataRowView)
also use an empty string for the "-" value not a "-"
Code:
<asp:DropDownList id="MyDDL" runat="server" AppendDataBoundItems="true">
   <asp:ListItem Value="" Text="-" />
</asp:DropDownList>

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
OK, the DataRowView fixed the first error. But the ListItem edit doesn't help anything. Now, no matter which row I choose to edit, it always defaults to the "-" (or basically selectedindex 0). Any ideas? :D

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
I'm seeing that every other row is working fine, so it's doing something with alternating rows, but I can't see in my code where it would be doing that. Strange...

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
I think you need a bitwise operator to check if it's edited something like
Code:
if (e.Row.RowState > RowState.Edit && RowState.Alternate)
{
   ...
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
If I remember correctly, I changed the following code and added the alternate part, but then the gridview blew up on me due to the fact it couldn't find the DDL, like:

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 [b][COLOR=red]Or e.Row.RowState = DataControlRowState.Alternate[/color][/b] 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
        End If
    End If
End Sub

Unless I'm supposed to be putting stuff in a different location. Thanks again. :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
a bitwise operator is different than and and/or key word.
Code:
If (rowState And DataControlRowState.Edit) <> 0 _
    OrElse (rowState And DataControlRowState.Insert) <> 0 
Then
   radio.Enabled = True
End If

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top