Solved

Adding an item to an embedded dropdownlist CONTROL

Posted on 2006-07-06
11
258 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
Industry Leaders: 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 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 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