Click here to Skip to main content
12,634,107 members (23,347 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET LINQ Gridview
Using LINQ I created the following query:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Results_Standings : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataClasses2DataContext db = new DataClasses2DataContext();

        var Standings = from p in db.Pit_Outputs
                         where p.Season  == "2011"
                         group p by p.Team_ID into g
                         orderby g.Sum(w => w.W) descending
                         
                         select new 
                         {
                             Team = g.First().Teams.Team_City,
                             W = g.Sum(w => w.W),
                             L = g.Sum(l=> l.L),
                             P = g.Sum(w => w.W) / (g.Sum(w => w.W) + g.Sum(l => l.L)),
                         };
        GridView1.DataSource = Standings;
        
        GridView1.DataBind();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
}

In my gridview I want to align the text fields left and the value fields right. Here is my current gridview code:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
        AllowSorting="True" CellPadding="4" EnableSortingAndPagingCallbacks="True"
        ForeColor="#333333" GridLines="None" PageSize="30"
        HorizontalAlign="Center" style="text-align: right"
        onselectedindexchanged="GridView1_SelectedIndexChanged1">
        <AlternatingRowStyle BackColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
Posted 1-Jan-13 5:23am
MrSME286
Updated 1-Jan-13 11:53am
v3
Comments
Sheikh Muhammad Haris 1-Jan-13 13:23pm
   
What do you mean by Text Fields and Value Fields ? You haven't mention the fields to bind with your GridView
MrSME 1-Jan-13 17:04pm
   
My LINQ to SQL query is bound to the gridview. Team is a text column and the other fields are numeric. Thanks.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Your select code is obviously irrelevant. Where your data comes from does not matter. You seem to be letting the gridview just display everything it's given automatically. You will need to write a template that lists the elements to display, if you want to take control to the point of setting alignment on individual cells.
  Permalink  
Comments
MrSME 1-Jan-13 17:01pm
   
I am very new to ASP.net so not much is obvious to me. Is writing a template something that I do instead of using a gridview or something that I do with my gridview. Do you have any recommended links to learn how to do this or should I search this sight? Thanks.
Christian Graus 1-Jan-13 17:14pm
   
If you read the MSDN documentation and this site, you should find the info you need. You should not be trying to do all of this without having a clue on these most basic things, however.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100