Solved

Binding a DropDownList and adding a value to first element

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Offline Apllication 5 72
rest webservice call over https via c# 6 42
HttpPostedFile 1 26
Call windows 10 virtual keyboard from windows forms app 2 36
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…

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