Using GROUP BY with SQLDataSource - asp.net

I have a SQLDataSource in an ASP.NET Web Form, bound to a GridView and would like to perform a GROUP BY on the underlying data after it has been bound.
I would like the grouped data to appear in a separate GridView.
How would I accomplish this task?

would like to perform a GROUP BY on the underlying data after it has
been bound.
You cannot do that. Once the data is bound, it is bound. The same data will be displayed in the gridview however it was pulled in query with out without grouping. You cannot change it afterwards.
I am thinking you want something different as your question suggests. You want to be able to group on the fly depending on what field the user select. For that you will need different set of controls. May be a dropdownlist and a Gridview Combination? That will be a different scenario.
You can also bound sql datasource on the fly in C# code rather than in ASP.NET code. In that you would be able to achieve it with some additional controls like if this button is clicked, group by this field or if that button is clicked, group by that field.

Related

How to loop gridviews for each database row?

I am developing an asp.net web form application that displays some info on separate gridviews based on parameters. The second gridview depends of the values in the first one (Salida and Llegada, they work as a time range). This works only when the data displayed on the first GV has just one row.
This is how it works:
But, is there a way to loop the same gridviews for each row stored in the database? something like this:
Or maybe there is an easier option I haven't considered.
Thanks in advance.
Ok it should be simple, first sit your gridview1 paging to fetch only view only one record, Then after you fill your gridview1 i assume that you have putten an ID either as Datakeys or what ever method you have used, the index of the row should be 0 since it's always viewing one row only. Get your id and fetch your data and bind it the second gridview2.
After that on the Gridview1_Paging event you bind your data again and the use the same method above to fetch the data for the next record.
Seems a bit clunky, but the simplest option may be to add GV1 and GV2 to a repeater. Each row of the repeater would essentially be the data source for your GV1.

Is it possible to have a SQLDataSource with a parameter that is based only upon the GridView that is binding to it?

I have a scenario where I want to put four identical Gridviews on the same page. (They will be on different tabs in an Ajax TabControl.) They show the same source data, but there are four corresponding groups of source data in a common underlying table. So I want to show Group 1 on Tab 1, Group 2 on Tab 2, etc. These Gridviews contain complicated controls, so I would prefer to use the same data source for all of them to avoid unnecessary repetition. The Insert and Update commands are completely identical.
So in theory I could build the Select command in such a way that I could filter the data based on the GridView that is binding to the SQLDataSource. The problem is that if I use the same SQLDataSource for all the Gridviews, I cannot find a way to have each GridView tell the SQLDataSource which one is calling it. I am thinking maybe this is not possible, because the SQLDataSource binds first before it knows what is binding to it, but I'm not sure. Can anyone think of a way to do this?
You can change the parameter value dynamically using OnSelecting event of SQLDataSource. This can be done in server side code.
Create a property which holds your current gridview unique key, which is causing SQLDataSource to fetch data from SQL database.
Assign this property unique gridview key on DataBinding event of gridview.
Based on this property change the parameter in OnSelecting event of SQLDataSource.
Let me know if I am missing something.

Gridview: column with different controls is different modes

Here is my scenario.
I have an objectdatasource-bound gridview where i want to show Students {name, age, class}
name:string, age:int, Class:Foreign key, int
I also want to edit it, but when doing so i want to show a dropdownlist for the class field.
Currently i solve this by
a)creating a view, binding the grid to that view
b) on the editItemTemplate, put a DDL and bind it to the Class table.
What i want to know is if there is a way without involving the view shortcut.
NOTE: this question, Bind column to a different entity source than the gridview is
is somewhat related, but not quite the same thing. in that example, the OP just wanted to show the related data. Since am also updating, i need both the key and the value.

Bind a Text Box to a Field Selected by SQLDataSource (VB.NET)

I'm looking for the easiest way to bind data from a SqlDataSource to textboxes dropped in Visual Studio 2008.
For example, I have 4 textboxes currently that have Address, City, State, Zip. I also have a SqlDataSource on the page fetching the ID of the record and selecting those 4 fields based on ID.
How am I able to quickly bind each box to those particular fields selected? I would think this would be really straight forward - but seems it's not. Seems like the answer is funneled towards having to create a GridView or some type of control.
Be gentle...I'm a nub :)
In general you are correct, if you want to use databinding you'll need to use an appropriate control. For this example I'd suggest using a FormView - it is designed to display the results from a single database record and uses templates, meaning you'll have complete control over the output. This article is probably a good place to start: FormView Control: Step by Step.
To read the values bound to the FormView in the code-behind class you would need to create an event handler for the FormView's DataBound event. In that event handler you would reference the controls programmatically via FindControl, like so:
Dim myLabel As Label = CType(FormViewID.FindControl("id"), Label)
Here, id would be the ID of the Label whose value you were interested in. Once you have a reference to the Label you can get its value using myLabel.Text.

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