Gridview Automatically Selects first Row after a sort - c#

I am working on a master/detail gridview and detailsview in asp.net web forms using an objectdatasource. The details view is displaying extra information about the selected row from the gridview. When I sort the grid, I want the currently selected row, before sort occurs, to remain in the details view after the sort is completed. Instead, the gridview is auto selecting the new first row whenever I sort.
I found a partial solution to this problem. If I set WhateverGridview.SelectedIndex= -1 onsort and the value becomes null. This makes it deselect any rows after a sort. This leaves the details view blank. However, what I want to do is maintain the selected row not nullify it.
So, does anyone have a good way to retain the selected value or prevent the details view from displaying after sort event fires.

Here you need to use the GridView.EnablePersistedSelection property. Set this property to true.
Setting this property to true means that GridView will make sure that the selection of a row is based on data-key values.
By default GridView makes row selections based on index. This is the reason why when you sort, gridview is selecting row based on index and you lose your actual selected row.

Related

Blazor server side table with paging and multirow select feature using bootstrap?

I have a table which display records in a for each loop.Each row has a checkbox to show selected rows.
I want to keep track of the rows selected in the table so that when i navigate back to the pages where I had selected the rows I can see the checkbox checked.
My question.
1.Can I do this somehow without injecting javascript with bootstrap or some logic in the code behind?
If not what other logic can i use to bind the checkbox? (I was thinking keep a list of indices selected and check onrendering the rows but hope there is a nice way to do this)
2.How can I highlight the selected rows when i check the corresponding row checkbox again without injecting javascript.
On further analyzing my requirement and Data I was able to achieve both the above conditions through a collection of indices selected and if conditions while building the UI to render the checkboxes and the styling the rows

GridView Sort and delete; Wrong row

So I have a grid view which gets data from a database. I want to be able to sort and page this table, as well as to remove single rows.
Now the Problem: I need to bind my data every time (also on postback), else my datasource will be null when trying to sort, but by doing so I will also get the wrong row each time I try to delete (or edit) a row.
The problem was that I thought that I had to bind the rows each time. All I had to do each time is to set the DataSouce and ONLY bind if (!IsPostBack). Then onSort i just bind again and it will be sorted. Also the delete-Function will then get the right row.

Partial view checkbox value not getting updated in MVC

I have a partial view, which is bound to a collection of data having property called as IsDelete, used to check if the user wants to delete that specific row.
On click of button, the data comes as expected.
The problem arises as I try sorting this collection, suppose I checked the first row check and now sort the rows, even after sorting still the first row checkbox is shown as checked while the data has moved from first to some other row after perform sorting.
Hence, when I try submitting the page, I am getting that user has selected to delete first row each time in this case, instead of the actual selected row.
Note : I am doing a server side sorting, using linq and then updating the partial view

Dynamic gridview based on dropdown

I've got a bit of an chicken and an egg.
I need to bind a gridview to a set of database values. The gridview is dynamic and the columns, cells are created at run time.
As such, I need to re-bind the grid on every postback in the pre-init, init events after very post back.
However, the data used to populate the grid uses a value from a dropdown box on the same page. As such, the value of dropdown is not accessible through viewstate until after the init event (i.e. the selected index is always the first item in the list until after on init).
How can I get access to the value of the drop down in time to rebind the grid before the pageload event?
If the Gridview always has the same number of columns, you could have the dynamic query generate column names as aliases, and then just rebind the Gridview on dropdown change?
you can use and HtmlHidden control for store the value you need. It doesn't depend by the viewstate and it's available into the page init

Stuck with datagridview and combo box column

I have a typical requirement.
I have a datagridview with a combobox column(items loaded at design time). When a user selects an item from combobox, remaining rows gets updated in database based on the selectedItem and dgv gets refreshed.
Problem is the combo box will lose its current selection and goes to unselected state.
I want to retain the selected item even after dgv refreshed.
Could anyone help me out
Thanks in advance
Do you mean that you're using an unbound comboboxcolumn? If so, the value can't automatically be persisted when refreshing the datasource. You need to store the selected value before updating and setting it in code after refresh.
If your column is actually databound, the selected value is either not stored in the database or you have some data type problem.
Is the combobox there to let the user select a value for the field or do you use it as a way to execute a command on the record?
Do you have any code you can post?
Datagrid Combo-Box value will retain the string value but will refresh any integer values automatically.
Here is what you need to do :
-When populating Combo-Box value to just convert its value to toString().
-Also if you are setting a default select value also set it with string type.
-Your Combo-box will automatically retain the value selected even after refreshing.
:)
have a combobox in ur datagridview. Assign values to it using a bindingsource.
then write an eventhandler for the datagridviews "EditingControlShowing" event.
in that, remove had handler if any exists for the comboboxes Selectedindexchanged event. then add an event handler for the selectedIndexChanged event say "ComboBoxValueChanged"
in that "ComboBoxValueChanged",
DirectCast the sender to System.Windows.Forms.DataGridViewComboBoxEditingControl and get the selected value of it.
Now use it to compute any value you want.
you may wana refer to this
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol.aspx

Categories