Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2006-08-31 : 09:53:03
|
I am using ASP.NET 1.1 I have created a datagrid which is used for correcting data. I have dropdowns in the edititem template and they are populated correctly, the problem I have is pre-selecting the existing value where there is one. I've more or less copied the code for this from the microsoft site [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchTopQuestionsAboutASPNETDataGridServerControl.asp[/url] but for completeness have shown my code below, there are two routines. If anyone has any suggestions on how to fix this I would be very grateful. I don't think the ASP is a problem but am happy to post it if anyone thinks it will helpsteve Private Sub BindDGRow()' bind the datagrid dropdowns for when editing called from dgClinOutcome_EditCommandDim ddIndication As DropDownList = CType(Me.dgClinOutcome.Items(Me.dgClinOutcome.EditItemIndex).FindControl("ddInd"), DropDownList)Dim strConn As StringstrConn = "My connection string"Dim dgConn As SqlConnection = New SqlConnection(strConn)Dim daDisease As New SqlDataAdapterDim sel_Disease As New SqlCommanddaDisease.SelectCommand = New SqlCommanddaDisease.SelectCommand.Connection = dgConndaDisease.SelectCommand.CommandType = CommandType.StoredProceduredaDisease.SelectCommand.CommandText = "dbo.[sproc_selDisease]" TrydaDisease.Fill(DsDGRow, "Indication")daDisease.SelectCommand.Connection.Open()daDisease.SelectCommand.ExecuteReader()daDisease.SelectCommand.Connection.Close()CatchResponse.Redirect("..\MyError.aspx")End Try'Now create the dataviews to bind the objects toDim dv_indication As DataViewdv_indication = New DataView(DsDGRow.Tables("Indication"))dv_indication.Sort = "OMIM Number" 'This is a number that is stored as text, it is also the PK and the value that the users will see in the drop downWith ddIndication.DataTextField = "OMIM Number" .DataValueField = "OMIM Number".DataSource = dv_indication.DataBind()ddIndication.Items.Insert(0, New ListItem("Select a value", "-1")) ' Put this in for the case that there is no existing data, for that case, the selecteditem value should be -1End WithEnd Sub Private Sub dgClinOutcome_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgClinOutcome.ItemDataBound'Bind the data to the drop down and pre-select any existing value, if there is no value set the selectedindex to -1' now format the cells with missing valuesWith e.ItemIf .DataItem("CH_Indication") = "Unknown" Then.Cells(10).BackColor = Color.Red.Cells(10).ForeColor = Color.White.Cells(10).Font.Bold = TrueEnd IfEnd With Pre-select item on drop down if item exists' from http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dv_vstechart/html/vbtchTopQuestionsAboutASPNETDataGridServerControl.asp#vbtchtopquestionsaboutaspnetdatagridservercontroldisplayingadrop-downlistineditmodeIf e.Item.ItemType = ListItemType.EditItem ThenDim drv As DataRowView = CType(e.Item.DataItem, DataRowView)Dim currentIndication As String = CType(drv("CH_Indication"), String)Dim ddIndication As DropDownListddIndication = CType(e.Item.FindControl("ddInd"), DropDownList) 'CType(Me.dgClinOutcome.Items(Me.dgClinOutcome.EditItemIndex).FindControl("ddIndication"), DropDownList)TryddIndication.SelectedIndex = ddIndication.Items.IndexOf(ddIndication.Items.FindByText(currentIndication))CatchddIndication.SelectedIndex = -1End TryEnd IfEnd Sub-----------Don't worry head. The computer will do all the thinking from now on. |
|
dfiala
Posting Yak Master
116 Posts |
Posted - 2006-08-31 : 11:07:40
|
What problem are you having? Is is not finding the dropdown?Is it not finding the selected Item?Dean FialaVery Practical Software, IncNow with Blogging...http://www.vpsw.com/blogbabyMicrosoft MVP |
 |
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2006-09-01 : 03:17:24
|
Sorry I obviously didn't explain it very well. The drop down is not showing the existing value as a pre-selected item. So for example the columns that are invalid will make the selecteditem in the drop down equal to -1, but the valid entries will make it the appropriate value (e.g. the tenth value) from the drop down when the row is being edited.Is that clearer?thankssteve-----------Don't worry head. The computer will do all the thinking from now on. |
 |
|
dfiala
Posting Yak Master
116 Posts |
Posted - 2006-09-01 : 10:25:34
|
I gathered that. This code:TryddIndication.SelectedIndex = ddIndication.Items.IndexOf(ddIndication.Items.FindByText(currentIndication))CatchddIndication.SelectedIndex = -1End Tryis not working. I was trying to understand if the exception was being thrown and why.So, make sure thatddIndication is not Nothingthen for debugging purposes, break down this statementddIndication.Items.IndexOf(ddIndication.Items.FindByText(currentIndication))intoDim CurrentItem as ListItem = ddIndication.Items.FindByText(currentIndication)Dim CurrentIndex as Integer = -1if not isNothing(CurrentItem) then CurrentIndex = ddIndication.Items.IndexOf(CurrentItem)else System.Diagnostics.Debug.WriteLine(String.Format("Can't find :{0}", currentIndication))end ifddIndication.SelectedIndex = CurrentIndexAlso, why are you using the text value of the indication? If you were using an ID you could just set the SelectedValue property.Dean FialaVery Practical Software, IncNow with Blogging...http://www.vpsw.com/blogbabyMicrosoft MVP |
 |
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2006-09-01 : 13:16:43
|
Hi DeanMany thanks for your input yet again!I was using the text value because that was originally different to the index and I wouldn't know the index,however that has now changed and so the code has been changed.I'll try the rest of your suggestion on Monday as I'm now at home (time differences and all that)Thanks once againsteve-----------Don't worry head. The computer will do all the thinking from now on. |
 |
|
|
|
|
|
|