简体   繁体   中英

How to add values programmatically to a datalist/datasource in ASP.NET?

I am in need of a method of adding values to a DataList that are not coming from the SQLDataSource that the DataList is connected to. These values are not necessarily found in my database but nonetheless need to show up in the DataList I already have.

On my page I have this structure:

DataListA
    Hidden Field
    Label
    DataListB
    SQLDataSourceB

SQLDataSourceA

I am looking for methods to programmatically add entries (custom ones at that) to SQLDataSourceB such that DataListB includes these extra rows I need displaying in DataListB. I've looked into the Pre_Render events and examples such as found here but I don't need to change entries, nor alter their style. I am looking for adding custom entries, is there a way to do this?


Edit: The results that DataListB is displaying is an "Department", "Code" and "Name". These are displaying school courses. Ie, a typical result would look like: ART 3000 Photography . Courses that I am looking to add to DataListB are custom courses that are in the same format as the above example. The reason behind this - if I display ART 3000 Photography in DataListB, I want to display SCI 1000 Biology rather than if I didn't display ART 3000 Photography , I would want SCI 1100 Chemistry . It is a multitude of cases such as this that I need to be able to check what courses I am displaying (before page load) and alter which custom classes I need to add to DataListB.

@ethorn10 I have two primary needs of adding these custom courses to my DataList. The first case is for a range of classes. For every class displayed in the table within this range alters the x number of classes more that I need from this pool. For example let's say I have a range of classes with a "Code" from 1000 to 3000. The limit of classes within this range that should (at minimum) display in the table is 6. Thus, (and I realize a label of some sort, rather than an entry in the datalist is necessary for this) I want the user to see "6 more classes between 1000 to 3000". For every course in this range that is displayed, I want the "6" to decrement, thus if one class is taken in the range, the display/label should read "5 more classes between 1000 to 3000".

The second case is similar to the first, but instead of being inclusive from 1000 to 3000, there are is a pool of distinct classes. Thus it changes to "You need 2 more classes from these classes: 1200, 1350, 2300, etc..."

You can use DataView to get the SqlDataSource data and then convert to DataView to Datatable . After that you can add custom data as Rows to the DataTable .

DataView dv =(DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
DataTable dt = dv.ToTable();
dt.Rows.Add(1, "item1", "item2", 3, "item4"); //the number of param should match the number of column you query returns
GridView1.DataSource = dt; //binding the datatable to a gridview after adding customer record
GridView1.DataBind();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM