Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 17th, 2003, 11:11 AM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dropdownlist within datagird

I have two database tables: tblPeron and tblArea. The structure is as follows:

[u]tblPerson</u>
PersonID
Name
AreaID

[u]tblArea</u>
AreaID
Area

I am having problems when trying use a datagrid to create/edit and delete name's of people and which area they live in. Basically, when I set a record into edit mode, the dropdownlist of the area always defaults to the first item within tblArea and so not giving a correct reflection of what is stored in the database.

My question is how do you ensure that the correct value is displayed in the dropdownlist when the record is in edit mode?

Can anyone help?

Thanks for your time
  #2 (permalink)  
Old June 23rd, 2003, 03:21 AM
Registered User
 
Join Date: Jun 2003
Location: bournemouth, dorset, United Kingdom.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What you need to do is put all the information that is to go into the droplist into a datable. You then need to send the data item in the datagrid cell to a function which will iterate through the datatable while incrementing a counter until it finds a match (eg name). This counter is returned to the droplist as its index number.


Here is an example of my code which will explain it better. First we'll start with the drop down list asp code. You'll see that when the binding data event happens and the datagrid cell turns into a drop list it the dropdownlis Edit_level calls two functions. The first is LevelDrop(), which can be seen underneath the droplist code. This returns a Datatable of data that is the DataSource of the dropList. The second function is called by the selectedIndex of the DropDownList. This is the function called GetAreaDropListIndex and can be seen under the LevelDrop function(). This function takes the string which was in the datagrid cell (and is passed to the function in the Asp code as "AwardLevelName"), it then calls the
LevelDrop() function. This again returns the same DataTable which the droplist recieved. Using a counter and a for statement, the function compares the string against all the variables in the DataTable, if a match is found the counter is returned to the droplist instructing it what index to show once it is bound to the datagrid.


<ItemTemplate>
                                                            <%# DataBinder.Eval(Container.DataItem, "AwardLevelName") %>
                                                        </ItemTemplate>
                                                        <EditItemTemplate>
                                                            <asp:DropDownList runat="server" id= "Edit_Level" Datasource= '<%#LevelDrop()%>' datatextfield= "AwardLevelName" SelectedIndex= '<%#GetLevelDropListIndex((string)DataBinder.Eval( Container.DataItem, "AwardLevelName"))%>' />
                                                        </EditItemTemplate>
                                                    </asp:TemplateColumn>



DataTable LevelDrop(){

        string ConnectionString = "server=\'diginet\'; user id=\'training\'; password=\'training\'; Database=\'training\'";
        string CommandText = "select AwardLevelName from AwardLevel";

        SqlConnection myConnection = new SqlConnection(ConnectionString);
        SqlDataAdapter myCommand = new SqlDataAdapter(CommandText, myConnection);

        DataSet ds = new DataSet();
        myCommand.Fill(ds, "LevelList");
        return ds.Tables["LevelList"];


    }






int GetAreaDropListIndex(string area){

    //returns the index setting of the area droplist, which it must display when called.
        DataTable AreaTable = AreaDrop();

        for (int i=0; i<AreaTable.DefaultView.Count; i++)
            {
            if(AreaTable.DefaultView[i]["area"].ToString()==area)
                        {
                            return i;

                         }

             }
        return 0;

      }

I'm afraid the code is in C# but the theory will be the same in VB.
  #3 (permalink)  
Old February 11th, 2005, 05:09 AM
Registered User
 
Join Date: Feb 2005
Location: Pretoria, Gauteng, South Africa.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for the code it was very helpful.
The only problem I have now is getting the selected value from the DropDownList.

I'm working with plain DataTables without the use of SQL, which makes it even more complicated. Nothing really happens when I pick a value in the DropDownList, DropDownList.SelectedItem.Text always stores the value of the DataTable, which never changes.

I've tried to use DropDownList.SelectedIndexChanged += new EventHandler(this.SetDropDownIndex), but SetDropDownIndex function never gets called.

In the normal DropDownList (outside the DataGrid) you simply call DropDownList__SelectedIndexChanged(), but inside the grid I cannot get any event.

Please help!!!!

Thank you
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
datagird to database hcmdien ASP.NET 1.x and 2.0 Application Design 0 August 19th, 2007 05:32 AM
Deleting record from Datagird with Delete Button elygp ASP.NET 1.0 and 1.1 Professional 1 May 1st, 2007 03:27 PM
Listbox in Datagird? Jolin VB Databases Basics 5 April 29th, 2007 06:10 AM
dropdownlist ngadiman BOOK: ASP.NET Website Programming Problem-Design-Solution 1 April 17th, 2007 01:06 AM
DropDownList jameee ASP.NET 2.0 Professional 1 October 18th, 2006 02:21 AM



All times are GMT -4. The time now is 11:44 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.