How to reorder columns in gridview dynamically - c#

Similar questions may exists, but none of them seems to be helpfull. So I'll try to explain a more specific case, and see if anyone can help me :)
Here is the thing, I have a gridview with templatefields, and I want to let the user to specify the order those columns are shown. So, the user creates a view and decides which column to display first, second, and so on.
So basically, I need to change the order of the columns just after the grid is loaded with data.
Sounds easy huh? Well, apparently it's not. At least I couldn't achieve that just yet.
Some notes:
- Of course I have AutogenerateColumns set to false.
- Change the sql select columns order won't work because of previous item.
- And I'd like not to generate the columns by the code.
Any ideas?

You can modify the Gridview's Columns collection in your code-behind. So, one way of doing this is to remove the column from its current position in the collection and then re-insert it into the new position.
For example, if you wanted to move the second column to be the first column you could do:
var columnToMove = myGridView.Columns[1];
myGridView.Columns.RemoveAt(1);
myGridView.Columns.Insert(0, columnToMove);
If you need to move them all around randomly, then you might want to try to clone the field collection, clear the collection in the GridView, and then re-insert them all in the order you want them to be in.
var columns = myGridView.Columns.CloneFields();
myGridView.Columns.Clear();
myGridView.Columns.Add(columns[2]);
myGridView.Columns.Add(columns[0]);
etc..
I'm not 100% sure whether this all will work AFTER binding to data, so unless there's a reason not to, I'd do it in Page_Init or somewhere before binding.

I know this is an really old question but the suggested answer did not solve the problem fully.
The ViewState for the GridView will failed after remove / add a dynamic column.
If you need to bind any event, like OnRowCommand and OnRowUpdating, such events will not be fired.
Edit:
Look into this function if you need answers.
Protected Overrides Function CreateColumns(dataSource As PagedDataSource, useDataSource As Boolean) As ICollection

Related

C# Gridview: Want to create an extended gridview class that automatically hides rows with a specific value in DataKeyNames column

I'm trying to create an extended gridview class that will enable me to always have a footerrow that can be used to add data. That's the end goal.
I've tried classes that were found elsewhere on stackoverflow, but the code I found is apparently buggy (it either works when the footerrow is the only row, or it works when the footerrow is not the only row, but never both. Possibly related to the fact that I have dropdownlists in my rows). This problem was explained here: Bizarre issue with customized gridview control
The class itself is also problematic, in that the formatting is kind of messed up when the footerrow is the only row. It just doesn't look right... the color formatting is gone.
So now I'm thinking what I really want to do, is rewrite the data sources so they're based on UNION queries, so that there's always one blank row with -1 in the key column (my tables always have single incremental primary keys, so no legit row will ever have -1 in the key column), and then have a class that automatically hides any row where the value in the "DataKeyNames" field is -1.
Problem: I have no idea how to do this. :) I know how to do it in the aspx.cs code (RowDataBound event -- if(condition) {e.Row.Visible = false;}), and I know how to do it via CSS, but I don't know how to do it automatically in an extended class. I'm afraid I'm a little lost when it comes to writing extended classes.
To sum up, what I'd like to do is put an extended gridview class in my aspx code, set the usual gridview things, and have the only additional thing in my code be an added "UNION SELECT" in my datasource, and have it just automatically hide the row that the UNION SELECT provided, so that there's always at least one row in the gridview (thereby taking care of the entire footerrow not showing up problem), without me having to code anything extra in a RowDataBound event every time, because I have these gridview controls EVERYWHERE in my code.
Many thanks for any help you can provide! :)

Always keep one specific row on top while sorting the others as usual

I've got an aspx with a GridView component.
My requirement is to always keep one specific row on top while sorting the others as usual, i.e. by clicking the header.
I tried with the OnSorting tag but regardless of what order I feed it into the datasource it does it's own resort anyway.
How to override this behavior, so that when the header is clicked I do the sorting in the codebehind myself, ignoring the gridview sorting?
EDIT:
GridView.DataSource = list.OrderBy(x => x.type).Select(....
GridView.DataBind();`
Initially.
Remove your special row from the list. and keep it separately.
sort the list.
bind data.
Insert your special row at 0 place of grid view
Then
DataGridView.Sorted event
Remove your special row from gridview
Insert your special row at 0 place of grid view.
Unfortunately I cannot figure out what it was. But repeated full rebuild solved this for me...

Filtering and then selecting data from flexgrid

I want to copy data from flexgrid and I have a little problem. I am using filtering and I just want to copy selected data but It´s copy data which are “hide” (not show thx to filter). For example, I used filter and in flexgrid there are just few rows and I want to all copy so click to left upper cell and it select all shown rows but when I past it somewhere it past all rows (with that which arent shown). Same when select rows with Shift button.
How can I “fix” it? I know that´s normal and it´s not error but I need to change it and I hope that there is easy way to do that. Change some property or something like that.
But if there is not I actually dont have idea how to do that “hard” way. I was thinking that maybe use _c1FlexGrid.Rows.Selected and some way control if selected row is shown. But I dont know how.
Thanks for help
Edit
I figure out that if I change SelectionMode for flexgrid from Default to ListBox then I can use Rows.Selected and it has property Visible which I can check if row is shown or not. So I can thanks to this take just rows which are shown. But now I don´t know how can I add these rows to clipboard. Before I was using this:
Clipboard.SetDataObject(_c1FlexGrid.Selection.Clip);
but now I don´t now which class use to save shown rows and then at to clipboard. And there is problem with selecting because I like SelectionMode which was default (CellRange) and selecting just all rows it is not ideal. Any ideas?
Because no one else get with better solution I´ll write here mine. As I wrote in Edit in question there were problem with SelectionMode as CellRange. I still don´t know how to "fix" it with this mode but I figure out how to do it with ListBox mode.
So in flexgrid change mode to ListBox. Then you can use this in your copy method:
foreach (C1.Win.C1FlexGrid.Row item in _c1FlexGrid.Rows.Selected)
{
if (!item.Visible)
item.Selected = false;
}
Clipboard.SetDataObject(_c1FlexGrid.Clip);
I hope that this help someone else too :)

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.

Why isn't my datagrid updating?

I have an object that has as one of its properties, a List. I want to bind a datagrid to that list, such that when I add objects to the grid, the datagrid updates. I tried:
myDataGrid.DataSource = myObject.MyList;
but when I update the datasource with new rows, the grid doesn't update.
Then I tried:
myDataGrid.DataSource = null;
myDataGrid.DataSource = myObject.MyList;
Calling the above code every time I added an item. This resulted in an error when clicking on the grid (specifically, index -1 has no data, something to do with the datagridview.get_current internally. Happens despite the fact that I'm not clicking the -1st row).
So then I tried:
myDataGrid.DataBindings.Add(new Binding("DataSoruce",myObject,"MyList",false,DataSourceUpdateMode.OnPropertyChanged));
That didn't reflect the updates either, so I added:
myDataGrid.DataBindings[0].ReadValue();
whenever I added an item, but it has no effect either. I feel like I'm circling around a simpler solution to this problem, but I can't seem to find it. Any pro tips?
You seem to already know this, but you want to use a BindingList if at all possible here. Any hamfisted attempt to make a List function like a BindingList is just going to be a lot more pain than simply copying the elements from a List you already have to a BindingList.
If I changed my type from List to BindingList, all the problems go away and the grid autoupdates as expected.

Categories