How to get Rows and Columns collection in a data bound server control, independent of DataSource attached.



In my custom data bound server control (Inherited from existing server control). I need to get the column collection of the data source.

I had tried to get the collection by



            This returns DataSourceView, which I can not enumerate to get rows and columns collection. I want to use it as DataTable/ DataView.




            This return IdataSource. I am not getting how to get DataView/ DataTable out of this.


Can any body help to find me the rows and columns collection in a custom data bound server control, irrespective of whatever data source has been used for binding.




6/17/2005 1:18:04 PM 4216 articles. 0 followers. Follow

7 Replies

Similar Articles

[PageSpeed] 14

One thing to keep in mind with the data source model is that there is no guarantee what the exact data type is that is returned. The only guarantee is that the data returned by a data source implements the IEnumerable interface, and that's it. In many cases the data really is a DataView, but certainly not always. For example it might be a generic list of Customer objects, or perhaps it's an Array of Book objects.

If you want to get the data from the data source, you should call GetData(), and that gives you back the DataSourceView. Then you have to call its Select() method and pass in a callback that will be called when the data is available - it will be given to you as an IEnumerable. To get the "rows" you simply iterate over the data:
IEnumerable data = ...
foreach (object o in data) {
   // Do something with "o"

How to get the "columns" is entirely up to you. Typically the way data bound controls get column data by looking at the first row, and calling:
PropertyDescriptorCollection fields = TypeDescriptor.GetProperties(firstDataRow);

7/7/2005 4:56:50 AM

Im in exactly the same position now.

Isn't having a call back a ridiculous way to work? If you're in your custom control and you're about to create the control hierarchy and you need the data from one of the DataSourceControls why can't you just get it?

I can understand a few situations where a call back would be useful but 99% of the time you need your data before proceeding.

Am i totally misunderstanding this situation?

I've been looking through the GridView and DetailsView controls and in their CreateControlHierarchies they are able to call .ExecuteSelect on their DataSourceViews which obviously i can't because it's a protected method. And im not sure how they are able to call it either?

Any additional information would be super useful!
11/10/2005 7:44:22 PM
Hi Paul,
The reason for using callbacks is so that asynchronous data sources can be implemented. The data sources we shipped in the box are not asynchronous, however 3rd parties can implement them. Due to the asynchronisity, the callback might not be called all the way until around the PreRender phase of the page. As such, you have to divide up your child-control-related work into two parts: stuff to prepare the call to Select(); stuff to do once you have the data back from Select().

Regarding some of the controls that directly called ExecuteSelect(), that was simply a bug. That method is actually marked as "protected internal" so that's how GridView was able to call it. Prior to the RTM release of 2.0, GridView was changed to call Select() instead of ExecuteSelect() since that's all that 3rd parties (such as yourself) are able to do.


11/10/2005 9:17:40 PM


thanks for such a fast and informative response. I'll need to research more into asynchronisity, however does this mean there's no official way to do it sychronously?

I note that the DataSourceView has only Select (which uses a callbank) and a protected ExecuteSelect. The SqlDataSourceViews implementation of Select is to simply call ExecuteSelect with no callback - i've got something similar working, is this the closest to an official synchronous way? I assume so...

Of course the next research task is finding out how the GridView can tell which controls are bound to which fields through the Bind syntax. Now there's a subject that needs more documentation for Control builders!


11/10/2005 11:15:59 PM

Yup, there's no official way to use data source synchronously. You'll have to programm against the async pattern, but it really isn't all that hard since it mostly just involves splitting your databinding code into two parts. And if you derive from a base class such as CompositeDataBoundControl, you really don't have to worry about it at all.

I'm not an expert on Bind() myself (I wrote the data source controls in 2.0, among other things, but not data bound controls), however hopefully there will be a bit more documentation out there on how to use it in the coming months.


11/18/2005 5:21:30 AM
Can You pls tell me how to post Q. on this forum
11/18/2005 5:34:22 AM
You justed post one ;)
/Fredrik Normén - fredrikn @ twitter

