Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Programitically add a Drop Down Menu inside a Datagrid

Posted on 2004-04-27
7
Medium Priority
?
233 Views
Last Modified: 2012-05-04
Hi
  This is what my Datagrid looks like
Name     Phone          Rank               Years Participated      View Details

All the columns are sortable. Now, I have people have have participated more that 1 year. So I  want to group the entries by Name and then put all the years participated in a drop down menu. When a person selects a year it should then jump to the details page. Is it possible to put this functionality inside a Template Column somehow. Does anyone have any experience with this.

Thanks
0
Comment
Question by:BrijBhasin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 9

Expert Comment

by:Thalox
ID: 10936390

I assume your dropdow menu is a dropDownList-Control which has values like

1999
2000
...
and on selecting one entry the user should be redirected to another page, right?

if so, you could place the list in a template-column or by code, but you allways have to fill in the value by yourself
(I dont know a way to let .net do this for you, but maybe there is...)

so i would prefer to add the controls by code after the databinding of the grid.
go through each row of your datasource, get the id of your people and with this select the years and put them into the new created list.
add an event-handler for the SelectedIndexChanged event

to determine, which control raised the event, set the name of each dropdownlist-control to something containing the id of the corresponding people
this id and the selected year could then be passed to your details-page


maybe there is an easier solution for this, but it works for me.
if you need any sample code let me know


Thalox
0
 

Author Comment

by:BrijBhasin
ID: 10939745
Hi Thalox,
  I think your explanation is pretty close to what I'm looking for. Could you give me some sample code?

Thanks
0
 
LVL 9

Expert Comment

by:Thalox
ID: 10940079

try this


private void BindData()
{
      // load Data (Name     Phone          Rank)
      DataSet ds = "<replace with your datasource>";

      Grid.dataSource = ds;
      Grid.DataBind();


      // go through rows an add lists
      for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
      {
            DropDownList dl = new DropDownList();
            
            // set name
            dl.ID = "dl_" + ds.Tables[0].Rows[i]["id"].ToString();
            //fill list (replace with own code)
            dl.DataSource = "select years from x where id = '" + ds.Tables[0].Rows[i]["id"].ToString() + "'";
            dl.DataBind();
            dl.SelectedIndex = 0;
            // add eventhandler
            dl.SelectedIndexChanged +=new EventHandler(dl_SelectedIndexChanged);

            // add to grid (need to replace 3 with right index)
            Grid.Items[i].Cells[3].Controls.Add(dl);
      }
}

private void dl_SelectedIndexChanged(object sender, EventArgs e)
{
      // get id and year
      string id = ((DropDownList)sender).ID.Replace("dl_", "");
      string year = ((DropDownList)sender).SelectedValue;

      // go to details
      Response.Redirect("details.aspx?id=" + id + "&year=" + year);
}


I think this should work,


Thalox

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:BrijBhasin
ID: 10943432
Hi Thalox,
  I'm trying to convert your code into VB and I get an error at

do while i < dstUserListingB.Tables[0].Rows.Count

BC30452: Operator '<' is not defined for types 'Integer' and 'System.Data.DataTableCollection'.

Do you know how I can fix this.

Thanks,
Brij
0
 
LVL 9

Expert Comment

by:Thalox
ID: 10946553

hmm,

are you sure, the error is caused by this line?
it looks right to me, but i dont have much experience in vb

is this the right syntax for a do-while-loop?
in c# it is
do {
...

} while <expression>

it seems that "dstUserListingB.Tables[0].Rows.Count" is only evaluated to "dstUserListingB.Tables"

could you post a little more of your code?

Thalox
0
 
LVL 4

Expert Comment

by:Kittrick
ID: 10953721
There is a pretty awesome and comprehensive article on how to do it at:

http://www.4guysfromrolla.com/webtech/050801-1.shtml
0
 
LVL 9

Accepted Solution

by:
Thalox earned 500 total points
ID: 10960953

Hi Brij,

i just "discovered" that vb uses (i) as indexer insted of [i] in c#
so i think replacing all [] by () should solve the problem.

Thalox
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question