Column name of datgridview in property

Pankaj tripathi 185 Reputation points
2023-12-17T11:13:32.2333333+00:00

Hi ,

i had created a property to store , fetch and display some names in a property named as SelectedColumns . Now the problem is i am getting a warning and an error .

the warning is

User's image

and the warning is
User's image

the Full code is

   public partial class CustomDatagridview : DataGridView
    {     

        private List<string> selectedColumns = new List<string>();

        [Editor(typeof(ColumnSelectionEditor), typeof(UITypeEditor))]
        [TypeConverter(typeof(ColumnsConverter))]
        public List<string> SelectedColumns
        {
            get { return selectedColumns; }
            set { selectedColumns = value; }
        }


        public CustomDatagridview()
        {
}

}


   public class ColumnsConverter : TypeConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            // Add column names WHICH YOU WANT TO SKIP here
            List<string> columnNames = new List<string> { "Column1", "Column2", "Column3" };

            return new StandardValuesCollection(columnNames);
        }
    }

    public class ColumnSelectionEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService =
                provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;

            if (editorService != null)
            {
                ListBox listBox = new ListBox();
                listBox.SelectionMode = SelectionMode.MultiSimple;

                if (context.Instance is CustomDatagridview customDataGridView)
                {
                    // Add available column names
                    List<string> columnNames = new List<string> { "Column1", "Column2", "Column3" };

                    foreach (string columnName in columnNames)
                    {
                        listBox.Items.Add(columnName);
                    }

                    // Select the previously selected columns
                    foreach (string selectedColumn in customDataGridView.SelectedColumns)
                    {
                        int index = listBox.Items.IndexOf(selectedColumn);
                        if (index != -1)
                        {
                            listBox.SetSelected(index, true);
                        }
                    }
                }
                editorService.DropDownControl(listBox);

                // Update the selected columns when the user makes a selection
                if (context.Instance is CustomDatagridview customDataGridView)
                {
                    customDataGridView.SelectedColumns.Clear();
                    foreach (var selectedItem in listBox.SelectedItems)
                    {
                        customDataGridView.SelectedColumns.Add(selectedItem.ToString());
                    }
                }
            }

            return value;
        }
    }
}
Developer technologies | Windows Forms
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2023-12-18T05:42:42.71+00:00

    Hi @Pankaj tripathi , Welcome to Microsoft Q&A,

    The first warning (warning) is a prompt of naming conflict or hidden members in the programming language of C#(CSHARP). Specifically, this warning is to say that there is a class in your code (probably CustomdatagridView) defines a member called selectdColumns, but this member and the inherited base class inherited by this category (Maybe it is datagridView) The members of the same name have a conflict.

    You can choose to use adding NEW keywords to clearly indicate that you intentionally hide members in the base class, or choose to modify it to other non -heavy names(Selected_Columns).

    public new List<string> SelectedColumns
    {
        get { return selectedColumns; }
        set { selectedColumns = value; }
    }
    

    The second error is also the usage of (if ... is ... x), which is temporarily named X. Two if (Context.Instance is CustomdatagridView CustomdatagridView) in the same scope, that is, in the same code block, you have defined a local variable called CustomDataGridView. In C#, the role of variables is usually defined by parentheses {}.

    To solve this problem, you can consider changing the variable names in the first IF statement to different names to avoid conflicting with variable names in the second IF statement block. You can't define it to it.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.