DataGrid problem

dr_manhattan

New member
Joined
Mar 14, 2005
Messages
3
Programming Experience
1-3
Hi there,

I have a slight problem concerning editing data within a datagrid which I would like to pick your collective brains on.

I have a datagrid, each row with an edit button. When a row is put into edit mode one of the columns is displayed as a dropdownlist.
The problem is I cannot seem to set the selected value for this dropdownlist. I have found a couple of guides which suggest I can simply set the selectedIndex property in the HTML like so:

VB.NET:
[/color][/size][size=3][color=blue]<[/color][color=maroon]EditItemTemplate[/color][color=blue]>[/color]
[color=blue][b]            <asp:DropDownList runat="server" 
                SelectedIndex='<%# GetStateIndex(Container.DataItem("state")) %>' [/b][/color]
                id="edit_State"[color=blue]>[/color][/size][size=3][color=blue]


However, in the Visual Studio property box the SelectedIndex property is greyed out
and when i attempt to run the page I receive the following error message:

[font=Arial, Helvetica, Geneva, SunSans-Regular, sans-serif] Parser Error Message: The 'SelectedIndex' property is set only by the runtime. It cannot be declared.[/font]

Any ideas on how I can get around this would be very much appreciated.

cheers,


 

dr_manhattan

New member
Joined
Mar 14, 2005
Messages
3
Programming Experience
1-3
Don't worry about it - I managed to find another way to solve the problem.

Thanks anyway
 

tomcruisetx

New member
Joined
Mar 15, 2005
Messages
3
Programming Experience
Beginner
What another way did you manage to solve your problem, please let we readers know about it.

Tom
 

dr_manhattan

New member
Joined
Mar 14, 2005
Messages
3
Programming Experience
1-3
I used the ItemDataBound event of the dataGrid and tested the event args to see if the current row was in edit mode. If it was, I was able to access the control using FindControl(String) and then alter it in anyway I wanted.

VB.NET:
Private Sub dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemDataBound
		If e.Item.ItemType = ListItemType.EditItem Then
			Dim List As DropDownList
			List = CType(e.Item.FindControl("ddl"), DropDownList)

			List.SelectedIndex = getIndex(List)
		End If
	End Sub
getIndex(ddl) simply finds the index I require
 
Last edited:
Top Bottom