简体   繁体   中英

Changing type of column in UltraWinGrid

I have an ultrawingrid with 2 columns. The requirement is to open up a context menu with an option "Copy", when any cell is right-clicked. The user can then select "Copy", which should then copy the cell contents.

I already have a class "ExtendedLabel" which extends the class "Label". This label has the same functionality as above; difference is the right-click happens on a label.

The problem is; how can I integrate this ExtendedLabel as the column data type of the ultrawingrid? This is what I tried:

public class Content
{
    public Content()
    {
        Item = new ExtendedLabel();
        Value = new ExtendedLabel();
    }

    ExtendedLabel Item = new ExtendedLabel();
    ExtendedLabel Value = new ExtendedLabel();
}

Content a = new Content();
a.Item.Text = "Item1";   // The ExtendedLabel has a property called "Text"
a.Value.Text = "Value1";

Content b = new Content();
a.Item.Text = "Item2";
a.Value.Text = "Value2";

List<Content> contents = new List<Content>();
contents.Add(a);
contents.Add(b);

ultrawingrid.DataSource = contents;

Now the grid is like this.

Item                            Value
-------------------------------------------------------------
{ExtendedLabel, Text: Item1}    {ExtendedLabel, Text: Value1}
{ExtendedLabel, Text: Item2}    {ExtendedLabel, Text: Value2}

whereas, what I wanted was:

Item     Value
---------------
Item1    Value1
Item1    Value1

In addition to all this, Am I doing this correctly? Is this the best approach here? If not, then how?

TIA!

Not tested, but I think that the grid binds to the public members of you class.
If this is viable, change the visibility of your internal labels to private and expose Item and Value as the text of the two internal labels.

public class Content 
{ 
    public Content() 
    { 
        Item = new ExtendedLabel(); 
        Value = new ExtendedLabel(); 
    } 

    private ExtendedLabel internal_Item = new ExtendedLabel(); 
    private ExtendedLabel internal_Value = new ExtendedLabel(); 

    public string Item
    { 
        get{return internal_Item.Text;}
        set{internal_Item.Text = value;}

    }
    public string Value
    { 
        get{return internal_Value.Text;}
        set{internal_Value.Text = value;}
    }
} 

The best approach would be to bind the grid to data objects that expose the needed properties. For example you could have the following class:

public class DataItem
{
  public string Item {get;set; }
  public string Value {get;set;}
}

Then create a List and bind that to the grid as the grid will create a column for each public property so you will get the values you expect in the grid. Note that if the list can change and the grid needs to update in response to those changes you should use BindingList rather than List.

By default the grid will allow editing the values and you will have copy and paste behavior built into the grid that works with the keyboard. There is an example in the help that shows how you can have an edit menu in your application that will work with the WinGrid.

If the copy and paste functionality of the grid meets your needs you could then expose a context menu for the grid itself that exposes these same options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM