Monday, May 26, 2014

ASP.NET Gridview sorting that bound to a List<> of custom objects as DataSource

In AsP.NET, Gridview with List<> of custom objects as DataSource does not support sorting. Here is a code to achieve it. Here the sort direction and Sort column is stored in a ViewState. So when we click a column name to sort it will check the previous sort direction from the ViewState["SortDirection"] and ViewState["SortColumn"]. If the previous direction is ASC means it will do DESC sort vice versa. I have used LINQ to sort the List. The sorting is done inside the GridView Rowcommand event with the help of CommandName and CommandArgument as parameter. And also you have to change the Gridview header from Label to LinkButton in the Header Template as it will make postbacks. Check out the code you can learn some new C# techniques as i have learned when i tried to implement this functionalities. Design Code:

<asp:GridView ID="grdManageUsers" runat="server" AutoGenerateColumns="False" OnRowCommand="grdManageUsers_RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="First Name">
            <HeaderTemplate>
                <asp:LinkButton ID="lnkFirstname" runat="server" CommandName="Sort" CommandArgument="Firstname">First Name</asp:LinkButton>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblFirstNameEdit" runat="server" Text='<%# Eval("Firstname")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtFirstname" runat="Server" Text='<%# Eval("Firstname") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Last Name">
            <HeaderTemplate>
                <asp:LinkButton ID="lnklastname" runat="server" CommandName="Sort" CommandArgument="Lastname">Last Name</asp:LinkButton>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblLastnameEdit" runat="server" Text='<%# Eval("Lastname")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtLastname" runat="Server" Text='<%# Eval("Lastname") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Email">
            <HeaderTemplate>
                <asp:LinkButton ID="lnkEmail" runat="server" CommandName="Sort" CommandArgument="Email">Email</asp:LinkButton>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblEmailEdit" runat="server" Text='<%# Eval("Email")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEmail" runat="Server" Text='<%# Eval("Email") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        </Columns>
    </asp:GridView>

Code behind C#:

List userList = new List();

private SortDirection LastSortDirection
{
    get
    {
        return (ViewState["LastSortDirection"] == null) ? SortDirection.Descending : (SortDirection)ViewState["LastSortDirection"];
    }
    set
    {
        ViewState["LastSortDirection"] = value;
    }
}

private string LastSortColumn
{
    get
    {
        return (ViewState["LastSortColumn"] == null) ? "Firstname" : (string)ViewState["LastSortColumn"];
    }
    set
    {
        ViewState["LastSortColumn"] = value;
    }
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        try
        {
           userList = GetUsers(); // It will return the List of users from data source.
           ViewState["USERLIST"] = userList; // It can be used for sorting
        }
        catch (Exception ex)
        {
           
        }
    }
}
protected void grdManageUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName == "Sort" && !string.IsNullOrEmpty(e.CommandArgument.ToString()))
        {
            userList = ViewState["USERLIST"] as List;
            if (userList != null && userList.Count > 0)
            {
                IQueryable query = userList.AsQueryable();
                string newSortColumn = string.Empty;
                SortDirection newSortDirection = SortDirection.Descending;
                BindGridView(Sort(query, e.CommandArgument.ToString(), LastSortColumn, LastSortDirection, out newSortColumn, out newSortDirection));
                LastSortColumn = newSortColumn;
                LastSortDirection = newSortDirection;
            }
        }
    }
    catch (Exception ex)
    {      
    }
}
private void BindGridView(List users)
{
     grdManageUsers.DataSource = users;
     grdManageUsers.DataBind();
}
public List Sort(IQueryable list, string sortColumn, string lastSortColumn, SortDirection lastSortDirection, out string newSortColumn, out SortDirection newSortDirection)
{
    newSortColumn = string.Empty;
    newSortDirection = SortDirection.Descending;
    var result = new List();
    try
    {
        if (sortColumn == lastSortColumn)
        {
            newSortColumn = sortColumn;
            if (lastSortDirection == SortDirection.Ascending)
            {
                newSortDirection = SortDirection.Descending;
                result = SortProcess(list, SortDirection.Descending, sortColumn);
            }
            else
            {
                newSortDirection = SortDirection.Ascending;
                result = SortProcess(list, SortDirection.Ascending, sortColumn);
            }
        }
        else
        {
            newSortColumn = sortColumn;
            newSortDirection = SortDirection.Ascending;
            result = SortProcess(list, SortDirection.Ascending, sortColumn);
        }
    }
    catch
    {
        throw;
    }
    return result;
}
public List SortProcess(IQueryable list, SortDirection sortDirection, string sortColumn)
{
    IQueryable query = list.AsQueryable();
    try
    {
        var pi = typeof(T).GetProperty(sortColumn);

        if (sortDirection == SortDirection.Ascending)
        {
            query = query.OrderBy(x => pi.GetValue(x, null));
        }
        else
        {
            query = query.OrderByDescending(x => pi.GetValue(x, null));
        }
    }
    catch
    {
        throw;
    }
    return query.ToList();
}

Thursday, May 22, 2014

Project Euler Solution using C#: Problem 22: Names Scores

Problem:

Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?
My Solution:

static void Main(string[] args)
{
    StreamReader sr = new StreamReader("names.txt");
    string textNames = sr.ReadToEnd().Replace("/", "").Replace("\"", "");
    char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
    long result = 0;
    string[] arr = textNames.Split(',');
    Array.Sort(arr);
    for (int i = 1; i <= arr.Length; i++)
    {
        int score = 0;
        foreach (var eachChar in arr[i - 1].ToCharArray())
        {
            score += (Array.IndexOf(alpha, eachChar) + 1);
        }
        score *= i;
        result += score;
    }
    Console.WriteLine(result);
    Console.ReadLine();
}

Note: You can simplifies the coding :)

Project Euler Solution using C#: Problem 20: Factorial Digit Sum

Problem:

n! means n × (n − 1) × ... × 3 × 2 × 1 For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. Find the sum of the digits in the number 100! My Solution:

static void Main(string[] args)
{
    BigInteger multi = 1;
    long result = 0;
    int digits = 100;
    for (int i = digits; i >= 1; i--)
    {
        multi *= i;
    }
    foreach (var item in multi.ToString().ToCharArray())
    {
        result += long.Parse(item.ToString());
    }
    Console.WriteLine(result);
    Console.ReadLine();
}

Note: You can simplifies the coding :)