Solved

Binding a DropDownList and adding a value to first element

Posted on 2003-10-28
7
750 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
Comment Utility
You can call the .DefaultView.AddNew() method on the relevant DataTable and then, if necessary, set the relevant fields.
0
 

Author Comment

by:smacca
Comment Utility
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
Comment Utility
Also, I would like to add the "None" option to the START OF THE DATASET!
0
 

Accepted Solution

by:
smacca earned 0 total points
Comment Utility
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
Comment Utility
can you give the same solution in VB.net. i will be appricited
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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

17 Experts available now in Live!

Get 1:1 Help Now