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

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.

Related

ASP.NET - Trying to implement SortParameterName property to SqlDataSource

I have several Gridviews and Repeaters bound to SqlDataSources using stored procedures. I am trying to implement sorting functionality into some of these but am having a hard time finding concrete instructions and/or examples of what is required on the SqlDataSource side to generate the ORDER BY's needed. Particularly, I do not understand the point of having a SortParameterName property in the SqlDataSource if all it does is manually connect to an ORDER BY clause in the stored procedure. Why define it as such if it is just another parameter in the SelectParameters list like any other, but just so happens to be connected to the ORDER BY clause? When I run the code example below, I am told there are too many arguments specified (obviously, the extra SortParams argument). Do I really need to alter my stored procedures and add "ORDER BY #SortParams" clauses to the end of the existing queries to make this work? I feel like I am missing something.
SqlDataSourceInLine.SelectParameters.Clear()
SqlDataSourceInLine.SelectCommandType = SqlDataSourceCommandType.StoredProcedure
SqlDataSourceInLine.SelectCommand = "ApproverGetApproved"
SqlDataSourceInLine.SelectParameters.Add("CompanyID", ConfigurationManager.AppSettings("Temp_CompanyID"))
SqlDataSourceInLine.SelectParameters.Add("SortParams", "EmpName DESC")
SqlDataSourceInLine.DataSourceMode = SqlDataSourceMode.DataSet
SqlDataSourceInLine.SortParameterName = "SortParams"
Dim dv As DataView = SqlDataSourceInLine.Select(DataSourceSelectArguments.Empty
Any clarification would be appreciated!
I was just trying to figure out how to use the SortParameterName and found this question. After doing some more search I think I have now found the correct answer.
Microsofts page Sorting Data with Data Source Controls says as follows (my emphasis):
The parameter identified by the SortParameterName property is passed to the ObjectDataSource control's SelectMethod or passed as part of the parameter collection to the SqlDataSource control's SelectCommand. The ObjectDataSource control can use the information passed to it in the sort parameter to return the data in sorted order. For the SqlDataSource control, you must supply the name of a stored procedure that can take the sort parameter and return the sorted data, because you cannot pass a parameter as part of an ORDER BY clause.
This indicates that the answer given by Icarus is not correct.
My conclusion is that when the SortParameterName property is set (in combination with an appropriate stored procedure) the Gridview will not do the sorting itself, but will let the datasource do a so called Custom Sorting, which for example would be the necessary way to sort if Custom Paging is used.
Update:
I have now used it in my own programming and confirmed that my conclusion was correct.
I've never used the SortParamter in the past, but what I gather from the documentation is that the purpose of this parameter is to allow you to get the results sorted in the way you want them in case the stored procedure does not do it already. Other than that, you don't need to use it for anything. A GridView whose datasource is of type SqlDataSource already implements sorting out of the box. You simply need to set the AllowSorting property to True and the SortExpression on every column.
Example:
<asp:GridView ID=" productsGridView" Runat="server"
DataSourceID="SqlproductDataSource" AutoGenerateColumns="False"
AllowSorting="True" >
<Columns>
<asp:BoundField HeaderText="Product"
DataField="ProductName" SortExpression="ProductName">
</asp:BoundField>
...
Nothing is required on the SqlDataSource. You need to implement the Sorting event for the Gridview and something else for the repeaters since they don't have sorting built in. What you could do (if you have small datasets coming back) is to use ViewState and store your DataTable of results and then utilize the DataView and the sorting capability of that, then bind the Repeaters/GridViews to the sorted DataView. You still have to keep track of the SortDirection and SortParameter within ViewState, regardless.

Using GROUP BY with SQLDataSource

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.

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.

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