Gridview binding without using a Database - asp.net

Can a GridView be bound to a List<>?
I want to create a Master Details scenario. So The first List<> contains Master data and the second Child data. When the user clicks on the Master Data, the child data, should appear in a pop.
Is that possible? Please share some code.

Yes, this is possible. Take a look at the ObjectDataSource to use for databinding a list to a GridView. It requires some more manual coding, but it works.
There's a good discussion of one possible solution here that manually constructs a DataTable from your business object list to make filtering/sorting/paging easier than manually implementing those methods.

You can create on temporary table and then bind that table with grid.
Like,
dataset.DataSource=tempTable;
tempTable.DataBind();

GridView1.DataSource = theList;<br/>
GridView1.DataBind();

Related

How to populate many textboxes from a sql query automatically?

This might be a silly question. But I have a lot of textboxes in an in asp.net form I need to populate with data from a sql query.
If the query column names and textbox names are all the same, is it possible to populate the textboxes from the query data automatically? Maybe using loop? There are too many fields so I think it is stupid to populate each field individually using something like:
textboxX.text = data.getValue(0).ToString();
textboxY.text = data.getValue(1).ToString();
...
I am thinking there must be a simpler way to populate all of the textboxes. Can anyone please help me to do this? A simple example would be great.
If you were using something like MVC or MVVM then it would be in a manner of speaking. You could tie your view page to a model that has properties corresponding to the fields. Then in your view page you would bind the model properties to the fields.
If you don't work with MVC/MVVM/knockout then the best thing you could do is to loop your form's controls and assign them some values from your list. For this purpose it's simplier if your controls are named like Ctrl1, Ctrl2, etc - it's just to simplify the looping/assignment

ASP.NET: GridView and DetailsView, Do I have to have separate connections to same database?

I have a GV and a DV that extract data from the same database. The link between these controls is when a record in the GV is selected, the DV displays more details about that record.
Do I need separate connections? Obviously, I haven't achieved this goal and am working on it. Thanks.
CLARIFICATION so as not to waste your time:
I am asking about connection, not datasource. The reason that I am not sure the same connection can be used is that with GV, the connection doesn't have any parameter. Whereas with the DV, it needs the record ID passed to it. Or am I wrong?
Here's the link to code on the net that makes me wonder:
http://asp.dotnetheaven.com/util/srcview.aspx?path=~/aspnet/samples/data/GridViewMasterDetails.src
I am a novice so am still confused with the terminology. Thanks for being patient.
They can use the same datasource. (Such as an ObjectDataSource or SQLDataSource)
You can use the same data source, i.e. a DataTable. But when the Gridview row is selected you will need to find the index of the selected row and then find the DataRow from the DataTable and rebind your DetailView to that DataRow. Hope this helps.
I think that you are using Visual Studio databinding, and I would say that same Connection(DataSource) object can be used (if that is the way it goes), but I would rather suggest you avoid this design time Visual Studio programming, although it is simple and fast

Trying to populate ASP GridView as it's data source gets populated on the server

Hi I am new to using ASP.NET and GridView and it would be great if someone could kindly help me out here.
My scenario is that on a button click event, my code tries to get data from a webservice in chunks for a long period of time. Now I create a DataTable and populate it as each chunk is retrieved. My question is how do I show in gridview whatever I have retrieved so far before the button click returns ? Say, if I have three chunks being retrieved one after another then as of now the gridview gets populated with all the data from the 3 chunks at once, I want to be able to show the first chunk as it is downloaded, then add the second chunk and finally the third. Not sure how to do this.
I'd be happy if you just give me an overall solution/point me in the asp feature I should be using to get this done. Thanks.
If you are binding using webservice. Then asp:GridView will create unnecessary hassles for you as its designed for simple server side bindings.
Why dont you try DataTables or jqGrid, as they are much better alternatives?
You can Merge the existing DataTable every time you receive a new chunk from the Web service and then bind the updated (Merged) DataTable to the gridview.
Check out this article from MSDN and get a better idea of how you can merge DataTables
DataSet.Merge Method (DataTable)
Also look at this article that may help you in your current problem.
Merging two Datasets into a single Datagrid

Telerik GridViewDataControl - load from datatable

does anyone uses this control and knows how to load data to it from simple datatable. There is not such a trick as DataSource...
Im assuming you just mean a datatable you have created programatically?
It will bind straight up like it would from a DataSource.
Just make sure your DataField name in the columns mark up matches the name of the column and your good to go, just as you would with a different datasource.
Then just do
grvFoo.DataSource = yourProgrammticallyCreatedTable;
grvFoo.DataBind();
But, since your doing this progrmatically I would recommend making a "BindTable" function. Eg a function that builds your datatable and bind. You can then use this for sorting / paging etc. As your doing this all programatically you must handle such events by hand, they are normally done with the SQL / Object Datasource etc.
you mean Telerik Radgrid ? If yes, you can also specify a "NeedDataSource" event in the aspx/ascx
http://www.telerik.com/help/aspnet-ajax/grdadvanceddatabinding.html
On a side note, just to let you know, we are having a lot of performance problems with Telerik. For the most part, you can use the inbuilt asp.net gridview (and do some more styling if needed) than taking a performance hit by using telerik (for their fancy grid)
We must use ItemsSource property like this:
dataGridView1.ItemsSource = LoadDataTable.DefaultView;
LoadDataTable - this is some DataTable, filled from SQL.

how do I create an array or list of ASP.NET checkboxlists

I know how to use the checkboxlist in ASP.NET to display options retrieved from a database. What I don't know how to do is to make this 2-dimensional. That is, I need a list of checkboxlists where I don't know how long the list is; both dimensions of the checkboxlist will be determined by
list of people (pulled from database)
list of tasks (pulled from database)
and the user of the web page will click in column/row to specify which people will be assigned which tasks.
Right now I'm thinking that my only option is to brute-force it by creating a table and populate each cell with its own checkbox. (yuck)
Is there a more elegant way to create a 2-dimensional array of checkboxes with labels for both rows and columns?
I would use a repeater along with a checkboxlist. Depending on how your database is setup you could have each checkboxlist databound.
I've done this before and resorted to the brute-force method you suggest.
It's not as nasty as you'd think. Other solutions that were declarative and databound would likely be just as convoluted and confusing.
I use the ASPxGridView from DevExpress. It has a control column type of Selected (or something like that) which will display a checkbox in the column with the other column populated from your bound datasource. The User can select any rows desired by checking the checkbox on the row and you can get all the selected rows easily inot a collection to process. DevExpress components really do get rid of a lot of brute-force programming.
You can programmitaclly use a GridView control. It's inherently two-dimensional and you can use databound CheckBoxFields for it.
If you're looking for a quick and dirty way, you can use the AJAX Control Toolkit with the two controls and can populate one based on the other. If that's not what you're looking for, I'd do it the brute force way.

Resources