Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Shifting gridview columns, doable?

We have a Windows application, done in proprieroty software. It has something like a gridview/datagrid rows/colmns of data on a screen. It has 10 columns: Name, ID, Address, phone#, etc.

If user searches by Name, the first column displayed is name. If use Searches by phone#, first columns is shown is Phone etc.

*** the key for the grid row will always be the ID

Is this doable in ASP.Net's gridview??
Avatar of samtran0331
samtran0331
Flag of United States of America image

the short answer is yes, it is doable with a Gridview.

But there are a few different ways to implement it.

The easier ways to "shift columns" is to just leave the Gridview with autogeneratecolumns=true
and then change the underlying datasource, datatable or dataview...
Avatar of Camillia

ASKER

"then change the underlying datasource, datatable or dataview..." not sure what u mean by that...

I'm calling a stored proc...coulmns are  ID, Name, Phone, ...

Have different Stored procs with different combinations of columns (i dont think u meant that)...

OR grab the data in a datatable, dataset something and shift columns like that?

Let me ask you this...u think I should even do that? I need a good reason (maybe performance)  to tell my manager why i dont want to do it this way ( i want to keep my coding simple but of course i cant tell him that :) )
have a look at this:
http://msdn2.microsoft.com/en-us/library/ms752213.aspx
scroll to the section: "User Interactions with a GridView"
"Users can reorder columns in a GridView by pressing the left mouse button while it is over a column header and then dragging that column to a new position. While the user drags the column header, a floating version of the header is displayed as well as a solid black line that shows where to insert the column.

If you want to modify the default style for the floating version of a header, specify a ControlTemplate for a GridViewColumnHeader type that is triggered when the Role property is set to Floating. For more information, see How to: Create a Style for a Dragged GridView Column Header."

my current project, we're using the telerik grid so I can't test right now...but that's really interesing...
...oh wait...that's probably talking about winform isn't it?
speaking of telerik, have a look at this:
http://www.telerik.com/demos/aspnet/Grid/Examples/Programming/WebMail/DefaultCS.aspx

the example allows you to drag/drop columns to re-order them
I did come across that link and thought it was for web. I cant use any third party tools...

 ex-developer did it in .Net 1.1 and datagrid. Have to get a copy of his code but his code is really crappy...
>>grab the data in a datatable, dataset something and shift columns like that?
That's what I was thinking.

>>Have different Stored procs with different combinations of columns
no, I wasn't thinking different sproc's, but you can have a conditional "if" statements in one stored procedure to execute different selects...this is the cheesiest way to accomplish the "column shift"...but also the quickest...so your sproc would have another variable like @sortcolumn

and the sproc becomes something like:
if @sortcolumn="name"
select name, address,ph...etc
end
if @sortcolumn="address"
select address,name,ph
end



let me think about your first method. I will open a new question if my manager doesnt agree with  the "non-shifting" of columns.
crap, my manager wants shifting columns...

let me ask you this... suppose i have 5 columns. The first column is ID and it is the "key" to the gridview...i mean...the column that will have the hyperlink which user can click on....

can I have more than one column with hyperlink? i dont think i can...so when I shift, i have to remove the hyperlink from the shifted column??
>>suppose i have 5 columns. The first column is ID and it is the "key" to the gridview...i
>>mean...the column that will have the hyperlink which user can click on....
>>can I have more than one column with hyperlink? i dont think i can...so when I shift, i
>>have to remove the hyperlink from the shifted column

when the user clicks the hyperlink, what happens?
you can use a "select" column that never shifts and make that your hyperlink column that always appears first...and then the rest of the columns shift after
it depends what you're doing with the hyperlink, and in some instances, you can make all the columns hyperlinks that do the same thing
>>maybe with that example.
yes, that's in line with what I was thinking...
thanks. So it's dynamic building of the gridview.
But is it really worth it? I think users/my managet should understand that some functionality will be different in a web app than in a winform app done in a proprietory software...

If i do dynamic, then sorting/paging will be dynamic too...right? just too much hassle...

any performance issues with doing it dynamic?
...that tutorial is manipulating the gridview columns...but you can see it's working closely and relying on the datatable that will eventually bind the gridview...so what I was originally thinking is manipulating the datatable and then leaving the gridview to bind to the datatable with autogeneratecolumns=true
**after** creating the datatable...

...realisitically, it doesn't really matter...the "shift" logic is the same...just where do you want to do it? in the sql, the datatable, or in the gridview....

how do you know which column the user is searching?

For searching, I have a "where" clause that i build in the code behind but you're right...i can have combinations of searches..for example, user can choose NAME and Account...then what should the first column be?? I think it's just stupid that they want this like winform app... in that app, users cant do combination searches...it's either search by name OR search by Account OR search by whatever...

 I can change my manager's mind only if I can tell him it will affect performace :)  If i use dynamic gridview, then i have to dynamically localize the headers as well, paging too, sorting too...right?
ASKER CERTIFIED SOLUTION
Avatar of samtran0331
samtran0331
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
let me try it. Thanks for sticking with this.
your solution does away with the combination search. User has to choose the field they want to search on. For example, if they cant select NAME and ACCOUNT. In case of combination search, i guess we need to make an assumption and say...ok...display NAME first...

i've presented the solution/options to my manager. He needs to decide if he want the shifting column or combination search.
...sure...in the "search column" dropdownlist, have a "search all"....and then add to the sproc to search all columns....and leave the column orde alone...

or...you can still get slick with the stored procedure....and shift the columns based on which column returns the search criteria...

the sproc would have to do a count of each column searched first....
...but a "search all" could still be done and returned all from one stored proc...
ah, yeah, very good idea...

I think one issue i see is this: since I'm shifting columns, I think all columns have to be hyperlinked. I will hide the "id" behind all the columns as my key...

Now, user searches by NAME, this will be the first column, this will be hyperlink. BUT, I dont want the rest of the columns to be hyperlinked now. (The vendor windows-app only makes the first-searched-by-column hyperlinked)....

how can I  **un-hyperlink** the rest of the columns??
i think i will open another question for this as it will get off-topic. I will post the link here .