Solved

Binding a DropDownList and adding a value to first element

Posted on 2003-10-28
7
762 Views
Last Modified: 2011-04-14
I would like to retrieve a DataSet from the database (already done) and then add one row (name/value pair) to the start of it (index=0).

Steps:
1. Retrieve a DataSet from the database - all cool, have code for this!
The DataSet will contain name/value pairs used in a DropDown List
2. Insert a name/value pair at begging of DataSet.

The reason for this is that I want to bind data to the list control and then include a "None" option at the beginning of the list for when clients do not need to choose anything from the list.

Cheers.
0
Comment
Question by:smacca
  • 3
7 Comments
 
LVL 2

Expert Comment

by:sedmans
ID: 9633876
You can call the .DefaultView.AddNew() method on the relevant DataTable and then, if necessary, set the relevant fields.
0
 

Author Comment

by:smacca
ID: 9641576
I dont understand!! Why when I add another row, cant I access it - I am still investigating but here is the code I am using:

// Create Instance of Connection and Command Object
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlDataAdapter command = new SqlDataAdapter("spGetScheduleList", conn);

// Mark the Command as a stored procedure
command.SelectCommand.CommandType = CommandType.StoredProcedure;

// Create and Fill the DataSet
DataSet myDataSet = new DataSet();                  
command.Fill(myDataSet);
command.Dispose();
conn.Close();

// Create void option with specified details
if (includeVoidOption)
{
      myDataSet.Tables[0].DefaultView.AddNew();
      int lastRow = myDataSet.Tables[0].Rows.Count;
      myDataSet.Tables[0].Rows[lastRow][0] = voidOptionValue; //if I use lastRow-1 it works but changes the second last row!!!!!!
      myDataSet.Tables[0].Rows[lastRow][1] = voidOptionText;//if I use lastRow-1 it works but changes the second last row!!!!!!
}
0
 

Author Comment

by:smacca
ID: 9641578
Also, I would like to add the "None" option to the START OF THE DATASET!
0
 

Accepted Solution

by:
smacca earned 0 total points
ID: 9641677
SOLUTION:

DataRow newRow = myDataSet.Tables[0].NewRow();
newRow[0] = voidOptionValue;
newRow[1] = voidOptionText;
myDataSet.Tables[0].Rows.InsertAt( newRow , 0);
0
 

Expert Comment

by:saivegesna
ID: 10094657
can you give the same solution in VB.net. i will be appricited
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Performance  Html.BeginForm vs jQuery 3 45
New to SOAP and need some direction 10 40
Runtime Error 2 27
Help with C#, MVC, razor. 6 18
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
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…

932 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

11 Experts available now in Live!

Get 1:1 Help Now