|
-
Mar 21st, 2008, 01:32 PM
#1
Thread Starter
Hyperactive Member
Add Properties To DataGridView
Could someone show me how to create 2 new properties for the DataGridView?
1. Add a .Info property to each Rolumn of the DataGridView.
2. Add a .LastName property to each Row of the DataGridView.
So when the Grid's CellChanged even fires, I can do something like this...
TextBox1.Text = DataGridView1.Columns(theCurrentColumn).Info
Is that possible?
-
Mar 21st, 2008, 02:08 PM
#2
Re: Add Properties To DataGridView
You'd have to create two classes that derives from the DataGridViewRow and DataGridViewColumn classes. Properties, and their variables, are created like this:
vb Code:
Private _myVariable As MyType
Public Property ThisIsMyProperty() As MyType
Get
Return _myVariable
End Get
Set(ByVal value As MyType)
_myVariable = value
End Set
End Property
-
Mar 21st, 2008, 03:04 PM
#3
Thread Starter
Hyperactive Member
Re: Add Properties To DataGridView
I understand the workings of Get and Set as they apply to properties of a new Class. What I don't know, is how I'm going to apply this information to creating a new UserControl that looks and acts just like a DataGridView but with my new properties.
-
Mar 21st, 2008, 03:05 PM
#4
Thread Starter
Hyperactive Member
Re: Add Properties To DataGridView
Will I actually have to create a UserControl or will I simply be able to edit the DataGridView class and use Inherits?
I'm confused.
-
Mar 21st, 2008, 04:33 PM
#5
Thread Starter
Hyperactive Member
Re: Add Properties To DataGridView
Actually, I need to be able to add a couple of properties to each cell in the grid too. I gave it a try at home but I didn't get very far.
-
Mar 21st, 2008, 06:05 PM
#6
Thread Starter
Hyperactive Member
Re: Add Properties To DataGridView
In trying to get a new property for each cell and for each column, I've tried the following...
Code:
Public Class myDataGridViewTextBoxCell
Inherits DataGridViewTextBoxCell
Public GradeValue As String
Public Info As String
Public Function PercentCorrect() As Integer
Return 100
End Function
End Class
and
Code:
Public Class myDataGridViewColumn
Inherits DataGridViewColumn
Public AssignmentPage As String = ""
Public DateAssigned As Date
Public DateTurnedIn As Date
Public PerfectScore As Integer = 100
Public Sub New()
Me.CellTemplate = New myDataGridViewTextBoxCell()
Me.ReadOnly = False
End Sub
End Class
I'm expecting my grid to now be able to create a column of type myDataGridViewColumn which will have the extra properties. But I still only have access to the standard DataGridView columns that are normally available. How do I get to this new column type that I've created?
-
Mar 21st, 2008, 08:28 PM
#7
Thread Starter
Hyperactive Member
Re: Add Properties To DataGridView
I got the custom column type to show up now by doing a Build from the menus. But I still don't have access to my new properties.
I tried...
DataGridView1.Columns(1).PerfectScore = 100
But PerfectScore is not a member....
Now what?
-
Mar 23rd, 2008, 08:54 AM
#8
Re: Add Properties To DataGridView
 Originally Posted by DroopyPawn
I got the custom column type to show up now by doing a Build from the menus. But I still don't have access to my new properties.
I tried...
DataGridView1.Columns(1).PerfectScore = 100
But PerfectScore is not a member....
Now what?
Columns is a collection of DataGridViewColumn objects. It has to be able to store all sorts of column types, so each column gets returned as the lowest common denominator: DataGridViewColumn. You have to cast that as your specific type in order to access your specific properties. It's exactly the same as an ArrayList returning each item as an Object reference and your having to cast those items as their specific type to access their specific properties.
-
Mar 23rd, 2008, 12:14 PM
#9
Thread Starter
Hyperactive Member
Re: Add Properties To DataGridView
-
Mar 23rd, 2008, 06:07 PM
#10
Thread Starter
Hyperactive Member
Re: Add Properties To DataGridView
Now I'm a little confused...
In the code below, I add my new column to my DataGridView (named grd) and give the column's AssignmentDescription a value of "1st value"
The form has 2 buttons. Button one verifies that "1st value" is actually in the column's AssignmentDescription with a messagebox. Then I set the AssignmentDescription to "2nd value" but it's done in the variable c.
I know that c is a myDataGridViewColumn with my properties and is equal to the column that I actually added to the grid. But in this case, it seems that c acually "IS" the column that I added. Is that right?
Also, I want to add a few custom properties to each row of the DataGridView. I've already created a class called myDataGridViewRow and added my specific new properties. How can I make sure those properties are available to the rest of my program? I assume it will have something to do with grd.Rows.Add or something like that. Am I far off?
-
Mar 23rd, 2008, 06:08 PM
#11
Thread Starter
Hyperactive Member
Re: Add Properties To DataGridView
-
Mar 23rd, 2008, 06:09 PM
#12
Thread Starter
Hyperactive Member
Re: Add Properties To DataGridView
vb Code:
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim col As New myDataGridViewColumn grd.Columns.Add(col) col.AssignmentDescription = "1st value" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim v As String Dim c As myDataGridViewColumn = CType(grd.Columns(0), myDataGridViewColumn) v = c.AssignmentDescription MsgBox(v) c.AssignmentDescription = "2nd value" End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim v As String Dim c As myDataGridViewColumn = CType(grd.Columns(0), myDataGridViewColumn) v = c.AssignmentDescription MsgBox(v) End Sub End Class
-
Mar 23rd, 2008, 06:31 PM
#13
Re: Add Properties To DataGridView
 Originally Posted by DroopyPawn
