ObjectDataSource from a DataTable

I recently ran into a situation where I needed an ObjectDataSource in order to use the sort and filter capabilities of the SharePoint SPGridView control, however, my data was already in a DataTable.

I found out that I could use the ObjectCreating event of the ObjectDataSource in order to set the control to an existing data object, such as a DataTable.    I was pretty excited to see that I would be able to take the existing DataTable and expose it as an ObjectDataSource.    That excitement was short lived as I continued to have problems getting it to work with the SPDataView control.  ?

The issue is that the ObjectDataSource was configured to call the Select method of the DataTable whenever the SPGridView control requested data.   This caused the ObjectDataSource to return a DataRow array which the SPGridView did not understand.   I needed the ObjectDataSource to return the data as a complete DataTable and not DataRow array.

The solution that I arrived at is to create a very simple wrapper class around my DataTable object that returned the original DataTable when a method is called on the wrapper class.   Once implemented I had a perfect rendering SPGridView with both filtering and sorting enabled. ?

Below is an example of the wrapper class:

public class DataTableWrapper
{
private DataTable _dt = new DataTable();

public DataTableWrapper(DataTable dt)
{
_dt = dt;
}

public DataTable GetTable()
{
return _dt;
}
}

Here is an example of connecting this up to the ObjectDataSource:

ObjectDataSource ds = new ObjectDataSource();
ds.ID = “myDataSource”;
ds.TypeName=”[…strong name of your DataTableWrapper class here]”;
ds.SelectMethod=”GetTable”;
ds.ObjectCreating += new ObjectDataSourceObjectEventHandler(ds_ObjectCreating);

And here is the ObjectDataSource ObjectCreating event handler:

void ds_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
myDataTable  = new DataTableWrapper(sourceDataTable);
e.ObjectInstance = myDataTable;
}

Things to note about the above samples:

  • The original data source sourceDataTable was defined with class scope.  This allows the ds_ObjectCreating method to access it.
  • myDataTable was defined with class scope.
  • The TypeName of the ObjectDataSource must be the full strong type name for the DataTableWrapper object.

Maybe there is a simpler way of doing this.   I would love feedback if you have any suggestions.

