Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

set Dropdownlist SelectedIndex in a DataGrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
marcbb - 29 May 2007 11:17 GMT
Hi all,

I have a really strange problem working with Dropdownlists in a
DataGrid.

I'm trying to preselect some values from the DropDownlist for each
row in the DataGrid, but it seems that both selectedIndex has the same
reference and modifying the first one also automatically modify the
second one. For example:

 A     <DDL_1>   <DDL_2>
 B     <DDL_1>   <DDL_2>
  ...    ...               ...

For each row (i) in the DataGrid, I make:

[1]
//Get Dropdownlist 1
DropDownList ddl1 = (DropDownList)
dgList.Items[i].FindControl("DDL_1");

//Get Dropdownlist 2
DropDownList ddl2 = (DropDownList)
dgList.Items[i].FindControl("DDL_2");

and then, I preselect their SelectedIndex:

[2]  I have this function for this:

     public void SelectDropDownValue(DropDownList ddl, string
selectedValue)
        {
            ListItem li = null;
            li =  ddl.Items.FindByValue(selectedValue);
            if (li != null)
            {
                int index = ddl.Items.IndexOf(li);
                ddl.SelectedIndex = index;
            }
        }

 when I make:

    //here, ddl1.SelectedIndex = 0 and ddl2.SelectedIndex = 0

 SelectDropDownValue(ddl1, val1);  //after this, ddl1.SelectedIndex =
val1 and ddl2.SelectedIndex = 0

 SelectDropDownValue(ddl2, val2);   //after this, ddl1.SelectedIndex
= val2 and ddl2.SelectedIndex = val2

//after this, every change that i make to ddl1.SelectedIndex is also
affecting at the same time to ddl2.SelectedIndex and IDEM with
VICEVERSA.

 Is it normal?

Thanks in advance
marcbb - 29 May 2007 16:47 GMT
Ok, finally I've resolved the problem.

It was at the initialization of the DDL in ItemDataBound function. I
was doing:

private void dgList_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
....
    ListItem li = new ListItem(valDesc, valId);
    ddl1.Items.Add(li);
    ddl2.Items.Add(li);
...
}

 I was initializing both DropDownlists with the same object (ListItem
li). It makes its content has the same reference (or pointer) and
changing the content of one of them also mofify the other one because
they were pointing to the same place (or at least, it seemed).

 Then, the solution was to initializate each control with a different
ListItem. Something like this:

private void dgList_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
....
    ListItem li = new ListItem(valDesc, valId);
    ddl1.Items.Add(li);
    ListItem li2 = new ListItem(valDesc, valId);
    ddl2.Items.Add(li2);
...
}

Thanks for your attention.

Regards

> Hi all,
>
[quoted text clipped - 54 lines]
>
>  Thanks in advance

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.