kmizle kmizle - 3 years ago 83
Vb.net Question

Want to set dropdown list to the first value in a datatable asp.net

I have a drop down list, GroupDropDown, and a data table, GroupDataTable. I have a select statement that only produces one value, i want to set the drop down selected value to the one value in the data table. I've tried a bunch of different stuff and still can't seem to figure it out. Like most errors, I'm sure I am making a silly mistake, but here is my code:

Dim Region As String = Encryption64.Decrypt(Request.QueryString("Region"))
Dim MySqlString As String = "Select GROUPNAME, GROUPOWNERSHIP from Table1 where GROUPNAME like '%" & Region & "%'"

Dim GroupDataTable As DataTable = OleFun.GetMyDataTableString(MySqlString, 2)

GroupDropDown.DataSource = GroupDataTable
GroupDropDown.DataBind()

For Each row As DataRow In GroupDataTable.Rows
Response.Write(row(1))
Next

GroupDropDown.SelectedValue = ?

GetData()


Thanks in advance for your responses

Answer Source

You can select a row and column from a DataTable by name or index and use that value as the SelectedValue

GroupDropDown.SelectedValue = GroupDataTable.Rows[0]["myColumn"].ToString();

//or by column index

GroupDropDown.SelectedValue = GroupDataTable.Rows[0][7].ToString();

VB

GroupDropDown.SelectedValue = GroupDataTable.Rows(0)("myColumn").ToString
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download