Aashish, How are you?
Here's the story: you need to feed the SelectedIndex to your datagrid. The problem is finding the index of the selected item...
Let's say you are trying to select the state that a student lives in and you have a database of states (we're just pretending). Here's what will happen:
1. You are going to feed the state you want selected to the function
2. You'll get your list of states from the database - in alpha order, of course
3. the dataset will loop through the records and compare the value you fed to the database to the value of each row
4. When it finds a value that matches, it will return a value of 'x' which has, as if by magic, been incrementing along with the rows
5. x is your index. For example, Alabama = 0, Alaska = 1...
Call this function in the selectedIndex property of your dropdownlist:
Code:
Function findIndex(ByVal sStr As String)
Dim x As Integer = 0
Dim ds As New DataSet
Dim sqlStr As String = "select YourValue from YourTable"
Dim da As New SqlDataAdapter(sqlStr, conn) '(or whatever your connection is)
da.Fill(ds)
Dim dr As DataRow
For Each dr In ds.tables(0).rows
If sStr = dr(0) Then
Return x
Exit For
x += 1
End If
Next
End Function
I didn't think of this by myself, I got if from someone online. Actually, I might have got it from Scott Mitchell's ASP.Net Data Web Controls book. A most excellent read.
Let me know if this works.
mark