?
Solved

Adding an item to an embedded dropdownlist CONTROL

Posted on 2006-07-06
11
Medium Priority
?
262 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
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!

 
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 2000 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

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.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

800 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