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

Strange Issue with a custom Column: custom CellType appears in incorrect cells

4 Answers 76 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Fraser Tustian
Top achievements
Rank 1
Fraser Tustian asked on 14 Jan 2011, 03:28 AM
Greetings!

I'd love to know if there's a problem with my code or if this is some kind of bug in the GridView, as the result is very strange.

If I create a custom column by inheriting GridViewDataColumn, the cell for that column will appear correctly in its own column, but will also occasionally appear in other columns.

Steps to reproduce
This is not my actual implementation, but a much simplified bit of code that reproduces the problem - the custom column here just displays a button for testing purposes.

I am running Telerik Controls for Windows Forms 2010.3.10.1215

Create a form called "ManualBuildTest" with a RadGridView on it, called "Grid" (Dock: Fill). Then use the following code in the form:

 

 

 

public partial class ManualBuildTest : Form
{
    public ManualBuildTest()
    {
        InitializeComponent();
    }
    private void ManualBuildTest_Load(object sender, EventArgs e)
    {
        var col1 = new DataLinkColumn(string.Empty);
        col1.AllowGroup = true;
        col1.HeaderText = "Requested By";
        Grid.Columns.Add(col1);
        var col = new GridViewTextBoxColumn();
        col.AllowGroup = true;
        col.ReadOnly = false;
        col.HeaderText = "Position";
        Grid.Columns.Add(col);
        col = new GridViewTextBoxColumn();
        col.AllowGroup = true;
        col.ReadOnly = false;
        col.HeaderText = "Organisation";
        Grid.Columns.Add(col);
        col = new GridViewTextBoxColumn();
        col.AllowGroup = false;
        col.ReadOnly = false;
        col.HeaderText = "Work Phone";
        Grid.Columns.Add(col);
        col = new GridViewTextBoxColumn();
        col.AllowGroup = false;
        col.ReadOnly = false;
        col.HeaderText = "Mobile";
        Grid.Columns.Add(col);
        col = new GridViewTextBoxColumn();
        col.AllowGroup = false;
        col.ReadOnly = false;
        col.HeaderText = "E-Mail";
        Grid.Columns.Add(col);
        col = new GridViewTextBoxColumn();
        col.AllowGroup = false;
        col.ReadOnly = false;
        col.HeaderText = "Fax";
        Grid.Columns.Add(col);
        var dcol = new GridViewDateTimeColumn();
        dcol.AllowGroup = true;
        dcol.ReadOnly = false;
        dcol.HeaderText = "Date Requested";
        Grid.Columns.Add(dcol);
        col = new GridViewTextBoxColumn();
        col.AllowGroup = true;
        col.ReadOnly = false;
        col.HeaderText = "Request";
        Grid.Columns.Add(col);
        col = new GridViewTextBoxColumn();
        col.AllowGroup = true;
        col.ReadOnly = false;
        col.HeaderText = "Status";
        Grid.Columns.Add(col);
        col = new GridViewTextBoxColumn();
        col.AllowGroup = true;
        col.ReadOnly = false;
        col.HeaderText = "Assigned To";
        Grid.Columns.Add(col);
        dcol = new GridViewDateTimeColumn();
        dcol.AllowGroup = true;
        dcol.ReadOnly = false;
        dcol.HeaderText = "Request Completed";
        Grid.Columns.Add(dcol);
        col = new GridViewTextBoxColumn();
        col.AllowGroup = true;
        col.ReadOnly = false;
        col.HeaderText = "Result of Request";
        Grid.Columns.Add(col);
        Grid.Rows.AddNew();
        Grid.Rows.AddNew();
    }
}
public class DataLinkColumn : GridViewDataColumn
{
    public DataLinkColumn(string fieldName)
        : base(fieldName)
    {
    }
    public override Type GetCellType(GridViewRowInfo row)
    {
        if (row is GridViewDataRowInfo)
            return typeof(DataLinkCellElement);
        return base.GetCellType(row);
    }
}
public class DataLinkCellElement : GridDataCellElement
{
    private RadButtonElement _ctlButton;
    public DataLinkCellElement(GridViewColumn column, GridRowElement row)
        : base(column, row)
    {
    }
    protected override void CreateChildElements()
    {
        base.CreateChildElements();
        _ctlButton = new RadButtonElement();
        _ctlButton.Text = "TEST";
        _ctlButton.ShowBorder = true;
        Children.Add(_ctlButton);
    }
    protected override void SetContentCore(object value)
    {
        _ctlButton.Text = value == null ? "(null)" : value.ToString();
    }
}

If you then run the form, everything (usually) looks fine at first: the custom column appears in the first column as set (the cell contains a button with the word "(null)" in it for now).

But if you make sure that the window is small enough that the Grid's horizontal scrollbar appears, and scroll back and forth, the custom button is appearing in other cells sporadically (see the attachment for an example).

I've tried to match my code to existing code examples, and even tried it with the RadRadioButtonCellElement custom column from the Knowledge Base, and got the same result.

Is there something fundamentally wrong with my code?

FRASER TUSTIAN

 

4 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 14 Jan 2011, 08:34 AM
Hello Fraser,

There are 2 ways of handling this, the easiest would be to just override IsCompatible and say use this cell only if the column is that type, like so:
public override bool IsCompatible(GridViewColumn data, object context)
{
    return data is DataLinkColumn;
}

But if this is all that you need, i would suggest using a GridViewCommandColumn, please take a look at the documentation here.

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

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Jack
Telerik team
answered on 18 Jan 2011, 09:59 AM
Hi Fraser Tustian,

Thank you for contacting us. Emanuel suggested a proper solution, could you please confirm that it suits your needs? If there is something else that we can assist with, please do not hesitate to contact us

@Emanuel, thank you for your suggestion.

Kind regards,
Jack
the Telerik team
Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
0
Fraser Tustian
Top achievements
Rank 1
answered on 18 Jan 2011, 10:32 AM
Hey!

Worked a treat - thanks all!

My only question is... the RadioButtonColumn in the knowledgebase (http://www.telerik.com/support/kb/winforms/gridview/creating-a-radradiobuttoncellelement.aspx) doesn't override IsCompatible. Is that an oversight, is it a version difference or is there a reason why it's deliberately missing (I was using it as a template).

Thanks again,

FRASER
0
Jack
Telerik team
answered on 20 Jan 2011, 02:22 PM
Hi Fraser,

I am glad to hear that we could help. The RadioButtonCellElement from the KB article implements the IsCompatible method, however, this is not shown in the text. Please check the full code in the attached files. I agree that this can lead to some confusion and we will improve the article.

In case you have any other questions, please do not hesitate to write us back.

Greetings,
Jack
the Telerik team
Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
Tags
GridView
Asked by
Fraser Tustian
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Jack
Telerik team
Fraser Tustian
Top achievements
Rank 1
Share this question
or