Solved

Adding an item to an embedded dropdownlist CONTROL

Posted on 2006-07-06
11
244 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
  • 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
Media.Imaging 1 23
Getting error in connectionstring with Excel. 30 45
c# - Best approach for objects in functions 3 24
Change to file doesn't show up 16 51
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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!
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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