Solved

Binding a DropDownList and adding a value to first element

Posted on 2003-10-28
7
798 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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