Microsoft MVP, MCSD, MCAD, MCT

My Blog
11/18/2005 6:18:01 AM

Similar Artilces:

Issue with Template Columns & Bound Columns while updating the data in a GridView control
Hi, I'm facing the below problem (In general, ObjectDataSource is driving me crazy ! ) I have a simple gridview control and an ObjectDataSource is used to populate the grid. I'm trying to update a row and the corresponding tableadapter methods & stored procedure are in place. Each row has 2 checkboxes and 2 textboxes in edit mode. When the change the values in edit mode and hit update, the checkbox values changed are saved into the Database properly, whereas the textbox values are not being saved. If the textbox column is a bound column then the column is updated with <NULL> in...

Getting collection into server control
Hi I need to retrieve some values from a collection like: customer1customer2customer3 etc Using a control like dropdownlist or listbox and selecting a value and making it as disabledI can read it and populate my first datagrid column. Question is if I try to use a label instead of controls declared above it does not workWhat control can I use? I just want to show the word e.g. 'customer1' without borders and arrows around Thanks a lotBest regards,Ivan Andrade If you just want to display a simple list of the items in your collection, then use something like a Repeater wher...

Controls bound to Datasource
I have a problem, and understand why it is happening but not the best way to fix it.    I have SQLDatasource control with a parameter in its WHERE statement (WHERE ID = @ID).  The parameter's source is a session variable.  Control values on the page are bound to this SQLDatasource during the page load event.  I then have a couple of buttons on the page that change the Session("ID") value in thier click event handler.  When I click a button the page reloads, and while reloading the new Session value is assigned at some point...

Get data from bound column?
Hi-- From code, how do I refer to the value that is in the field listed in a BoundColumn's DataField property: 1. From one of the grid's event handlers (e argument), and 2. From any other procedure that does not have the e argument? Thank you!...

