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.

 All Forums
 Development Tools
 ASP.NET
 Datagrids ruined my life

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 help

steve



Private Sub BindDGRow()

' bind the datagrid dropdowns for when editing called from dgClinOutcome_EditCommand

Dim ddIndication As DropDownList = CType(Me.dgClinOutcome.Items(Me.dgClinOutcome.EditItemIndex).FindControl("ddInd"), DropDownList)

Dim strConn As String

strConn = "My connection string"

Dim dgConn As SqlConnection = New SqlConnection(strConn)

Dim daDisease As New SqlDataAdapter

Dim sel_Disease As New SqlCommand

daDisease.SelectCommand = New SqlCommand

daDisease.SelectCommand.Connection = dgConn

daDisease.SelectCommand.CommandType = CommandType.StoredProcedure

daDisease.SelectCommand.CommandText = "dbo.[sproc_selDisease]"

Try

daDisease.Fill(DsDGRow, "Indication")

daDisease.SelectCommand.Connection.Open()

daDisease.SelectCommand.ExecuteReader()

daDisease.SelectCommand.Connection.Close()

Catch

Response.Redirect("..\MyError.aspx")

End Try

'Now create the dataviews to bind the objects to

Dim dv_indication As DataView

dv_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 down

With 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 -1

End With

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

With e.Item



If .DataItem("CH_Indication") = "Unknown" Then

.Cells(10).BackColor = Color.Red

.Cells(10).ForeColor = Color.White

.Cells(10).Font.Bold = True

End If



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

If e.Item.ItemType = ListItemType.EditItem Then

Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)

Dim currentIndication As String = CType(drv("CH_Indication"), String)

Dim ddIndication As DropDownList

ddIndication = CType(e.Item.FindControl("ddInd"), DropDownList) 'CType(Me.dgClinOutcome.Items(Me.dgClinOutcome.EditItemIndex).FindControl("ddIndication"), DropDownList)

Try

ddIndication.SelectedIndex = ddIndication.Items.IndexOf(ddIndication.Items.FindByText(currentIndication))

Catch

ddIndication.SelectedIndex = -1

End Try

End If



End 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 Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

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?

thanks

steve

-----------

Don't worry head. The computer will do all the thinking from now on.
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2006-09-01 : 10:25:34
I gathered that.

This code:
Try

ddIndication.SelectedIndex = ddIndication.Items.IndexOf(ddIndication.Items.FindByText(currentIndication))

Catch

ddIndication.SelectedIndex = -1

End Try

is not working. I was trying to understand if the exception was being thrown and why.

So, make sure that

ddIndication is not Nothing

then for debugging purposes, break down this statement
ddIndication.Items.IndexOf(ddIndication.Items.FindByText(currentIndication))

into

Dim CurrentItem as ListItem = ddIndication.Items.FindByText(currentIndication)
Dim CurrentIndex as Integer = -1
if not isNothing(CurrentItem) then
CurrentIndex = ddIndication.Items.IndexOf(CurrentItem)
else
System.Diagnostics.Debug.WriteLine(String.Format("Can't find :{0}", currentIndication))
end if
ddIndication.SelectedIndex = CurrentIndex

Also, why are you using the text value of the indication? If you were using an ID you could just set the SelectedValue property.







Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2006-09-01 : 13:16:43
Hi Dean

Many 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 again

steve

-----------

Don't worry head. The computer will do all the thinking from now on.
Go to Top of Page
   

- Advertisement -