Representing multiple values in a cell

3 posts, 1 answers
  1. neebs
    neebs avatar
    106 posts
    Member since:
    Apr 2007

    Posted 26 Oct 2010 Link to this post

    Hello,

    I have been struggling for a few days on this and have met with no success. I'm hoping someone can send me in the right direction. I have a grid that represents product prices by user type. User Type across the top, SKU down the left side. I need to show the price for each intersection, and as well, indicate whether that user type has access to the item. In addition, I need to be able to edit both values. 

    So in a single cell, I need a way to represent a decimal and a boolean. At first I tried the examples for loading multiple controls into a cell, but that didn't work for a number of reasons, most importantly, it would have been cumbersome to navigate and interact. So my next attempt was to have the cell display the price in two colors: Gray implies that the user has no access, blue implies that the user has access. I could then allow the operator to tab through the cells, type in prices and hit Ctrl + A to toggle the active flag. 

    I started with an override of GridDataCellElement that takes a string, with the pipe character separating the two values: "true|150.00", and stores the result internally in a string and a bool. This is being done in the SetContentCore override. Display works great, but when you edit a cell, you are presented with the composite field - true|150.00 - rather than just the price field.

    I can supply code if necessary, but if someone can recommend the best way to do something like this, or better yet post an example, that would be great.

    Thanks, Steve
  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 27 Oct 2010 Link to this post

    Hello Steve,

    You just have to add custom elements inside the cell when it is in edit mode, like this KB Article explains. But if that is your only problem that you just want to hide the price | from the cell while it is editing, you can also do the following:
    Register for the CellBeginEdit and CellEndEdit events, on CellBeginEdit, remove the price info from the cell value, and set it on the tag, the user will be able to edit just the name.
    After that, on cell end edit you can take the price from the Tag and add it to the cell's value.

    void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
    {
        if (radGridView1.CurrentCell.ColumnInfo.Name == "Name")
        {
            var cellStrings = radGridView1.CurrentCell.Value.ToString().Split('|');
            var priceInfo = cellStrings[0].Trim();
            var textInfo = cellStrings[1].Trim();
     
            radGridView1.CurrentCell.Tag = priceInfo;
            radGridView1.CurrentCell.Value = textInfo;
        }
    }
     
    void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
    {
        if (radGridView1.CurrentCell.ColumnInfo.Name == "Name")
        {
            var price = radGridView1.CurrentCell.Tag as string;
            if (price != null)
            {
                radGridView1.CurrentCell.Value = price + " | " + radGridView1.CurrentCell.Value;
            }
     
            radGridView1.CurrentCell.Tag = null;
        }
    }
    This is the easy approach, there is also another approach, a more complex one, if you want to try that one, please let me know and i will try to prepare an example by tonight.

    If you need any help please let me know.

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. neebs
    neebs avatar
    106 posts
    Member since:
    Apr 2007

    Posted 29 Oct 2010 Link to this post

    Thanks, that solution works!

    Steve
Back to Top