I am using C#,ASP.NET
I have a Gridview for which I have provided Sorting, Edit functionality. I am not able to perform EDIT when I perform Sorting. After sorting edit is set on some other row. I think there is some problem with the index it is taking..
Can any one help me how to fix this..
Regards
sbmarya
I think the issue is that the sorting is using a different call/datasouce than the editing. So in the RowEditing event I am getting an index relative to the sort order (either ASC() or DESC()). But then I am binding using getUsers() which is returning the data in a different order.
What I did is I stored some kind of a flag(Value) in ViewState to indicate what sort order I am in and made use of that when I am binding in the Editing event, so that I can call the right method to return the same datasource.
Regards,
sbmarya
I faced this problem as well. This is how I fixed it. (In my example the gridview is sorted on a column called submit date).
a. When the underlying dataTable of the gridview is crated and sorted store it in a session variable. The trick is, before storing to a session variable make sure you store the sorted view.
dt.DefaultView.Sort = "Submit Date" + " " + "DESC";
GridView1.DataSource = dt;
GridView1.DataBind();
Session["gridViewData"] = dt.DefaultView.ToTable(); //Only storing dt will not have the sorted table stored in session.
b. next, perform all edit/update operation using the dataTable stored in session in the above step. It will always come up in proper sorted order and you will not see the issue of row index changing unexpectedly after update.
Related
i want to send grid view data from one page to another without using database.
i am taking some inputs from user, storing and displaying in gridview. now i want to send same data gridview values to another page on click of button.
what will be the best possible way to do this.
Yous data not lives on GridView, but on the database, or on session, or on a file.
The GridView only shows them, not store them ! Even if you see them there, is actually on the html page on the client side, and they are not even post back.
So what you need to move to the next page is the "pointer" to the data, and reads them base on this "pointer"
You may have a simple List<> on the session, or an id and reads them from the database.
The solution is to saves your user input data somewhere - from what I understand the session for you can be a fast easy solution, and take them back on the next page.
put the gridview data into datatable
then put the datatable into session as shown:
DataTable tb= new DataTable();
Session["dt"] = tb;
on the next page, to read session value:
DataTable dt = new DataTable();
dt = (DataTable)Session["dt"];
to bind gridview data to datatable, check this link:
Putting GridView data in a DataTable
hope it helped :)
I have a GridView consisting of editable text boxes. When the user enters a value, the OnTextChanged function calls my UpdateColumns() method, which performs various calculations and alters the whole GridView. I have limited user input to only numbers so that the operation errors out when the user enters alpha characters in any cell. Upon this error, I would like to revert to the Data Table that existed immediately prior to the user entering the alpha character.
Is there a way to capture the current GridView's data source and store in a DataTable?
I have tried the following (after the data table has been bound):
Dim dt As DataTable = DirectCast(gvBuildingBlocks.DataSource, DataTable)
Session("buildingBlocks") = dt
'If textboxes are all numeric, perform calculations here
'Else display error here and bind table below
gvBuildingBlocks.DataSource = CType(Session("buildingBlocks"), DataTable)
gvBuildingBlocks.DataBind()
The above results in an empty gridview, as the value of gvBuildingBlocks.DataSource is empty. Is there anything else I can do to get the values?
I imagine I could loop through all the rows and save them somehow as follows:
For Each row As GridViewRow in gvBuildingBlocks.Rows
'Save cell value here
Next
But I don't know what code to use to save the values?
Any help is much appreciated.
Thanks!
I understand you are doing calculations on rows and you want to rollback to original values if the user input is invalid. You could accomplish this at the row level by calling DataRow.BeginEdit() before the edit, and DataRow.CancelEdit() if the input is invalid. See this example
Don't forget to call DataRow.EndEdit() if the edit is valid to commit changes.
The Setup:
I currently have a page with a GridView control on it inside of an update panel, using a SqlDataSource. I have a timer setup to update the GridView every X amount of seconds. Typically for what I am testing every time the GridView updates about 4-5 new rows of data are added to the gridview, while the last 4-5 get tossed out. I am only displaying 15 results at a time and will have new results coming in every update.
The Problem:
I allow the user to select the rows, while the GridView is being updated. I am handling this by setting the SelectedIndex property. However, when I select a row and then the grid is updated the row the user selected gets pushed down about 4-5 rows and the data in the previous selected index is selected instead. So where they clicked is selected at this point, not what they clicked.
I need a way to determine, if possible from the SqlDataSource/Gridview, how many new rows have been added to the gridview. OR a way to maintain the selected data by the data in the row and not just the SelectedIndex.
Any help is appreciated, thanks.
RESOLVED:
Ok I went ahead and added a new invisible column to my grid, and am now keep track of the unique ID's selected from the DB. By setting an array before databinding, and comparing that to the new array I get after databinding I was able to use a simple Intersect to determine the number of rows that are the same. Then I used that to determine from the total how many are new this postback.
Just an idea:
I think you can use an invisible column (more specifically an ID column) to store the selected rows' IDs value in the Session object and then after the grid updates, you can retrieve this value(s) and select the row(s) again if they are still present.
If you have custom GridView OnRowUpdating event.
public void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Session["CurrIndex"] = GridView.SelectedIndex;//index before insertion
Session["RowCount"] = GridView.Rows.Count;//row count before insertion
//Add new Rows
GridView.SelectedIndex = (Int32)(Session["CurrIndex"]) + ( GridView.Rows.Count - (Int32)(Session["RowCount"]);//update selected index
Session["CurrIndex"] = GridView.SelectedIndex;//restore the index into session
}
Scenario
I am using the latest version of DevExpress XtraGrid.
I am currently binding the DataView of a DataTable to the datasource of the gridcontrol in C#.
Since this DataTable gets updated every second, I have to refresh the gridcontrol.
ALSO
The DevExpress XtraGrid comes with the ability to automatically drag and drop the column headers into a group/sort like manner. This refresh problem I am having is particularly noticeable when I incorporate the use of the "group by column header" feature - In that the data that is grouped is collapsed under a single row. If I expand this row to see the data, it immediately shuts again upon Update - which at the minute is every second, rendering the grouping feature useless.
Question
How can I go about doing this without redrawing the whole thing?
I literally just want to see the numbers dynamically changing AND be able to use the grouping feature continually without it collapsing by itself when the rows of data update.
Current Code
DataView dvw = latestCurve.Tables[0].DefaultView;
dvw.Sort = "Ccy Asc AND Date Asc";
this.gridControl2.DataSource = dvw;
Assuming your default view for gridControl2 is named gridView2:
gridView2.BeginUpdate();
try
{
your code goes here
}
finally
{
gridView2.EndUpdate();
}
So I'm writing a page which does some reporting and it's pretty dynamic, the user has the ability to group data and sort columns. I've been able to get my dynamic grouping down and then the sorting except now my generated linq-to-sql sql order by statement is backwards from what I want it to be. I think I need to figure out how to get at the Result returned from the LinqDataSource when the Sorting event is fired in the ListView so that I can then append my grouping order by clauses.
Currently I'm overriding the LinqDataSource selecting event to provide it's result and do the dynamic grouping there, but then the Result is modified by the Sort on the listview and I was thinking there must be some way to get at that result during the Sorting or Sorted event.
Any insight would be appreciated, in the meantime I will be attempting to figure this out myself.
Awesome I think I found my answer, set AutoSort to false on my LinqDataSource and then sort the data myself, since I'm already sorting myself for the other data.
You can use Linq to manipulate the results of your queries (IEnumerables) based on user inputs. For example (very pseudocode event handler follows):
public void OnUserClickNameColumn(object sender, EventArgs e){
var data = DataProvider.RetrieveAllUsers();
// switch between sorting asc and desc when user clicks on a column head
SessionHelper.CurrentUser.NameColumnSort = !SessionHelper.CurrentUser.NameColumnSort;
if(SessionHelper.CurrentUser.NameColumnSort) // true = sort asc else sort desc
UserDataGrid.DataSource = data.OrderBy(x=>x.Name);
else
UserDataGrid.DataSource = data.OrderByDescending(y=>y.Name);
}