2

I'm attempting to create a custom control from dataGridView which includes a checkboxcolumn. I have added the code to produce this and added the newly created checkBoxDataGridView onto my form in a new project.

I have 2 problems:

1. The column gets added a 2nd time when I open and close the form that holds the new checkBoxDataGridView in designtime. You will see in my code I have tried multiple checks, now commented out, so it does not add the column during design time a 2nd time or more than once. Also only needs to add it during designtime, which I guess is no problem if I check whether its been added already.

2. The checkbox column does not instantiate the name I gave it in the controls class. eg.: col.Name = "dgvTickCol"

    public class CheckBoxDataGridView : DataGridView
    {          
        public CheckBoxDataGridView()
        {          
            AddCheckColumn();
        }

        private void AddCheckColumn()
        {
            //if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)                
            //if (!Columns.Contains("dgvTickCol"))                
            //if (this.Rows[0].Cells["dgvTickCol"].Value != null)
            //if (this.Rows[0].Cells["dgvTickCol"].ColumnIndex == -1)
            //{
                DataGridViewColumn col = new DataGridViewCheckBoxColumn();            
                col.Name = "dgvTickCol";
                col.HeaderText = "";
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
                Columns.Add(col);            
            // }

    }
}
Farrel
  • 185
  • 1
  • 10
  • I have had a similar problem. Are you implementing the custom control as referenced dll, or as a new item on your working project? – Adnand Jun 08 '17 at 14:49
  • I have it in separate project(assuming DLL) with a few custom controls and user controls in it, which I use in different desktop applications to speed up coding. Any of those controls I add off the toolbox on the left of the screen onto a form. – Farrel Jun 08 '17 at 15:01
  • Someone recommended clearing the container before adding the column. Not sure how or where. Still investigating. Will let you know if it works. – Farrel Jun 08 '17 at 15:04
  • I [my similar question](https://stackoverflow.com/questions/43999135/my-custom-control-act-differently-when-i-implement-it-as-dll) about this problem, someone suggested me [this article](https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/creating-a-wf-control-design-time-features) to read about it, but I didn't solvet. I noticed that on designer of the user control/windows form which I was implementing, the result wasn't as I expected, you can have a look on yours. – Adnand Jun 08 '17 at 15:22
  • Why would you need to create a “Custom” `DataGridView` to simply add a `DataGridViewCheckBoxColumn`??? Simply create a new `DataGridView`, add a `DataGridViewCheckBoxColumn` and that is all you need to do… there is no need for this “Custom` “Class” as you have it currently posted. What is this “Custom” `DataGridView` supposed to do that a regular `DataGridView` can’t already do? – JohnG Jun 08 '17 at 21:39
  • I've only added a small bit of the code here of a larger component/control I'm trying to create. Bottom line in doing this is to make the grid reusable with pre-loaded settings. Also in the name of learning how to create C# .NET controls. I come from a DElphi background and have rewritten almost every component on the VCL in some or other way in the past, so is interesting to understand how .NET works in this way. – Farrel Jun 09 '17 at 07:33
  • The problem with the check box column showing up twice is because the way you are creating the control, it does NOT appear to be a “true” “Control” component. It appears to be a simple class in your code. When done this way… is what will happen is that when you place the control from the tool box onto the form, it will also place the controls constructor code into the designer. – JohnG Jun 09 '17 at 22:44
  • You can check this yourself, drop your control onto the form and look at the designer code; it will also add this check box column. I made a separate “Control” and added your code to the control and this duplicated column does not happen, or if you place the control into the form without using the designer (place the control onto the form through code), then everything will work as expected. You may have to add this code to your existing “larger” component/control. – JohnG Jun 09 '17 at 22:44
  • @JohnG - Thank you, partially understand what you describing. Do you have an example of the code you are explaining where you add my code to your control? Are you inheriting your class from a UserControl and then instantiating the Datagrid view? Is that not then just a User Control with the datagridview on it or are you in fact extending the datagridview class like I'm trying doing? – Farrel Jun 11 '17 at 08:56
  • Extending the `DataGridView` as the code is, is not a problem programmatically. Example, as I mentioned earlier, if you programmatically add the `CheckBoxDataGridView`. to the form load event, there will not be duplicate column problems. Therefore, technically the code is correct and will work as expected if you use in programmatically. – JohnG Jun 11 '17 at 12:52
  • If the `CheckBoxDataGridView` “Control” is available in VS “ToolBox” and you drag the control onto a form… this is where the “Designer” is possibly adding some extra code for the control. This is coming from the “Designer” when you drag the control onto the form. – JohnG Jun 11 '17 at 12:52
  • Therefore, I am not sure how you would do this without creating a “User Control” instead of a `DataGridView`. As I mentioned, this will work properly if the grid is in a “User Control”. – JohnG Jun 11 '17 at 12:52
  • I think what we can take from this is that the IDE adds code and interprets the designer class. I will give the on load event a chance. But what I'm thinking is that if the column only exists during the on load, I wonder if it can be referenced in other bits of code. Perhaps have to create the checkbox column object, manipulate it and then add it only on the on load event. Will revert back with findings. Thanks – Farrel Jun 12 '17 at 10:11

0 Answers0