Solved

Binding a DropDownList and adding a value to first element

Posted on 2003-10-28
7
784 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

821 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