How can I sort on my new gridview column(user defined)?

I have a gridview bound to a SQL datasource:
       
    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Height="486px" Width="661px">
                <columns>
                    <asp:BoundField DataField="spid" HeaderText="spid" SortExpression="spid" />
                    <asp:BoundField DataField="Login" HeaderText="Login" ReadOnly="True" SortExpression="Login" />
                    <asp:BoundField DataField="DB" HeaderText="DB" ReadOnly="True" SortExpression="DB" />
                    <asp:BoundField DataField="SERVER" HeaderText="SERVER" SortExpression="SERVER"/>
                    <asp:BoundField DataField="LOGIN TIME" HeaderText="LOGIN TIME" ReadOnly="True" SortExpression="LOGIN TIME"/>
                    <asp:BoundField DataField="USER COUNT" HeaderText="USER COUNT" ReadOnly="True" SortExpression="USER COUNT" visible="FALSE"/>
                    <asp:BoundField DataField="siteName" HeaderText="SITE" SortExpression="siteName"/>
                  </columns>
            </asp:GridView>

Open in new window


I added a new column onto the query as so:
'' AS siteName

Open in new window


It's working fine as far as displaying my data in the new column, but I notice that if I attempt to do a sort on that column, I
lose that columns data, and have to refresh the p[age to get it back.
Is it possible to sort by a column added onto the SQL datasource in this manner?

The columns data is derived at runtime, via a simple bit of code like so:
GridView1.Rows(i).Cells(6).Text = "MY_CELL_VALUE"

Open in new window



Thanks,
Rich
Richard GouetteIT ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ManjuIT - Project ManagerCommented:
What do you mean by "I lose that columns data?". Per you, you've inserted blank value to "siteName" right?

Try inserting "1" for siteName & check the result and let us know.
0
Pawan KumarDatabase ExpertCommented:
>>It's working fine as far as displaying my data in the new column, but I notice that if I attempt to do a sort on that column, I
lose that columns data, and have to refresh the p[age to get it back. Is it possible to sort by a column added onto the SQL datasource in this manner?

You have to rebind the data to the gridview. How are you sorting the data? Can you please show me your code.
0
Dirk StraussSenior Full Stack DeveloperCommented:
If the added column is something that isn't coming through from your database, why not add a TemplateField as the new column. Have a look at this article: ASP.NET GridView control demo to see how this works.

<asp:TemplateField HeaderText="SiteName" SortExpression="Sitename"> 
    <ItemTemplate> 
        <asp:Label ID="lblSiteName" runat="server"></asp:Label> 
    </ItemTemplate> 
</asp:TemplateField>

Open in new window


Initially, by just adding this template field you should get a blank column on your grid.

Next, add the OnRowDataBound event to your grid. Inside that, find the lblSiteName label and give it a value:

if (e.Row.RowType == DataControlRowType.DataRow)
{
    Label lblSiteName = (Label)e.Row.FindControl("lblSiteName");
    if (!lblSiteName == null)
    {
        lblSiteName.Text = "This text is set from code";
    }    
}

Open in new window

Please note: The code is untested. It might be a bit wonky, but that is the general idea.
Now, every time the grid refreshes, the Site Name column is populated according to whatever logic you use to get the Site Name.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard GouetteIT ManagerAuthor Commented:
Thanks for the direction Dirk,
When
GridView1_RowDataBound

Open in new window

fires,
I lose the Grid Binding for some reason.. meaning, no datasource, nor any rows..when
I step through debugging.
Am I not passing the Gridview onto the sub?

Here's what I'm seeing(note the row count):
stepping through debug, here's the GridView1_RowDataBound event
0
Richard GouetteIT ManagerAuthor Commented:
Closing , so that I can re-post the last question about the event.
But, you got me going in the right direction I think..
thanks,
Rich
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.