Solved

Adding an item to an embedded dropdownlist CONTROL

Posted on 2006-07-06
11
256 Views
Last Modified: 2010-04-16
Hi experts,

Does any one know how I can add an item to dropdownlist CONTROL which
resides in a datagrid? e.g
      <EditItemTemplate>
                                                                              <asp:DropDownList id="DeductDropDownList" Runat="Server" DataSource="<%# GetDeductionDataTable() %>" DataTextField="Deduction" DataValueField="Deduction">
                                                                                    <asp:ListItem Text="Either" Value="Either"></asp:ListItem>
                                                                              </asp:DropDownList>
                                                                        </EditItemTemplate>
                                                                  </asp:TemplateColumn>
                                                                  <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="save" ItemStyle-Width="50" ButtonType="LinkButton" />
                                                            </Columns>

the issue here is how do i reference the dropdownlist and add an item.

I found this, but not really what i want
http://authors.aspalliance.com/aldotnet/examples/dynamicdatasource.aspx

thanks
      
0
Comment
Question by:SirReadAlot
[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
  • 7
  • 4
11 Comments
 

Author Comment

by:SirReadAlot
ID: 17050165
this populates the dropdown
public DataTable GetDeductionDataTable()
            {
   
                  if (Cache["DeductionDataTable"] == null)
                  {
 

                        string ConnectionString = ConfigurationSettings.AppSettings["connectLocal"];
                        SqlConnection  myConnection = new SqlConnection(ConnectionString);

                        SqlCommand myCommand = new SqlCommand("sp_GetDeduction", myConnection);
                        myCommand.CommandType = CommandType.StoredProcedure;
                        
                        SqlDataAdapter  objDataAdapter = new SqlDataAdapter(myCommand);
                  
                        DataSet objDataSet = new DataSet();
   

                        try
                        {
                              objDataAdapter.Fill(objDataSet, "DeductionDataTable");
                             Cache.Insert("DeductionDataTable", objDataSet.Tables["DeductionDataTable"]);
                                    }
   
                        catch (SqlException SqlEx)
                        {
                              lblStatus.Text = SqlEx.Message.ToString();
                        }
   
                        catch (Exception Ex)
                        {
                              lblStatus.Text = Ex.Message.ToString();
                        }
   
                        finally
                        {
                              myConnection.Close();
                        }
                  }
   
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17050405
You aren't returning anything.

Bob
0
 

Author Comment

by:SirReadAlot
ID: 17050494
yes I am, the complete code does return some dat.

I was just wondering if it was possible as i on;y know how to return things from a database.

I took a guess thats why I pasted that code b/c i fink it has to be written in there.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 96

Expert Comment

by:Bob Learned
ID: 17050640
1) .NET framework version?  1.1 or 2.0?

2) What are you really wanting to know, because I don't understand your requirement?

Bob
0
 

Author Comment

by:SirReadAlot
ID: 17050750
ver 1.1

1) I have a populated dropdownlist enclosed in my datagrid.
2) The vaules are yes and no
3) users would Like to have "either".
4)"either" is not part of the database item
5) How do I add it to the dropdown, without touching the database

thanks
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17050877
If the values are "Yes" and "No", then why don't you just use a CheckBox?

Bob
0
 

Author Comment

by:SirReadAlot
ID: 17050903
user requirement, they want dropdown.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17051307
Fair enough ;)

Add an event handler for the DataGrid.ItemCreated event:

    private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
      if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
      {
        DropDownList ddl = (DropDownList) e.Item.FindControl("DeductDropDownList");
        if (ddl != null)
        {
          ddl.Items.Add("Yes");
          ddl.Items.Add("No");
          ddl.Items.Add("Either");
        }
      }
    }

Bob
0
 

Author Comment

by:SirReadAlot
ID: 17051585
WILL TRY THIS
0
 

Author Comment

by:SirReadAlot
ID: 17051635
hi I already have this
   public void dgCompensation_ItemDataBound(Object Sender, DataGridItemEventArgs e)
            {
   
                  if (e.Item.ItemType == ListItemType.EditItem)
                  {
                      DataRowView objDataRowView = (DataRowView)e.Item.DataItem;
                           string currentDeduction = (string)objDataRowView[2].ToString();
   
                        DropDownList ctlDropDownList = (DropDownList)e.Item.FindControl("DeductDropDownList");
   
                        ctlDropDownList.SelectedIndex = ctlDropDownList.Items.IndexOf(ctlDropDownList.Items.FindByText(currentDeduction));
                  }
   
            }

How would i incorporate you code with mine
0
 

Author Comment

by:SirReadAlot
ID: 17051698
done it

 public void dgCompensation_ItemDataBound(Object Sender, DataGridItemEventArgs e)
            {
   
                  if (e.Item.ItemType == ListItemType.EditItem)
                  {
                      DataRowView objDataRowView = (DataRowView)e.Item.DataItem;
                           string currentDeduction = (string)objDataRowView[2].ToString();
   
                        DropDownList ctlDropDownList = (DropDownList)e.Item.FindControl("DeductDropDownList");
                        //
                        
                        if (ctlDropDownList != null)
                        {
                              ctlDropDownList.Items.Add("Yes");
                        ctlDropDownList.Items.Add("No");
                        ctlDropDownList.Items.Add("Either");
                        }
                        //
       
                        ctlDropDownList.SelectedIndex = ctlDropDownList.Items.IndexOf(ctlDropDownList.Items.FindByText(currentDeduction));

                  }
   
            }
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c#, datatable, aspx 4 50
Read top line from CSV file 14 58
What!!!??? 5 60
How do you think this website does searches? 5 34
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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