• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

ASP.Net/C# - Dropdownlist Question selected value

Hello all.  I have a dropdown list that gets bound to a dataset I have.  In that dataset there is a field called Primary and its either Y or N.  This field is in the dataset but not my DataValueField or DataTextField.  What I want to do is after the bind select the value that has the Primary = Y.  Any idea how I can do that after the bind?  thanks all
0
sbornstein2
Asked:
sbornstein2
  • 3
  • 3
2 Solutions
 
strickddCommented:
dropdownObj.SelectedValue = "VALUE OF PRIMARY"

OR

for(int i=0; i<ROWCOUNT; i++)
{
    ListItem item = new ListItem(reader[TEXTFIELD].ToString(), reader[VALUEFIELD].ToString());
   
    dropdownObj.Items.Add(item);

    if(Convert.ToBoolean(reader[PRIMARY]))
    {
        dropdownObj.SelectedValue = reader[TEXTFIELD].ToString();
    }

    reader.MoveNext();
}
0
 
sbornstein2Author Commented:
dropdownObj.SelectedValue = "VALUE OF PRIMARY" would that be something like

dropdownObj.SelectedValue = "Primary = Y" or something?   Again the Primary is not actually the value field or the text field in the drop.
0
 
strickddCommented:
If you want to do it all with one SQL call you will have to loop through the DataReader and create each list item and check if it is primary or not. If you want to just use "dropdownObj.SelectedValue = "VALUE OF PRIMARY"" then you will need to do another SQL call to get the primary: SELECT TEXTFIELDCOLUMN FROM TABLE WHERE PRIMARY = 'Y'
0
Independent Software Vendors: 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!

 
spdudeCommented:
Hi sbornstein2

First loop through each row to find which row has primary value = y
DataSet ds = new DataSet();
        int index = 0;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            index++;

            if (dr["primary"] == "Y")
            {
                break;
            }
        }


After finding the index set dropdown selected index to found index
        DropDownList1.SelectedIndex = index;
   

regards
snehal
0
 
sbornstein2Author Commented:
actually I have a dataview is it the same code for the dataset?  thanks guys
0
 
strickddCommented:
I believe they are similar. Either way you should be able to loop through the set and find the Primary = Y and set the .SelectedValue equal to that rows value.
0
 
sbornstein2Author Commented:
thank you
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now