This is a migrated thread and some comments may be shown as answers.

How to autogenerate customColumn?

4 Answers 134 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tajes
Top achievements
Rank 1
Tajes asked on 18 Nov 2010, 05:41 PM
Hello,
     I'm trying to create a custom GridViewMultiComboBoxColumn with additional features. When the grid autogenerate the columns depending the data type, I want that it autogenerates my customColumn if the type of the column's datasource is a related persistent class. Maybe it is not possible, but I want to try it.

Thanks in advance.

4 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 20 Nov 2010, 01:21 AM
Hello Ivan,

First i would like to apologize for the late answer.

Sadly i don't know of a way of changing the type for a column for a DataBoundGrid, but you could define all of your columns, and set the
radGridView1.AutoGenerateColumns = false;

Please take a look at the following example:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
 
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
    }
 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        var column1 = new GridViewDecimalColumn("Id");
        radGridView1.Columns.Add(column1);
 
        var column2 = new GridViewMultiComboBoxColumn("BuyerId");
        column2.DataSource = new BuyersCollection(10, 10);
        column2.ValueMember = "Id";
        radGridView1.Columns.Add(column2);
 
        radGridView1.AutoGenerateColumns = false;
 
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.DataSource = new ProductsCollection(1000);
    }
}
 
#region Helpers
 
public class Product : INotifyPropertyChanged
{
    private int id, buyerId;
 
    public int BuyerId
    {
        get { return buyerId; }
        set
        {
            buyerId = value;
            OnPropertyChanged("BuyerId");
        }
    }
 
    public int Id
    {
        get { return id; }
        set
        {
            id = value;
            OnPropertyChanged("Id");
        }
    }
 
    public Product(int id, int buyerId)
    {
        this.Id = id;
        this.BuyerId = buyerId;
    }
 
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
}
 
public class Buyer
{
    public int Id
    {
        get;
        set;
    }
 
    public string Name
    {
        get;
        set;
    }
 
    public Buyer(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
}
 
public class ProductsCollection : BindingList<Product>
{
    public ProductsCollection(int noItems)
    {
        for (int i = 0; i < noItems; i++)
        {
            this.Add(new Product(i, i + 10));
        }
    }
}
 
public class BuyersCollection : BindingList<Buyer>
{
    public BuyersCollection(int startIndex, int lenght)
    {
        for (int i = 0; i < 10; i++)
        {
            this.Add(new Buyer(i + 10, "Buyer" + (i + 1)));
        }
    }
}
 
#endregion Helpers

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Tajes
Top achievements
Rank 1
answered on 21 Nov 2010, 07:15 PM
Thank you for your reply, Emanuel,
   I already knew this solution, but I was looking for a way to autogenerate the column with my own column class.
   Thank you anyway.

Greetings, Ivan
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 22 Nov 2010, 12:40 AM
Hello again,

Sorry, this is done internally, the best you could do is to use a CustomCellProvider in order to use custom cells for a specific column, but this will not change the type of the column.

If you are interested in using this, please take a look at the following example:
namespace TestCustomCellWProvider
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
 
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
 
        public Form1()
        {
            InitializeComponent();
            this.Size = new Size(500, 400);
            this.Load += new EventHandler(Form1_Load);
        }
 
