Solved

Adding an item to an embedded dropdownlist CONTROL

Posted on 2006-07-06
11
221 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# single quoting a dynamical sql statement. 4 35
Trouble with References... 5 25
Data Saving 5 40
Data Saving(2) 2 28
Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now