Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > in a DataGridView can each row bind to a different data source ?
 

in a DataGridView can each row bind to a different data source ?

hi developers
in a DataGridView can each row bind to a different custom data source (BindingList ) ?

Thanks in advance
alladeen88  Thursday, September 10, 2009 3:00 AM

hi Aland Li
I am thinking to making polymorphism (( polymorphism based interfaces or polymorphism based inheritance)) Lke that

 // This is the Base Class , It may be an Interface 
   public abstract  class BaseClass
    {
       public abstract int A
       {
           get;
           set;
       }
       public abstract int B
       {
           get;
           set;
       }
       public abstract int C
       {
           get;
           set;
       }
       public abstract int CalcValue();
    }
  
    
      // this is derived class 
    public  class Class1:BaseClass 
    {
       private int a;
       private int b;
       private int c;
       public override int A
       {
           get
           {
               return a;
           }
           set
           {
               a = value;
           }
       }
       public override int B
       {
           get
           {
               return b;
           }
           set
           {
               b = value;
           }
       }
       public override int C
       {
           get
           {
               return c;
           }
           set
           {
               c = value;
           }
       }
       public override int CalcValue()
       {
           // add implementation1 
           
       }

    }


// this is another derived class 
public   class Class2:BaseClass 
    {
        private int a;
        private int b;
        private int c;
        public override int A
        {
            get
            {
                return a;
            }
            set
            {
                a = value;
            }
        }
        public override int B
        {
            get
            {
                return b;
            }
            set
            {
                b = value;
            }
        }
        public override int C
        {
            get
            {
                return c;
            }
            set
            {
                c = value;
            }
        }
        public override int CalcValue()
        {
            // add implementation2 
            
        }

    }

// this is another derived class 
public  class Class3:BaseClass 
    {
        private int a;
        private int b;
        private int c;
        public override int A
        {
            get
            {
                return a;
            }
            set
            {
                a = value;
            }
        }
        public override int B
        {
            get
            {
                return b;
            }
            set
            {
                b = value;
            }
        }
        public override int C
        {
            get
            {
                return c;
            }
            set
            {
                c = value;
            }
        }
        public override int CalcValue()
        {
            // add implementation3 
            
        }

    }

// Note here the type of the collection ((Baseclass))
public   class MyBindingList:BindingList <BaseClass >
    {

    }



// In the Main Form 
 public partial class Form1 : Form
    {
        private MyBindingList _MyBindingListObj ;
        public MyBindingList MyBindingListObj
        {
            get { return _MyBindingListObj; }
            set { _MyBindingListObj = value; }
        }
        public Form1()
        {
            InitializeComponent();
            MyBindingListObj = new MyBindingList();
            dataGridView1.DataSource = MyBindingListObj;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Add object of type Calss1 to a DataGridView 
            BaseClass obj1 = new Class1();
            MyBindingListObj.Add(obj1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Add object of type class2 to a DataGridView 
            BaseClass obj2 = new Class2();
            MyBindingListObj.Add(obj2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Add object of type class3 to a DataGridView 
            BaseClass obj3 = new Class3();
            MyBindingListObj.Add(obj3);
        }
       I will be happy ,if you tell me your opinion 
        
           Alladeen
   
  • Marked As Answer byalladeen88 Wednesday, September 23, 2009 7:56 AM
  •  
alladeen88  Thursday, September 17, 2009 4:51 AM
Hi alladeen88,

As far as I know, there is no such binding. We need to make the DataGridView unbound and load data manually.

Let me know if this does not help.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, September 11, 2009 5:37 AM

Hi,

We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, September 17, 2009 2:37 AM

hi Aland Li
I am thinking to making polymorphism (( polymorphism based interfaces or polymorphism based inheritance)) Lke that

 // This is the Base Class , It may be an Interface 
   public abstract  class BaseClass
    {
       public abstract int A
       {
           get;
           set;
       }
       public abstract int B
       {
           get;
           set;
       }
       public abstract int C
       {
           get;
           set;
       }
       public abstract int CalcValue();
    }
  
    
      // this is derived class 
    public  class Class1:BaseClass 
    {
       private int a;
       private int b;
       private int c;
       public override int A
       {
           get
           {
               return a;
           }
           set
           {
               a = value;
           }
       }
       public override int B
       {
           get
           {
               return b;
           }
           set
           {
               b = value;
           }
       }
       public override int C
       {
           get
           {
               return c;
           }
           set
           {
               c = value;
           }
       }
       public override int CalcValue()
       {
           // add implementation1 
           
       }

    }


// this is another derived class 
public   class Class2:BaseClass 
    {
        private int a;
        private int b;
        private int c;
        public override int A
        {
            get
            {
                return a;
            }
            set
            {
                a = value;
            }
        }
        public override int B
        {
            get
            {
                return b;
            }
            set
            {
                b = value;
            }
        }
        public override int C
        {
            get
            {
                return c;
            }
            set
            {
                c = value;
            }
        }
        public override int CalcValue()
        {
            // add implementation2 
            
        }

    }

// this is another derived class 
public  class Class3:BaseClass 
    {
        private int a;
        private int b;
        private int c;
        public override int A
        {
            get
            {
                return a;
            }
            set
            {
                a = value;
            }
        }
        public override int B
        {
            get
            {
                return b;
            }
            set
            {
                b = value;
            }
        }
        public override int C
        {
            get
            {
                return c;
            }
            set
            {
                c = value;
            }
        }
        public override int CalcValue()
        {
            // add implementation3 
            
        }

    }

// Note here the type of the collection ((Baseclass))
public   class MyBindingList:BindingList <BaseClass >
    {

    }



// In the Main Form 
 public partial class Form1 : Form
    {
        private MyBindingList _MyBindingListObj ;
        public MyBindingList MyBindingListObj
        {
            get { return _MyBindingListObj; }
            set { _MyBindingListObj = value; }
        }
        public Form1()
        {
            InitializeComponent();
            MyBindingListObj = new MyBindingList();
            dataGridView1.DataSource = MyBindingListObj;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Add object of type Calss1 to a DataGridView 
            BaseClass obj1 = new Class1();
            MyBindingListObj.Add(obj1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Add object of type class2 to a DataGridView 
            BaseClass obj2 = new Class2();
            MyBindingListObj.Add(obj2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Add object of type class3 to a DataGridView 
            BaseClass obj3 = new Class3();
            MyBindingListObj.Add(obj3);
        }
       I will be happy ,if you tell me your opinion 
        
           Alladeen
   
  • Marked As Answer byalladeen88 Wednesday, September 23, 2009 7:56 AM
  •  
alladeen88  Thursday, September 17, 2009 4:51 AM
Hi alladeen88,

Thanks for your answer.

Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 23, 2009 10:27 AM

You can use google to search for other answers

Custom Search

More Threads

• Enabling/Disabling Save button on BindingNavigator
• Dataset findby<datarow>
• What is the procedure to add combobox in the data grid column?
• The located assembly's manifest definition does not match the assembly reference.
• Highlighted rows in DataGridView
• Linking a combobox to a remote database
• Storing a computed column back to the database
• Datagrid dropdown selection
• Mostrar en TextBox los Datos de una Fila de un DatagridView
• Problem with datagrid ... pls help