I know that c is a myDataGridViewColumn with my properties and is equal to the column that I actually added to the grid. But in this case, it seems that c acually "IS" the column that I added. Is that right?
That's how reference types work. Every class is a reference type and DataGridViewColumn is a class. Your type is a class too.
Reference type variables do not contain objects. They contain a reference to an object. A reference is basically a tarted-up pointer, i.e. a memory address. So, when you create your column it is stored in an area of memory known as the heap. Variables are stored on the stack. Any variable that refers to your column contains the address at which it is stored at on the heap.
If you assign one variable to another you copy the content of one variable to another. If the original variable contain the memory address of an object then the new variable now contains that same memory address, therefore both variables refer to the same object.
-
Mar 23rd, 2008, 06:34 PM
#14
Re: Add Properties To DataGridView
If you want to create a row in a DataGridView then you would need to inherit the DataGridViewRow class, but you're limited in how you can use it. In many cases the grid itself creates the rows and, unless you inherit and modify the DataGridView class too, no grid is going to create your custom rows. That means that you'd have to create instances of your class externally and then add them to the grid explicitly. This precludes you from using data-binding, which would generate standard rows.
-
Mar 23rd, 2008, 07:16 PM
#15
Thread Starter
Hyperactive Member
Re: Add Properties To DataGridView
My grid is unbound. I had already tried adding my custom row to the DataGridView (grd) but I didn't have any luck.
So if I create another class that inherits from DataGridView, what would I have to do to it to enable my custom rows, which I would add programatically?
And would I have to do anything extra to make it use my custom columns or would that part still be the same as it is now?
Thanks for the help.
-
Mar 23rd, 2008, 07:37 PM
#16
Re: Add Properties To DataGridView
I already said. Create instances of your row class and add them to the grid. You're already doing it for your column class. It's no different for the rows, except you add to the Rows collection instead of the Columns.
Last edited by jmcilhinney; Mar 23rd, 2008 at 07:44 PM.
-
Mar 23rd, 2008, 08:45 PM
#17
Thread Starter
Hyperactive Member
Re: Add Properties To DataGridView
I've tried the code bellow. The column properties work as expected but the Button1_Click sub returns nothing in the 2nd message box.
vb Code:
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim col As New myDataGridViewColumn grd.Columns.Add(col) col.AssignmentDescription = "1st value" Dim r As New myDataGridViewRow grd.Rows.Add(r) r.Name_First = "Joe1" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim v As String Dim c As myDataGridViewColumn = CType(grd.Columns(0), myDataGridViewColumn) v = c.AssignmentDescription MsgBox(v) c.AssignmentDescription = "2nd value" Dim r As myDataGridViewRow = CType(grd.Rows(0), myDataGridViewRow) MsgBox(r.Name_First) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim v As String Dim c As myDataGridViewColumn = CType(grd.Columns(0), myDataGridViewColumn) v = c.AssignmentDescription MsgBox(v) End Sub End Class
-
Mar 24th, 2008, 12:33 AM
#18
Re: Add Properties To DataGridView
And what exactly does the implementation of your Name_First property look like? Have you debugged it to make sure that the data is going where you think it is?
-
Mar 24th, 2008, 07:25 AM
#19
Thread Starter
Hyperactive Member
Re: Add Properties To DataGridView
I got it working. Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|