        void Form1_Load(object sender, EventArgs e)
        {
            radGridView1 = new RadGridView();
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.Dock = DockStyle.Fill;
            this.radGridView1.TableElement.CellElementProvider = new CustomCellProvider(this.radGridView1.TableElement);
 
            this.Controls.Add(radGridView1);
 
            radGridView1.DataSource = new TestsCollection(1000);
        }
    }
 
    #region Helpers
 
    internal class Test
    {
        public int Id { get; set; }
 
        public string OtherColumn { get; set; }
 
        public string CustomColumn { get; set; }
 
        public Test(int id, string otherColumn, string customColumn)
        {
            this.Id = id;
            this.OtherColumn = otherColumn;
            this.CustomColumn = customColumn;
        }
    }
 
    internal class TestsCollection : BindingList<Test>
    {
        public TestsCollection(int noItems)
        {
            for (int i = 0; i < noItems; i++)
            {
                this.Add(new Test(i, "item" + i, i.ToString()));
            }
        }
    }
 
    #endregion Helpers
 
    #region Custom Cell Provider & Custom Cell
 
    public class CustomCellProvider : CellElementProvider
    {
        public CustomCellProvider(GridTableElement tableElement)
            : base(tableElement)
        {
        }
 
        public override IVirtualizedElement<GridViewColumn> CreateElement(GridViewColumn data, object context)
        {
            var dataRow = context as GridDataRowElement;
            if (data.Name == "CustomColumn" && dataRow != null)
            {
                var cell = new CustomCell(data, dataRow);
                return cell;
            }
 
            return base.CreateElement(data, context);
        }
 
        public override bool IsCompatible(
            IVirtualizedElement<GridViewColumn> element, GridViewColumn data, object context)
        {
            if (data.Name == "CustomColumn" &&
                context is GridDataRowElement)
            {
                return element is CustomCell;
            }
 
            if (element is CustomCell)
            {
                return false;
            }
 
            return base.IsCompatible(element, data, context);
        }
    }
 
    public class CustomCell : GridDataCellElement
    {
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(GridDataCellElement);
            }
        }
 
        public RadButtonElement Button1Element
        {
            get;
            set;
        }
 
        public RadButtonElement Button2Element
        {
            get;
            set;
        }
 
        public CustomCell(GridViewColumn column, GridRowElement row)
            : base(column, row)
        {
        }
 
        protected override void CreateChildElements()
        {
            var button1 = new RadButtonElement();
            button1.Text = "Button1";
            button1.Margin = new Padding(0, 2, 0, 0);
            button1.MinSize = new Size(20, 20);
            button1.ImageAlignment = ContentAlignment.MiddleCenter;
            button1.Click += new EventHandler(button1_Click);
            Button1Element = button1;
 
            var button2 = new RadButtonElement();
            button2.Text = "Button2";
            button2.Margin = new Padding(0, 2, 0, 0);
            button2.MinSize = new Size(20, 20);
            button2.ImageAlignment = ContentAlignment.MiddleCenter;
            button2.Click += new EventHandler(button2_Click);
            Button2Element = button2;
 
            this.Children.Add(button1);
            this.Children.Add(button2);
 
            this.CreateTextParams();
            base.CreateChildElements();
        }
 
        void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button2 pressed");
        }
 
        void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button1 pressed");
        }
 
        /// <summary>
        /// Sets the actual text of the cell.
        /// </summary>
        /// <param name="value"></param>
        protected override void SetContentCore(object value)
        {
            if (this.Value != null && this.Value != DBNull.Value)
            {
                if (value.ToString() == "77" || value.ToString() == "1" || value.ToString() == "3")
                {
                    Button1Element.Visibility = Telerik.WinControls.ElementVisibility.Hidden;
                    Button2Element.Text = "Button2:" + value;
                }
                else
                {
                    Button1Element.Visibility = Telerik.WinControls.ElementVisibility.Visible;
                    Button1Element.Text = "Button1:" + value;
                    Button2Element.Text = "Button2:" + value;
                }
            }
        }
 
        protected override SizeF ArrangeOverride(SizeF finalSize)
        {
            if (this.Children.Count == 2)
            {
                this.Children[0].Arrange(
 
                    new RectangleF(
                        0,
 
                        (finalSize.Height / 2) - (this.Children[0].DesiredSize.Height / 2 + 1),
 
                        finalSize.Width / 2 - 1,
 
                        this.Children[0].DesiredSize.Height));
 
                this.Children[1].Arrange(
 
                    new RectangleF(finalSize.Width / 2 + 1, (finalSize.Height / 2) - (this.Children[0].DesiredSize.Height / 2 + 1), finalSize.Width / 2 - 1, this.Children[1].DesiredSize.Height));
            }
 
            this.UpdateInfo();
 
            return finalSize;
        }
    }
 
    #endregion Custom Cell Provider & Custom Cell
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Tajes
Top achievements
Rank 1
answered on 22 Nov 2010, 10:20 AM
Thank you so much Emanuel. This is very helpfull
Tags
GridView
Asked by
Tajes
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Tajes
Top achievements
Rank 1
Share this question
or