Dropdowlist in a gridview in edit mode

L

Lupakkiotto

Hi!, I am using framework 2.0 and Visual Visual Web Developer Express.
In a gridview, when it is in editmode, I have a dropdownlist in a
templatefield. The problem is that it show always the first item in the list
and not the current one correspondin to the value saved in the database....
How can I solve the problem? I have read about the event itemdatabound of
the gridview, but if I try the code below:

Sub GridView1_ItemDataBound(ByVal Sender As Object, ByVal e As
DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.EditItem Then
Dim ddl As DropDownList
ddl = CType(e.Item.FindControl("comboTipoIndennita"), DropDownList)
ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByText(DataBinder.Eval(e.Item.DataItem,
"comboTipoIndennita")))
End If
End Sub

it says me that the gridview can't handles that event...
Thanks in advance and sorry for my english...
Marco
 
N

NIan

Sub GridView1_ItemDataBound(ByVal Sender As Object, ByVal e As
DataGridItemEventArgs) <--DataGridItemEventArgs is that suppose to be
GridView.... EventArgs?
 
L

Lupakkiotto

NIan said:
Sub GridView1_ItemDataBound(ByVal Sender As Object, ByVal e As
DataGridItemEventArgs) <--DataGridItemEventArgs is that suppose to be
GridView.... EventArgs?


what you mean? I am not very skilled.. can you paste an example of code that
work?
TNX
 
L

Lupakkiotto

Patrick.O.Ige said:
Try going through this at:-
http://winfx.msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref
/html/E_System_Web_UI_WebControls_GridView_RowDataBound.asp
It should guide you.
Patrick

thank.. I tried to modify the code in:
Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
GridViewRowEventArgs)

If e.Row.RowType = ListItemType.EditItem Then

Dim ddl As DropDownList

ddl = CType(e.Row.FindControl("comboTipoIndennita"), DropDownList)

'ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByText(DataBinder.Eval(e.Row.DataItem,
"comboTipoIndennita")))

ddl.SelectedValue =
ddl.Items.IndexOf(ddl.Items.FindByText(DataBinder.Eval(e.Row.DataItem,
"TipoIndennita")))

End If

End Sub





but nothing happen...
 
L

Lupakkiotto

I solved my problem in a very simple way:
<asp:DropDownList runat="server" ID="comboTipoIndennita"
DataSourceID="SqlDataSource2" DataTextField="TipoIndennita"
DataValueField="idIndennitaGruppo" CssClass="combo" SelectedValue='<%#
Bind("myID")%>' />

setting SelectedValue='<%# Bind("myID")%>
Tnx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top