Click here to Skip to main content
15,907,183 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table, a dropdownbox, and a gridview control.

I want to display all the related columns in the gridview when I select an item from the dropdownbox that I have bounded with the table.

Can somebody help me?
Posted
Updated 15-Aug-10 7:25am
v2
Comments
Dalek Dave 15-Aug-10 13:25pm    
Minor Edit for Grammar.

1 solution

lets assume you have the table named MyTable as (Column1, Column2, Column3)

you want to put Column1 values in dropdown, and show the Column2 and Column3 values related to selected Column1 value, is that right?

for that, assuming you know how to relate dropdown and table and put the Column1 value into dropdown's SelectedValue property, you can do as below, or better, use a StoredProcedure.

SqlCommand command = new SqlCommand()
{ 
   CommandText = string.Format("Select Column2, Column3 From MyTable Where Column1 = {0}", DropDown1.SelectedValue);
   Connection = new SqlConnection("Data Source=.;Initial Catalog={your database};Integrated Security=True");
   CommandType = CommandType.Text;
}

DataSet datasource = new DataSet();

using(SqlDataAdapter adapter = new SqlDataAdapter(command))
{
   adapter.Fill(datasource);
}


now use the datasource as you need, like :

GridView1.DataSoure = datasource.Tables[0];
GridView1.DataBind();


----------------------
Regards

H.Maadani
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900