We help IT Professionals succeed at work.

Grid View and Drop down list in VB.NET

I created a VB.net Form with Drop down list and Grid View.

Once the Form Loads, I need the user to select the ID from the Drop down list. Based on the selection, The grid view will show ONLY the details related to the select ID.

How to achieve that?
Thanks
Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2014
Commented:
You have said VB.NET form which I assume means winforms. But then you said you are using drop down list and gridview which are both ASP.NET controls. What are you actually using?

Author

Commented:
Apologizes for the mistake.I'm using ASP.NET.
Thanks
Most Valuable Expert 2012
Top Expert 2014
Commented:
Then handle the selectedindexchanged event of the drop down list. In there, depending on your DB, put something like below

Dim dbadp As New SQLDataAdapter("Select * From table Where Column = " & dropdownlist1.SelectedValue, "your connection string")
Dim dTable As New DataTable
dbadp.Fill(dTable)

dbadp.Dispose()

Gridview1.DataSource = dTable
Gridview1.DataBind()

Author

Commented:
Dear Code Cruiser,
I added your proposed code after editing it. I've noticed that the code in the selectedindexchanged is not being executed!!!!!

I've added a msgbox inside selectedindexchanged and when I run the project, I can view the drop down box. I can select an item in it but I'm not recieveing any msg!!!! and definitly the gridview is not changing!!
Most Valuable Expert 2012
Top Expert 2014

Commented:
Set the AutoPostBack of the dropdownlist to true.

Author

Commented:
It is not working.
Is it possible the you send me the complete project. I'll change the datasource.

My database is MS SQL database.

Thanks
Most Valuable Expert 2012
Top Expert 2014

Commented:
No I can't send the complete project as I dont have it. Show me your aspx and vb files.

Author

Commented:
Attached both files for your reference.

Thanks
Default.aspx
Default.aspx.vb
Most Valuable Expert 2012
Top Expert 2014

Commented:
Why do you have

GridView1.Visible = False

at the top of your VB code?

Author

Commented:
because once I open the form, I need to see the dropdown list only. after selection, the grid view will show.
Most Valuable Expert 2012
Top Expert 2014

Commented:
You are setting the visibility to false not true.

Author

Commented:
That is true. Visibilty of the Grid view is false when I load the form. Once I select the value from the drop down list, I'm setting the visibility to True!
Most Valuable Expert 2012
Top Expert 2014

Commented:
   Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        GridView1.Visible = False

>Once I select the value from the drop down list, I'm setting the visibility to True!

YOU ARE NOT.

Author

Commented:
Appologises. It is my fault.
But anyway, I removed the  GridView1.Visible = False.

Now the DropDown list is not showing any value as if there is something wrong in the connect string!!!

Most Valuable Expert 2012
Top Expert 2014

Commented:
Do you think the connection string is correct? Does it work if you hard code the connection string in?

Author

Commented:
The connection string is correct and it is already hardcoded.
Most Valuable Expert 2012
Top Expert 2014

Commented:
>Now the DropDown list is not showing any value as if there is something wrong in the connect string!!!

How did you guess that then? Did you get any error?

Author

Commented:
Since I'm not a developer, more details was needed. But I thank CodeCrusier anyway.