GridView Sort and delete; Wrong row - c#

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.

Related

Refresh Column order in DataGridView after adding column and using SetOrdinal

I have a DataGridView that shows summary data. It is bound to a BindingSource which is bound to a DataTable. Depending on the summary type selected by the user, the program may split the data into multiple columns. In my code, I update the existing column and add columns as needed. The new column, as expected, gets added to the end of the list and I use SetOrdinal to reposition it. The DataGridView, however never moves the column unless I blank and rebind the DataSource to the BindingSource as follows:
I add columns with the following code:
this.Columns.Add(newcolumn);
this.Columns[columnname].SetOrdinal(index++);
And I refresh the source as follows:
teamstats.UpdateColumn(teamstats.Columns[columnClicked].ColumnName);
bsTBAstats.DataSource = null;
bsTBAstats.DataSource = teamstats;
This is problematic, as after I refresh the DataGridView, I have to reapply my formats (cell formts and event handlers) for each of the columns which slows down the process significantly.
Is there a more efficient way to have the DataTable refresh the column order? If the user can reorder the columns manually, why can't I do it programmatically?
The best way I've found to accomplish what I asked above is to manually resort the columns in the DataGridView to match the order of the columns in the DataTable. The following codes shows the process I used:
foreach (DataColumn c in teamstats.Columns)
dgvTBAstats.Columns[c.ColumnName].DisplayIndex = teamstats.Columns.IndexOf(c);
Seems as though the binding process should automate this, but this works. Unfortunately, if the user has manually reordered the columns, that order is lost since the opposite is true and the DataTable doesn't get reordered to match the DataGridView.
I can't seem to find a better way to bind the column order.

Gridview Automatically Selects first Row after a sort

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.

Refreshing a particular cell of a grid without loading entire grid view

Is there any way to refreshing a cell of a grid view without refreshing the entire grid view in c# asp.net
you could possibly use a templatefield, put an UpdatePanel in it and refresh that single cell that way. You would just need someway to initiate the refresh.
You may have to use a nested UpdatePanel within the cell.
If you don't want to put an UpdatePanel in every cell in your GridView, you can refresh a cell on a regular, full postback. The contents of the grid will be roundtripped, but your data source won't necessarily be queried in full.
First, make sure your grid doesn't rebind on every postback, e.g. wrap your gv.DataSource = x; gv.DataBind(); inside an if (!this.IsPostBack).
Then you should be able to do something along the lines of
((Label)gv.Rows[x].Cells[y].FindControl('myLabel')).Text =
GetDataItemNumber(x).FieldForColumnY.ToString();
Bear in mind that gv.Rows[x] may not correspond to the xth item in your data source, as Rows includes header rows etc. You might need to iterate through Rows checking e.g. IDs to find the correct row.
Disclaimer: I've never actually done this before, but it should be possible...

Populate a datagrid from another datagrid in another form

As mentionned in the title, I try to populate a datagrid from data of another one. Basically, The first datagrid is populated from the active directory. I do some operations on these datas, and I would like to make a report of these operations (Passed or failed).
I'd like this report in another datagrid in another winform.
As my operations is to delete parts of the datas I get in my firt datagrid, I cannot populate the second one the same way it works for the first one
So I'd like to get the datas from the first datagrid, add them to another datagrid in the second form, add a column 'Status' and populate it with the results of my operations.
Is there a way to 'copy' the first datagrid ? Is there a best practice to do this kind of job ?
Thanks!
Of course there could be many possible ways, but that came to my mind is.
You know the number of columns, so make a list of class(corresponding to your fields). Extract the cell data from datagrid and populate the list with the entries (if all required). Import this list to the other form. Set Datasourceof datagrid equal to list and refresh.

How to hide the empty Column

Using C# & asp.net
if there is no data's in the table, gridview displaying one rows (it was empty)
I want to hide the gridview empty row.
How to do this.
Assuming that you can normally delete that one visible row just check that if a field that would normally have a value is empty and the row count is 1 then delete that row.
if(String.IsNullOrEmpty(mydatagrid.Rows[0][0].ToString()) && mydatagrid.Rows.Count==1) //Check a field that would normally have a value
{
mydatagrid.Rows.RemoveAt(0);
}
Let me know if this helps/works
If you are manually data binding than you can check at that time and hide or disable the control if there is no data. If you are data binding using the design view than you should subscribe to the DataBinding or the PreRender events on the control and check then.
you can check if the datatable doesn't have any rows
use:
mydatagrid.DataSource=null;
mydatagrid.DataBind();
As the other two comments you can either check in code and set MyDataGrid.Visible to true or false to hide the entire table, or you can not bind the datasource, or you can EmptyDataTemplate option to display whatever you want when there is no data for the GridView to display normally.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx
The normal behavior for GridView is to render NOTHING if there are no data rows. (Well, almost nothing; it insists on rendering a <div> wrapper, but that'll be empty.)
If you specify an EmptyDataTemplate, however, a one-celled wrapper table is generated to contain whatever is in your template. Even if your template is empty, you'll get that wrapper table (with its one cell, empty). Sounds like the answer to your question is don't specify an EmptyDataTemplate.

Categories