Populating gridview from listbox

Hello all,

I would like to know how to populate a gridview based on user selected option in a listbox.

I have a dropdownlist which is connected to a database and a listbox displaying items when the user selects an option in the dropdownlist. I'm able to populate the dropdownlist and th listbox but how can I populate the gridview based on user selection. I want to display the gridview when the user chooses the option in the listbox and clicks a button called "view".

I'm a beginner, so go easy on me.

I'm using Visual Studio 2005, c# and sql server 2005.  

Here is my code.

populateddl  is the dropdownbox
setzipcode is the listbox


Thanks for the help.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using MITS.BL;
 
public partial class BulkExtractM : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
        if (!(IsPostBack))
        {
            PopulateDDL();
        }
 
    }
 
 
    protected void PopulateDDL()
    {
 
        CHHExtractBL_Impl blHHExtract = new CHHExtractBL_Impl();
        DataSet ds = null;
        ds = blHHExtract.GetContractArea("<Root><ContractorID>1</ContractorID></Root>");
 
        DropDownContractorArea.DataSource = ds.Tables[0];
        DropDownContractorArea.DataTextField = "CITYMAP_Description";
        DropDownContractorArea.DataValueField = "CITYMAP_ID";
        DropDownContractorArea.DataBind();
    }
 
    protected void DropDownContractorArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        SetZipCode(DropDownContractorArea.SelectedValue.Trim());
    }
 
    private void SetZipCode(string strContractArea)
    {
 
        CHHExtractBL_Impl blHHExtract = new CHHExtractBL_Impl();
        DataSet ds = null;
        string inXML = "<Root><ContractArea>" + strContractArea + "</ContractArea>";
        //Change to session
        inXML += "<ContractorID>1</ContractorID></Root>";
            ds = blHHExtract.GetZipCode(inXML);
 
        ds = blHHExtract.GetZipCode(inXML);
        ContractAreaZipCode.DataSource = ds;
 
        ContractAreaZipCode.DataTextField = ds.Tables[0].Columns["ZIP_CODE"].ToString();
        ContractAreaZipCode.DataValueField = ds.Tables[0].Columns["ZIP_CODE"].ToString();
        ContractAreaZipCode.DataBind(); 
 
 
 
    }
 
 
    protected void btnView_Click(object sender, EventArgs e)
    {
 
    }
}

Open in new window

AvinashKNAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dhanushkadCommented:
-> Set the AutoPost back property of the DropDownContractorArea into false
DropDownContractorArea.AutoPostBack = false;


-> delete the  DropDownContractorArea_SelectedIndexChan method.

-> modify code as follows...
 protected void Page_Load(object sender, EventArgs e)
    {
 
        if (!(IsPostBack))
        {
            PopulateDDL();
        }
        else
        {
            SetZipCode(DropDownContractorArea.SelectedValue.Trim());
        }
 
    }
 
 
protected void btnView_Click(object sender, EventArgs e)
{
    SetZipCode(DropDownContractorArea.SelectedValue.Trim());
}

Open in new window

0
AvinashKNAuthor Commented:
I think you misunderstood my question. My dropdownlist and listbox are working fine. My question is when a user clicks on an item in the listbox, I need the gridview to be populated based on the selection.

The listbox contains zipcodes. If the user clicks on one zipcode, I need the gridview to access the database and show the account information of that zipcode.

I hope I'm not confusing everyone. Sorry if I did.
0
dhanushkadCommented:
Sorry, I am not i have misunderstood your question properly. I think you need to postback the page and rebind some new data into the grid.

Set the AutoPostBack to true of the dropdown. When you change the seleted item in the dropdown, it will post back the page into the server and reload the page. I think this is what you want? Sorry for interuption..

DropDownContractorArea.AutoPostBack = true;

http://www.dotnetspider.com/resources/189-AutoPostBack-What-How-works.aspx


 protected void DropDownContractorArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        SetZipCode(DropDownContractorArea.SelectedValue.Trim());
    }

Open in new window

0
dhanushkadCommented:
Sorry, I am not sure that i have misunderstood your question properly. I think you need to postback the page and rebind some new data into the grid.

Set the AutoPostBack property of the dropdown to true, then when you change the seleted item in the dropdown, it will post back the page into the server and reload the page. I think this is what you want? Sorry for interuption..

DropDownContractorArea.AutoPostBack = true;

http://www.dotnetspider.com/resources/189-AutoPostBack-What-How-works.aspx

 protected void DropDownContractorArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        SetZipCode(DropDownContractorArea.SelectedValue.Trim());
    }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dhanushkadCommented:
*  Sorry, I am not sure that i have understood your question properly.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.