I have an ASP.net C# page that uses a stored procedure in a single SQL query to return multiple tables.
I then bind each GridView in the page to the corresponding table from the query:
grdCustomers.DataSource = objDS.Tables[3];
grdCustomers.DataBind();
This works well but clears out the data when you sort the GridView columns. I understand I need to bind the data again on post-back but I'm not sure of an easy way to do this since the GridViews are not bound on page load now.
What's the easiest way to keep the GridView binding on sort as well as sort the columns correctly when binding the data in code behind?
Related
I have looked-up custom paging with gridviews in ASP.NET, but it seemed to work only for displaying exact same rows from the database to the web page.
In my case, I currently use default gridview paging to select all rows from the database, then process that data in the code behind of my aspx page, and then show custom rows based on that processed data in my gridview.
My question is: I want to know how to select a certain amount of rows from the database that will reflect just enough "custom" rows for 1 page in my gridview on the web page.
Note: I process the data on the front-end and group them in custom rows based on a common, same FKID value.
Would really appreciate the help,
Regards.
Does each FKID-value have the same amount of rows in your table? I assume this isn't the case.
I think your options are the following:
You could leave your logic as-is. If the performance isn't too bad, I think this is the easiest solution.
Create a view at database-level. This view should then directly provide records as you need them as a row in your GridView. This puts the paging-logic at the database-level, and is performance-wise the fastest option I think.
Do the same as option 2, but then at your application level. That could work this way: Construct a select query that gives you the count of distinct FKID values, and a query that can give you a list of FKID values based on a 'startindex' and 'number of rows'. For each FKID returned, fire another query fetching the corresponding rows. Apply your logic, and return the page of rows to your GridView.
I need to make a table with huge data from a database, I've done it very easy with a gridview in asp.net but there is a big problem,I need to customize that table a lot.
I want something like this, with a row with data about a person and the row after with a description of projects he's in, then another row with another person data and so on.
Table:
https://ibb.co/dJmEqw
And when i click/hover on a platform(platform i want to be a accordion propriety/it can be a button too) to display a table in the right or down side.
Table inside the platform's accordion:
Hope you get the idea guys, how can I do this with horizontal table lines, tables in accordions click/hover and databind? I need to take data from database and put it in the tables I described but I don't know if i can do that with asp.net gridviews which automatically generate the rows/columns it needs or is it another method ?
Any idea, advice, code is welcome, thanks!
A GridView is a simple "PowerPoint" solution for simple data models.
If you want more complex things get rid of the GridView and use a Repeater to create your HTML table instead. The Repeater control lets you create the table row tags in the ItemTemplate. You can easily generate two rows for each item in DataSource.
Of course, you need to generate the table cells (columns) manually as well with the Repeater control. Use Literal or Label controls to view the data in the table cells. Use
<%#: DataBinder.Eval(Container.DataItem, "propertyname") %>
to assign the Text properties of the controls.
I populate a gridview from the db.
then i re arranged the rows in the gridview with javascript code.
i want to copy the rearranged gridview to a datatable
how it possible??
If you re-arranged your data using Javascript, you will have to update the database using Javascript. You will not be able to do if using server-side code.
Getting data and binding a GridView on the server-side and then rearranging it with Javascript is not a good idea. I suggest you either get your data purely from Javascript, or arrange your data on the server side before binding your GridView.
There could be multiple methods
First method
GridView row would have a hidden field in each row which should be updated with new row index and have a column for row index / order in database.
When user changes the row index of GridView Row, you will have to change the hidden fields of all effected with new rowindex. On server side you will have to iterate through the GridView rows and save the respective rows index / order in row column for order of row.
Second Method
You can send the changed rows index on each arrangement using the ajax (jquery ajax) and save the changed index in database row order column.
if you need to sort the gridview without reloading the page , then use an update panel enable and code for gridview pageindexchanging and sorting event , if you need any indication while processing use an updateprogress control .
If you are intended to update the data . then use ajax to send the data to a webmethod and update it into DB
I am having a problem with the RadGrid using Multiple Columns when I Bind a DataSource to the RadGrid. Firstly let me show some screenshots of what I am trying to do.
This is an example from Teleriks website under demos and the multiple columns (summary column) is what I am trying to achieve
This is the exception I am getting when I try and bind the control to a DataSource
This is the RadGrid I have set up in my ASP page.
Firstly I don't want to bind the datasource in my asp page. Secondly I wanted the "Product" and "Other" ColumnGroups to be used only as a Summary column and not to be bound to a datasource since these ColumnGroups are only used as headers for the other Columns.
Is there a workaround to not allow the ColumnGroups not to be bound to a DataSource while the normal columns are?
Firstly, each one of your columns is specifying a ColumnGroupName that does not exist. Your two groups are called OtherDetails and ProductDetails not Product and Other. Change them to resolve the exception.
Secondly, the GridColumGroups are simply groups, as they are not bound to any data from your datasource; they are simply for aesthetics.
I have a TextBox entry field where the user will enter a integer value. And then there is a "Create" button, which when clicked upon must generate a Table with 2 columns :
"Name" and "Email" being the column headers.
I want each row to have a textbox in each of these columns.
All of this has to happen after the button is clicked. I have discovered that if you dynamically add a control in ASP.NET(I am using C#) then the controls are lost during postback. And I don't know how to prevent that from happening.
Can somebody please give me some ideas regarding how to go about adding rows dynamically to a table (I tried using the asp.net Server side table control but ran into the "lost-during-postback" problem - can I try with something else like a gridview ? but afaik a GV will not work without data bound to it )
Point to note is that my table has textboxes for user entry and it is not for showing data ..rather it is for accepting data from the user which will be later used to persist details to the database.
Dynamic Controls
That's involved. Here's an interesting article on the issue of dynamic controls.
I think normally if you create dynamic controls then you're responsible for recreating them on postback; however the article contains a way to use the Init event if it's copacetic with your app.
Edit:
or Regular ASP.NET Controls
You can use data bound ASP.NET controls like: DataList, Repeater, GridView, etc. (You can put text boxes and all other kinds of controls into them for repeating in each row - or "item") and bind values to it. If you need to perform some processing on the user input first, then generate an internal Array, List, Dictionary, DataTable etc. (your choice) and bind that data as a data source to the ASP.NET control of choice.
aspnetControl.DataSource = myDataTable;
aspnetControl.DataBind();
or
aspnetControl.DataSourceId = "name_of_control";
There are various ways to assign a data source.
This way you won't run into the same problems as with dynamic control creation; however there is a lot to learn to facilitate this way too.