Gridview sorting C# - c#

I have a MSSQL DB, with lot of records. I have a column in my table with serial number (Auto generated). I am populating the DB in grid-view (C#), I want to sort the gridview based on this serial number column while the gridview loads.

Well why don't you pass already sorted data to gridview.just use order by clause in your sql query and pass the query results to gridview which will be already sorted.
select * from tablename order by SerialNumber(Sql syntax)
About the second solution u can use dataview class to sort data before passing to gridview

Related

How to implement custom paging in gridview in ASP.NET using C# with data modified in code-behind

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.

how to populate Datagridview each row from specific sql query?

Working c# ,want to fill datagridview for each row sql query is different ,Given a button to add the row ,so sql query is user specific.But not find way ,join query not worked because after 1st query filled the datagridview ,when next query complete datagridview data refresh.
As performance wise ,what is economical either direct query from sql for each row or get the table as datatable once and query from that table to populate datagridview?enter image description here

Dynamic DataGrid Header Text

Have an application i'm coding at the moment, which uses DataGrid's. What i want to do is replace the header text with that from a column within an sql database.. Has anybody tried this or any idea on how it could be achieved?
What you should do.
Fill a DataTable with data using an sql query(Easy and searchable on Google and Stackoverflow).
Say your query is
Select id, Emp_Name from emp;
Provide that Datatable as ItemSource to DataGrid.
Now your columns of grid will be same as that of your sql query. ie. datagrid will have 2 columns (id and Emp_Name)

Separate table column into multiple GridView columns?

I am retrieving a column from a table using an SqlDataSource in ASP.NET/C#, which contains multiple 'values' separated by commas. I'd like to be able to separate this values into multiple columns in my GridView, and also allow them to update them. Is this easy to accomplish?
Thanks
I would prefer to split the values to multiple columns before retrieving data from SQL.
But if you cant do that then you will have to loop through every row and separate the values in your datatable before you bind your data to the gridview.

"Dereferencing" data from a databound GridView

I have a GridView that is bound to a select statement from a table. That table contains a lot of keys out to other tables that are just IDs. I would like the GridView to "dereference", as it were, the id of the field in question and display the human-readable name found in the other table.
At the moment, the options that come to my mind are composing a DataSet by hand in the codebehind and binding the GridView to that instead of the SqlDataSource or creating a stored procedure to return the table already "dereferenced". Any other ideas or recommendations for this situation? I am using .NET 2.0 per employer mandate.
I am not entirely sure what you mean by "dereferencing" but if you just want to display the value from the tables refered to by foreign keys then just bind your gridview to a query that joins all the required tables in a flat dataset. e.g.:
SELECT Table1.Field1, Table1.Field2, Table2.Field1, Table2.Field2
FROM Table1
LEFT OUTER JOIN Table2 on Table1.Table2ID=Table2.ID
This can be done either in a stored procedure or by directly specifying this as the SelectCommand property.
I just ended up creating a view that automatically dereferenced the ids to the other tables and using that for the basis of the GridView.

Categories

Resources