Server Controls -- Getting the Data
I have a server control the I made that basically is a Full Name control that contains three text fields: Last First Middle [ ] [ ] [ ] below the control I have a asp:button control. When the button is clicked, I would like to be able to grab the data from the Full Name control -- each field. using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Text; using System.Collections.Specialized; namespace TRNameControl { ...

how to get a control ID from a bind field name / bound field (column) name from a formview's control
I have a form view which is bound to a table, now from codebehind file I need to apply some formatting based on some of the field's bound control. For example, if Formview1 is bound to Employee table, and it's item template has a control with ID "Label1" which is bound to "EmployeeJoinDate" . Now, I have a different table which has a list of field name(EmployeeJoinDate is one of them), all of which should be applied some formatting based on some condition. Now I can take two approach for that, but I am stuck at both:  1. If I can somehow get the contro...

getting bound column data values
hi Im trying to get the primary key from a datagrid to pass as a URL parameter, how can i get this value from the currently selected datagrid row ? Ive put the following code inside a procedure but i get a compliation error string strTemp = MainPageDataGrid.DataKeys(E.Item.ItemIndex).ToString; Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0118: 'System.Web.UI.WebControls.BaseDataList.DataKeys' denotes a...

Data not getting bound to the UI controls
Hi All,   I have a dll which has events.I am trying to assign some values in UI fields from the dll. However I can see those values on while I am debugging but it does'nt get bound to the controls at run time.Can anyone please help me with thsi regard?   Thanks,Anitha    Hi, Some code would be useful right now.   Regards,  Shandy Hidayat Regards,Shandy Hidayat, MCPD private void HandleWorkListWorkItemAdded(WorkListWorkItemAddedEventArgs e) { try {AddLogItem("WorkList WorkItem added event ocurred id= " + e.WorkItem.Id + " ...

getting data into the database through server controls
Hello again, I apologize for this very noob question but you see this is really important for me I'm building a site that gets user data then automatically stores it in DB (sql express). Getting data out to the user seems to be easy e.g gridview, detailsview etc but I have no idea how to get data in to the DB  I'm using  server control textbox ,button, fileuploader and calendar. please help which architecture are  u using Disconnected or Connected architecture?If possible send codesarikasarika This is such a broad subject that you should really do some ...

Getting Rows and Columns from a data set.
Hello Guys,             Actually I am new to ASP .NET 2.0.   i am writing a web service which returns a data set with records from an Access Database table.  as long as I bind this data set with a gridview it shows all records on my consuming application in a gridview.  But I don't want to use gridview i want to get each column's value separate.  is there any way i can get each column value?  Thanks in advance...  try   DataSet1.Tables(i).Rows.(j).Items(k)..i,j,k are indexes Hi De...

Data-bound control with multiple datasources
Hi, I need to create a data-bound control which can have virtually unlimited number of datasources and I'm a little lost about where to start. Do I start with DataBoundControl and try to somehow incorparate multiple datasources into it? Should I create a composite control with colleciton of smaller controls inherited from DataBoundControl? Or just forget about DataBoundControl and start scratch? I thought this should be a pretty common scenario but I wasn't able to find any articles on the web regarding this issue. Thank you for your ideas. Alan. you might be running into pro...

Getting data from data columns
 for now if i want to say retrive a field from a row, i use 1 SQLDataSource, 1 GridViewThen in the code it will be like Dim temp as String = GridView.Rows(0),Cells(5).Text, works but i need quite alot of redundant controls isit? how do i do it with say just 1 SQLDataSource or something else, classic ASP with SQL code, is a bit complex sometimes, although if thats the only way i don't mind, but state how to do it? i get quite blur when it comes to ASP with SQL, they by itself is still ok. Hello my friend, The SQLDataSource is good for simple get and display, but when you need to i...

How do i get Data Type of Each Column in a Sqldatasource control?
Let's say... the sqldatasource has 3 columns: TableKey(int), TableName(string) and StartDate(datetime) and i'm writing a method to return the data type based on the column name.. for example: GetDataType("StartDate") should return "datetime"...what should i do? I think this is what you need: Good luck.Regards,CS4Ever~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Dont forget to click "Mark as Answer" on the post that helped you.This credits that member, earns you a point and ma...

ADO.Net vs Data bound controls.
What are the pros and cons of using vs Data bound controls? I know data bound controls might be resource heavy, but alot of tutorials are pushing bound controls (SQLDataSource control and ObjectDataSource Control)I would love to hear anyones opinions and experiences.ThanksMike. Hi Mike, The reason why many tutorials use Data controls is because many times the articles want save time and focus on other core concepts and Data controls allow us to cut down on the data base related code so that the author can focus on the main topic. Another reason is that Data controls are perfect fo...

Web resources about - How to get Rows and Columns collection in a data bound server control, independent of DataSource attached. -

Independent - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Anderson Independent Mail: Local Anderson, South Carolina News Delivered Throughout the Day.
Current news for Anderson, SC and its surrounding communities brought to you by Anderson Independent Mail. Features local Anderson, South Carolina ...

Home - Newport, AR - Newport Independent - Newport, AR
Get the latest breaking news, sports, entertainment, obituaries - Newport Independent - Newport, AR

Irish Independent - Your News - Your Way - Your Say -
News, video, photos and commentary from your Irish Independent newspaper including Breaking, National, World, Sport and Business updates.

The Independent - News - UK and Worldwide News - Newspaper
The latest in UK News and World News from The Independent. Wide range of international and local news, sports news, commentary and opinion pieces. ...

Keller Independent School District - Wikipedia, the free encyclopedia
The Keller Independent School District is a K-12 public school district based in Keller , Texas ( USA ). It serves more than 31,000 students ...

Independent audit of public service finds APS is burdened by red tape and risk averse culture
An independent audit has found what many in the public service will already know.

Independent Metro Petroleum pressure puts Canberra on discounting cycle
&nbsp;Thrift drivers spread word on low-profile Fyshwick petrol discounter.

Ted Mack working to install an independent in Joe Hockey's old seat
... the defeat of another Liberal Party candidate in North Sydney. The former federal MP, who twice won the blue-ribbon Liberal seat as an independent, ...

Resources last updated: 11/21/2015 2:56:47 AM