Adding dummy item to DropDownList in a DataGrid

G

Guest

Hi folks,

I have a DropDownList in a DataGrid that is populated from records in a
database.

I want to add a value that might be a string such as "Select a Company" for
the first item since an OnSelectedIndex event is not fired if you select the
first item.

Does anyone know of an easy way to do this?

I am using the following code to get at the the dropdownlist that is inside
a cell within the datagrid which is placed inside my PageLoad function:

Dim list As DropDownList = New DropDownList ( )
Dim cell As TableCell = CType(list.Parent, TableCell)
Dim item As DataGridItem = CType(cell.Parent, DataGridItem)

Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"), DropDownList)

ddlCo .Items.Add("Select a Company")

I am getting the infamous "object reference not set to an instance of an
object" on the Dim item line...

Appreciate any replies...
glenn
 
G

Guest

You code makes no sense for example

Dim list As DropDownList = New DropDownList ( )

declares and initiates a DropdownList control called list, then

Dim cell As TableCell = CType(list.Parent, TableCell)

declares a TableCell object and sets it to the value the the parent control
of the cell control cast as a TableCell object. But at this point the list
object, has no parent control.

What are you actually trying to do?
 
G

Guest

I am looking to add a dummy value to my ddl so that when I select the first
real value in the ddl, an OnSelectedIndex event will be fired. The value
might be for example, "Select a Company".

The code I presented I believe needs to be provided in order to find a
control that is inside a DataGrid. If the ddl was not in a DataGrid I could
simply write something like:
Dim ddl as DropDownList = e.Item.Cells(2).FindControl("ddlCompany")
but since it is inside the DataGrid, I needed the additonal code.

HTH. If you know of an easier way, please let me know.
Thanks,
glenn
 
J

Jeff Dillon

Just add it to your SQL

SELECT "add a company"
UNION
SELECT CompanyName from Companies
 
G

Guest

This line


Dim list As DropDownList = New DropDownList ( )

creates a new DdropDownList control called list, it is not in a DataGrid, it
has no parent or child control, it is not on the page at all, it is
esentially just an object. Therefore list.parent makes no sense unless there
is a line such as

somecontrol.Controls.Add(list);

Which will place the ddl inside some other control.


If you wanht to add a dummy item to a DropDownList, that is declared on your
page you can do it as below.

<asp:DropDownList runat="server" ID="MyDDL" AppendDataBoundItems="true">
<asp:ListItem Selected="True" Text="DummyItem"></asp:ListItem>
</asp:DropDownList>

Because AppendDataBoundItems is set to true then the items from the database
will be added after the dummy item.
 
G

Guest

When used without a datasource in a ddl, your example works.

However, when used with a datasource in a ddl, your example does not add the
dummy item.

My code is as follows:

<asp:DropDownList id="ddlTo"
AppendDataBoundItems="true" runat="server" DataValueField="cname"
AutoPostBack="True" DataSource='<%# GetCompanyNamesForThisProject() %>'
OnSelectedIndexChanged="ddlTo_SelectedIndexChanged">
<asp:ListItem Selected="True"
Text="DummyItem"></asp:ListItem>
</asp:DropDownList>

Thanks again for any tips.
Glenn
 
G

Guest

The UNION statement only works as shown:

SELECT statement
UNION
SELECT statement

where a statement would be SELECT * FROM TABLE
 
J

Jeff Dillon

You're doing it the hard way. Try my suggestion

glenn said:
When used without a datasource in a ddl, your example works.

However, when used with a datasource in a ddl, your example does not add
the
dummy item.

My code is as follows:

<asp:DropDownList id="ddlTo"
AppendDataBoundItems="true" runat="server" DataValueField="cname"
AutoPostBack="True" DataSource='<%# GetCompanyNamesForThisProject() %>'
OnSelectedIndexChanged="ddlTo_SelectedIndexChanged">
<asp:ListItem Selected="True"
Text="DummyItem"></asp:ListItem>
</asp:DropDownList>

Thanks again for any tips.
Glenn
 
J

Jeff Dillon

WRONG

SELECT 'Select an author'
UNION ALL
SELECT au_lname FROM Authors

works fine
 
G

Guest

SELECT 'Select an author' is not a valid SQL statement...

I want to add a selection to the top of my ddl. That is the issue.
 
J

Jeff Dillon

YES IT IS!! Did you try it? What do you mean "not valid"? Of course it is.

I ran this in Query Analyzer, and it works just fine. We use it in our DDL
scripts all the time.

Please try my suggestions first next time. We do this all the time at this
company to populate the first item in our lists..
 
J

Jeff Dillon

Paste the following code in query analyzer, and use the Pubs database:

SELECT 'Select an author'
UNION ALL
SELECT au_lname FROM Authors

You can also do the same thing in Access..I just tested it
 
G

Guest

Jeff,

It works in SQL Server but not in Access. Currently we are working with
Access. I get a message box in Access upon running a SQL Query from the
Northwind.mdb file that states "Query Input must contain at least one table
or query".

I used your SQL syntax and changed it for the northwind.mdb file

SELECT 'Select a Title'
UNION ALL
SELECT TITLE FROM Employees

Am I missing something. I am unsure how you got it to work with Access.
Pubs is a SQL Server only db. Please do not use all caps in your response.
It sounds like yelling and is not warranted.

Thanks for your help so far. I truly appreciate it. I will investigate the
UNION ALL in Access unless you have anything to add.

glenn
 
G

Guest

Jeff,
I used the following SQL syntax and it works now in Access

SELECT DISTINCT 'Select a Title' FROM Employees
UNION ALL SELECT TITLE FROM Employees;

Thanks again Jeff. You put me on the right track. Doing this in the
ASP.NET code would have been a real bear.

glenn
 
J

Jeff Dillon

I was yelling. Got a problem with that?

SELECT DISTINCT 'Select a Title' FROM Employees;
UNION ALL
SELECT TITLE FROM Employees;
 
S

Scott R

Here is my solution. Just modifiy the DataBound event of the
DropDownList. This example add an "All" default to the Candidate
DropDownList :

protected void DropDownListCandidate_DataBound(object sender,
EventArgs e)
{
DropDownListCandidate.Items.Insert(0, new
ListItem("All").ToString());
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top