0 thoughts on “ObjectDataSource from a DataTable”

  1. Nice!… but What I don´t understand is what means "with class scope".. remember you are in a page an you need to persist de objects. If you create a class for sourceDataTable and MyDataTable how do you persist them? I recieved this error when I declared these two objects inside de usercontrols like public properties with viewstate objects. The error is this: Object reference not set to an instance of an object.  I know.. I Miss something but I really don´t know.
     
    best regards,
     
    Manolo Herrera
  2. http://www.akkuliste.com/acer/as07b71.htm acer as07b71 akku
    http://www.akkuliste.com/hp/nx6120.htm hp nx6120 akku
    http://www.akkuliste.com/hp/nc6100.htm hp nc6100 akku
    http://www.akkuliste.com/hp/nc6120.htm hp nc6120 akku
    http://www.akkuliste.com/dell/inspiron-1100-series.htm dell inspiron 1100 series akku
    http://www.akkuliste.com/dell/inspiron-1150.htm dell inspiron 1150 akku
    http://www.akkuliste.com/dell/inspiron-5150.htm dell inspiron 5150 akku
    http://www.akkuliste.com/dell/inspiron-5160.htm dell inspiron 5160 akku
    http://www.akkuliste.com/dell/inspiron-5100.htm dell inspiron 5100 akku
    http://www.akkuliste.com/dell/latitude-131l.htm dell latitude 131l akku
    http://www.akkuliste.com/dell/vostro-1000.htm dell vostro 1000 akku
    http://www.akkuliste.com/dell/gd761.htm dell gd761 akku
    http://www.akkuliste.com/dell/nt379.htm dell nt379 akku
    http://www.akkuliste.com/dell/latitude-d531.htm dell latitude d531 akku
    http://www.akkuliste.com/dell/precision-m65.htm dell precision m65 akku
    http://www.akkuliste.com/dell/vostro-1700.htm dell vostro 1700 akku
    http://www.akkuliste.com/gateway/8msb.htm gateway 8msb akku
    http://www.akkuliste.com/hp/pb992a.htm hp pb992a akku
    http://www.akkuliste.com/hp/dv2100.htm hp dv2100 akku
    http://www.akkuliste.com/hp/dv2200.htm hp dv2200 akku
    http://www.akkuliste.com/hp/hstnn-c29c.htm hp hstnn-c29c akku
    http://www.akkuliste.com/hp/nc8000.htm hp nc8000 akku
    http://www.akkuliste.com/dell/inspiron-b120.htm dell inspiron b120 akku
    http://www.akkuliste.com/dell/inspiron-b130.htm dell inspiron b130 akku
    http://www.akkuliste.com/gateway/12msbg.htm gateway 12msbg akku
    http://www.akkuliste.com/hp/7400.htm hp 7400 akku
    http://www.akkuliste.com/dell/inspiron-b120.htm dell inspiron b120 akku
    http://www.akkuliste.com/toshiba/pa3356u.htm toshiba pa3356u akku
    http://www.akkuliste.com/hp/2510p.htm hp 2510p akku
    http://www.akkuliste.com/hp/f3172a.htm hp f3172a akku
    http://www.akkuliste.com/dell/inspiron-2200.htm dell inspiron 2200 akku
    http://www.akkuliste.com/gateway/squ-414.htm gateway squ-414 akku
    http://www.akkuliste.com/hp/hstnn-lb33.htm hp hstnn-lb33 akku
    http://www.akkuliste.com/mitac/bp-8089.htm mitac bp-8089 akku
    http://www.akkuliste.com/toshiba/satellite-m55.htm toshiba satellite m55 akku
    http://www.akkuliste.com/toshiba/satellite-m115.htm toshiba satellite m115 akku
    http://www.akkuliste.com/toshiba/qosmio-f25.htm toshiba qosmio f25 akku
    http://www.akkuliste.com/toshiba/satellite-m60.htm toshiba satellite m60 akku
    http://www.akkuliste.com/toshiba/satellite-p200.htm toshiba satellite p200 akku
    http://www.akkuliste.com/toshiba/pa3536u-1brs.htm toshiba pa3536u-1brs akku
    http://www.akkuliste.com/hp/hstnn-db17.htm hp hstnn-db17 akku
    http://www.batteryfast.com/mitac/8375.htm mitac 8375 battery

  3. http://www.batteryfast.co.uk/acer/aspire-5520.htm acer aspire 5520 battery
    http://www.batteryfast.co.uk/acer/aspire-5920.htm acer aspire 5920 battery
    http://www.batteryfast.co.uk/acer/as07b31.htm acer as07b31 battery
    http://www.batteryfast.co.uk/acer/as07b32.htm acer as07b32 battery
    http://www.batteryfast.co.uk/acer/as07b41.htm acer as07b41 battery
    http://www.batteryfast.co.uk/acer/as07b42.htm acer as07b42 battery
    http://www.batteryfast.co.uk/acer/as07b51.htm acer as07b51 battery
    http://www.batteryfast.co.uk/acer/as07b52.htm acer as07b52 battery
    http://www.batteryfast.co.uk/acer/as07b71.htm acer as07b71 battery
    http://www.batteryfast.co.uk/acer/as07b72.htm acer as07b72 battery
    http://www.batteryfast.co.uk/acer/batbl50l6.htm acer batbl50l6 battery
    http://www.batteryfast.co.uk/acer/travelmate-4200.htm acer travelmate 4200 battery
    http://www.batteryfast.co.uk/hp/hstnn-db42.htm hp hstnn-db42 battery
    http://www.batteryfast.co.uk/hp/dv9000.htm hp dv9000 battery
    http://www.batteryfast.co.uk/hp/dv9500.htm hp dv9500 battery
    http://www.batteryfast.co.uk/hp/dv9600.htm hp dv9600 battery
    http://www.batteryfast.co.uk/hp/hstnn-lb33.htm hp hstnn-lb33 battery
    http://www.batteryfast.co.uk/hp/hstnn-fb44.htm hp hstnn-fb44 battery
    http://www.batteryfast.co.uk/sony/vgp-bps2a.htm sony vgp-bps2a battery
    http://www.batteryfast.co.uk/sony/vgp-bps2b.htm sony vgp-bps2b battery
    http://www.batteryfast.co.uk/sony/vgp-bps2.htm sony vgp-bps2 battery
    http://www.batteryfast.com/toshiba/pa3356u-1bas.htm toshiba pa3356u-1bas battery
    http://www.batteryfast.com/toshiba/pa3356u-1brs.htm toshiba pa3356u-1brs battery
    http://www.batteryfast.co.uk/sony/vgp-bps2c.htm sony vgp-bps2c battery
    http://www.batteryfast.co.uk/toshiba/pa3399u-1brs.htm toshiba pa3399u-1brs battery
    http://www.batteryfast.co.uk/apple/a1185-white.htm apple a1185 white battery
    http://www.batteryfast.co.uk/apple/a1185-black.htm apple a1185 black battery
    http://www.batteryfast.co.uk/hp/510.htm hp 510 battery
    http://www.batteryfast.co.uk/hp/530.htm hp 530 battery
    http://www.batteryfast.co.uk/compaq/nx9000.htm compaq nx9000 battery
    http://www.batteryfast.co.uk/compaq/nx9005.htm compaq nx9005 battery
    http://www.batteryfast.co.uk/compaq/nx9010.htm compaq nx9010 battery
    http://www.batteryfast.co.uk/compaq/nx9020.htm compaq nx9020 battery
    http://www.batteryfast.co.uk/compaq/nx1050.htm compaq nx1050 battery
    http://www.batteryfast.co.uk/dell/original-inspiron-1525.htm dell original inspiron 1525 battery
    http://www.batteryfast.co.uk/dell/original-inspiron-1526.htm dell original inspiron 1526 battery
    http://www.batteryfast.co.uk/dell/inspiron-1100-series.htm dell inspiron 1100 series battery
    http://www.batteryfast.co.uk/dell/inspiron-1150.htm dell inspiron 1150 battery
    http://www.batteryfast.co.uk/dell/inspiron-5150.htm dell inspiron 5150 battery
    http://www.batteryfast.co.uk/dell/inspiron-5160.htm dell inspiron 5160 battery
    http://www.batteryfast.co.uk/dell/inspiron-5100.htm dell inspiron 5100 battery
    http://www.batteryfast.co.uk/dell/inspiron-6400.htm dell inspiron 6400 battery
    http://www.batteryfast.co.uk/dell/latitude-131l.htm dell latitude 131l battery
    http://www.batteryfast.co.uk/dell/vostro-1000.htm dell vostro 1000 battery
    http://www.batteryfast.co.uk/dell/1501.htm dell 1501 battery

    http://www.batteryfast.co.uk/dell/e1505.htm dell e1505 battery

  4. http://www.360batteries.com/new-dell-latitude-d510-laptop-cpu-cooling-fan-n8715.html cpu fan

    http://www.360batteries.com/new-dell-latitude-d510-laptop-cpu-cooling-fan-n8715.html cpu cooling

    fans

    http://www.360batteries.com/new-dell-latitude-d510-laptop-cpu-cooling-fan-n8715.html dell cpu fan

    N8715

    http://www.360batteries.com/new-dell-latitude-d510-laptop-cpu-cooling-fan-n8715.html cpu fan cooler

    http://www.360batteries.com/new-dell-latitude-d510-laptop-cpu-cooling-fan-n8715.html cpu cooler

    http://www.360batteries.com/new-dell-latitude-d510-laptop-cpu-cooling-fan-n8715.html cpu fans

    http://www.360batteries.com/oem-usb-cable-for-sony-dsc-w50-w55-w70-w80-w90-w100-w110-w120-w130-

    w150-w170-w200-w300.html camera cable

    http://www.360batteries.com/oem-usb-cable-for-sony-dsc-w50-w55-w70-w80-w90-w100-w110-w120-w130-

    w150-w170-w200-w300.html digital camera usb cable

    http://www.360batteries.com/oem-usb-cable-for-sony-dsc-w50-w55-w70-w80-w90-w100-w110-w120-w130-

    w150-w170-w200-w300.html camera cables

    http://www.360batteries.com/oem-usb-cable-for-sony-dsc-w50-w55-w70-w80-w90-w100-w110-w120-w130-

    w150-w170-w200-w300.html usb cable camera

    http://www.360batteries.com/oem-usb-cable-for-sony-dsc-w50-w55-w70-w80-w90-w100-w110-w120-w130-

    w150-w170-w200-w300.html Sony digital camera usb cable

    http://www.360batteries.com/oem-usb-cable-for-sony-dsc-w50-w55-w70-w80-w90-w100-w110-w120-w130-

    w150-w170-w200-w300.html cable digtal camera

    http://www.360batteries.com/vga-svga-male-to-dvi-d-male-m-m-1-5m-5-feet-video-cable.html
    male to male vga

    http://www.360batteries.com/vga-svga-male-to-dvi-d-male-m-m-1-5m-5-feet-video-cable.html
    dvi vga cable

    http://www.360batteries.com/vga-svga-male-to-dvi-d-male-m-m-1-5m-5-feet-video-cable.html
    vga to component cable

    http://www.360batteries.com/vga-svga-male-to-dvi-d-male-m-m-1-5m-5-feet-video-cable.html
    vga to hdmi converter

    http://www.360batteries.com/vga-svga-male-to-dvi-d-male-m-m-1-5m-5-feet-video-cable.html
    vga male to male cable

    http://www.360batteries.com/vga-svga-male-to-dvi-d-male-m-m-1-5m-5-feet-video-cable.html
    mini dvi to vga adapter

    http://www.360batteries.com/hp-compaq-ac-adapter-presario-cq20-cq40-cq45-cq50-cq60-cq70.html

    hp ac adapter cq20

    http://www.360batteries.com/hp-compaq-ac-adapter-presario-cq20-cq40-cq45-cq50-cq60-cq70.html

    ac adapter hp cq45 cq50

    http://www.360batteries.com/hp-compaq-ac-adapter-presario-cq20-cq40-cq45-cq50-cq60-cq70.html

    cq70 cq60 ac adapter

    http://www.360batteries.com/hp-compaq-ac-adapter-presario-cq20-cq40-cq45-cq50-cq60-cq70.html

    cq20 cq40 hp ac adapter

    http://www.360batteries.com/hp-compaq-ac-adapter-presario-cq20-cq40-cq45-cq50-cq60-cq70.html

    ac adapter persario hp

    http://www.360batteries.com/hp-compaq-ac-adapter-presario-cq20-cq40-cq45-cq50-cq60-cq70.html

    cq70 persario ac adapter

    http://www.360batteries.com/white-battery-cover-for-blackberry-bold-9000.html battery blackberry

    cover

    http://www.360batteries.com/white-battery-cover-for-blackberry-bold-9000.html blackberry bold 9000

    cover

    http://www.360batteries.com/white-battery-cover-for-blackberry-bold-9000.html cover battery

    http://www.360batteries.com/white-battery-cover-for-blackberry-bold-9000.html blackberry battery

    cover

    http://www.360batteries.com/white-battery-cover-for-blackberry-bold-9000.html bold 9000 cover

    battery

    http://www.360batteries.com/white-battery-cover-for-blackberry-bold-9000.html battery cover

  5. http://www.bestlaptopbattery.com.au/laptop-ac-adapter/dell/dell-19.5V-7.7A-150w-7.4mm-5.0mm-with-pin-pa-15.php Dell PA-1151-06D 19.5V 7.7A 150W 7.4MM*5.0MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/dell/dell-19V-3.16A-60w-5.5mm-2.5mm.php Dell PA-1600-06D1 PA-1600-06D2 19V 3.16A 60W 5.5MM*2.5MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/dell/dell-20V-3.5A-70w-horseshoe-style-special-for-dell-pa-6.php Dell ADP-70E ADP-70EB 20V 3.5A 70W HORSE SHOE STYlE SPECIAl PA-6 adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/delta/delta-19V-3.42A-65w-5.5mm-2.5mm.php Delta ADP-65DB 19V 3.42A 65W 5.5MM*2.5MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/delta/delta-19V-4.74A-90w-5.5mm-2.5mm.php Delta ADP-90FB 19V 4.74A 90W 5.5MM*2.5MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/fujitsu/fujitsu-19V-3.16A-60w-5.5mm-2.5mm.php Fujitsu A4190 19V 3.16A 60W 5.5MM*2.5MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/hp/hp-19V-4.74A-90w-5.5mm-2.5mm.php Hp PPP009X PW-AC003 19V 4.74A 90W 5.5MM*2.5MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/hp/hp-18.5V-3.5A-65w-4.8mm-1.7mm.php Hp DC359A Dl606A 18.5V 3.5A 65W 4.8MM*1.7MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/hp/hp-19V-4.74A-90w-4.8mm-1.7mm.php Hp 19V 4.74A 90W 4.8MM*1.7MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/hp/hp-18.5V-4.9A-90w-5.5mm-2.5mm.php Hp 18.5V 4.9A 90W 5.5MM*2.5MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/hp/hp-18.5V-6.5A-120w-5.5mm-2.5mm.php Hp PA-1121-02H PA-1121-12H 18.5V 6.5A 120W 5.5MM*2.5MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/hp/hp-18.5V-6.5A-120w-flat-dc.php Hp 18.5V 6.5A 120W FlAT DC adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/ibm/ibm-16V-3.5A-56w-5.5mm-2.5mm.php Ibm 02K6553 02K655 16V 3.5A 56W 5.5MM*2.5MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/ibm/ibm-20V-3.25A-65w-7.9mm-5.5mm-DC-with-pin-inside.php Ibm 92P1153 92P1154 20V 3.25A 65W 7.9MM*5.5MM DC WITH PIN INSIDE adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/liteon/liteon-19V-3.42A-65w-5.5mm-2.5mm-white.php Liteon 19V 3.42A 65W 5.5MM*2.5MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/liteon/liteon-19V-3.16A-60w-5.5mm-2.5mm.php Liteon 19V 3.16A 60W 5.5MM*2.5MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/liteon/liteon-19V-3.95A-75w-5.5mm-2.5mm.php Liteon PA-1750-01 19V 3.95A 75W 5.5MM*2.5MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/liteon/liteon-19V-4.74A-90w-5.5mm-2.5mm.php Liteon PA-1900-15 19V 4.74A 90
    W 5.5MM*2.5MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/liteon/liteon-19V-6.3A-120w-5.5mm-2.5mm.php Liteon PA-1121-08 19V 6.3A 120W 5.5MM*2.5MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/liteon/liteon-19V-6.3A-120w-4pin-round.php Liteon 19V 6.3A 120W 4 PIN ROUND adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/liteon/liteon-20V-6A-120w-5.5mm-2.5mm.php Liteon 20V 6A 120W 5.5MM*2.5MM adapter
    http://www.bestlaptopbattery.com.au/laptop-ac-adapter/liteon/liteon-19V-4.74A-90w-5.5mm-1.7mm.php Liteon PA-1900-04 19V 4.74A 90W 5.5MM*1.7MM adapter

Leave a Reply