• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 71
  • Last Modified:

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
0
Richard Gouette
Asked:
Richard Gouette
1 Solution
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now