0

I have 6 dropdownlist that are populated from a sqldatasource. I also have another sqldatasource that returns some rows. What I want to do is to go through each row of the second datasource and select that value in my dropdowns. So if second datasource contains three rows it would select the appropriate value in the first three dropdownlist and set the others to "N/A" for example.

here is some pseudo code I thought of

protected void fileattributes_ItemDataBound(object sender, ListViewItemEventArgs e)
{
        DropDownList kw1 = (DropDownList)e.Item.FindControl("keyw1");
        DropDownList kw2 = (DropDownList)e.Item.FindControl("keyw2");
        DropDownList kw3 = (DropDownList)e.Item.FindControl("keyw3");
        DropDownList kw4 = (DropDownList)e.Item.FindControl("keyw4");
        DropDownList kw5 = (DropDownList)e.Item.FindControl("keyw5");
        DropDownList kw6 = (DropDownList)e.Item.FindControl("keyw6");

        DropDownList[] array = { kw1, kw2, kw3, kw4, kw5, kw6 };

        for(int i = 0; i<sqldatasource2.length;i++)
        {            
              array[i].SelectedItem.Text = sqldatasource2.item    
        }

        foreach(Array a in array)
        {    
              if (a is null)
              {    
                   a.selecteditem.text = "N/A";
              }        
        }        
}    
1
  • Hope my answer solves the problem Commented Nov 16, 2012 at 7:13

2 Answers 2

1
DataSourceSelectArguments args = new DataSourceSelectArguments();
DataView view = (DataView)SqlDataSource1.Select(args);
DataTable dt = view.ToTable();

DropDownList[] array = { kw1, kw2, kw3, kw4, kw5, kw6 };
int i;
for (i = 0; i < dt.Rows.Count;i++ )
{
    array[i].SelectedItem.Text = dt.Rows[i][0].ToString();
}

// If array length (number of DropDowns) is greater than rows in datasource
// Remaining Dropdowns will get text N/A --- Now i=dt.Rows.Count
for (; i < array.Length; i++)
{
    array[i].SelectedItem.Text = "N/A";
}
Sign up to request clarification or add additional context in comments.

1 Comment

the datasourceselectarguments is that any arguments I pass the sqldatasource? How can I create one instead of passing a blank one?
1

I'm not very experienced working directly with the sqldatasource, so what I did was to fill a DataTable with the result set. Hope this works!

    DropDownList kw1 = (DropDownList)e.Item.FindControl("keyw1");
    DropDownList kw2 = (DropDownList)e.Item.FindControl("keyw2");
    DropDownList kw3 = (DropDownList)e.Item.FindControl("keyw3");
    DropDownList kw4 = (DropDownList)e.Item.FindControl("keyw4");
    DropDownList kw5 = (DropDownList)e.Item.FindControl("keyw5");
    DropDownList kw6 = (DropDownList)e.Item.FindControl("keyw6");

    DropDownList[] array = { kw1, kw2, kw3, kw4, kw5, kw6 };

    DataView dv = new DataView();
    DataTable dt = new DataTable();    
    dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    dt = dv.ToTable();

    int i = 0;
    foreach (DataRow dr in dt.Rows)
    {
        // dr["column name or column index"] (zero in my case)
        array[i].SelectedItem.Text = dr[0].ToString();
        i++;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.