473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Making a reference to an object

132 100+
Hello guys,

In a project I'm building I have 4 datagrids which I'm using and where I'm updating information manually, so not from a database or a list.

Now to make it a bit easier I would like to use one generic sort of variable to prevent having to write the same code 4 times.

So if I have 4 datagridviews DataGrid1 through DataGrid4 I would like to be able to assign one of those Datagridview to a general object that has all of the properties and methods in it.

I was thinking of using something like this:
Expand|Select|Wrap|Line Numbers
  1. object DataGrid = DataGrid1
  2.  
  3. DataGrid.Rows.Add("INFO");
  4.  
But that doesn't work.
Any idea on how to do this?

Cheers,
Cainnech
Mar 19 '14 #1
1 1286
I think what you want is data binding using objects. (Note this code is using DataGridView for the DataGrids.)

DGData.cs (Data Model Object):
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.ComponentModel;
  3.  
  4. namespace DataGridTest
  5. {
  6.    class DGData
  7.    {
  8.       private string _name;
  9.       public DGData(string name) {
  10.          _name = name;
  11.       }
  12.  
  13.       public static implicit operator DGData(string name) {
  14.          return new DGData(name);
  15.       }
  16.  
  17.       [DisplayName("My Column Name")]
  18.       public string Name {
  19.          get { return _name; }
  20.          set { _name = value; }
  21.       }
  22.    }
  23. }
A form with two grids:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.ComponentModel;
  3. using System.Data;
  4. using System.Windows.Forms;
  5.  
  6. namespace DataGridTest
  7. {
  8.    public partial class Form1 : Form
  9.    {
  10.       public Form1() {
  11.          InitializeComponent();
  12.       }
  13.  
  14.       private void Form1_Load(object sender, EventArgs e) {
  15.          InitList(_dataGridView1);
  16.          InitList(_dataGridView2);
  17.       }
  18.  
  19.       static DGData[] _staticList = new DGData[] { "One", "Two", "Three" };
  20.  
  21.       static void InitList(DataGridView dgv) {
  22.          // The data source can be modified in this case
  23.          dgv.DataSource = new List<DGData>(_staticList);
  24.       }
  25.  
  26.       static void InitList2(DataGridView dgv) {
  27.          // The data source is fixed length
  28.          dgv.DataSource = _staticList;
  29.       }
  30.    }
  31. }
Mar 19 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
3
by: Pablo Gomes Ludermir | last post by:
Hello, I have the following case that I am trying to put in XML Schema. I have the classes HelpItem, Document and Message that work as follows: One HelpItem contains several Document and...
10
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
2
by: John Bowman | last post by:
Hi, I need to sign a specific node in an XML file that contains analytical data & has a structure something like the following: <DOCUMENT> <RESULT_SET> <TESTVAL> </TESTVAL> ...
0
by: Bala | last post by:
I have an ActiveX EXE. One of the methods in this component has "Recordset Object" as input parameter which has to be passed by reference. I have to call this method from C# code. From C#,...
3
by: Roger Maynard | last post by:
9
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
1
by: Matt Sollars | last post by:
Hi! Does anyone know how to reference the object created via an object initializer when setting a child property? It's hard to write into a sentence. Suppose the following summary class for a...
3
by: Hugh Oxford | last post by:
I want to build a string to reference an object. I can reference is manually thus: print_r($this->struct->parts->parts); but if I build a string... $string = "->parts->parts"
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.