Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView and new rows
 

DataGridView and new rows

I have a DataGridView that binds to an IList<T> collection of custom objects. The problem I have is that when I click on the new line, the DataGridView creates a new object using the default constructor (but I assume it doesn't get added to the collection at this point). What I would like to do is intecept this proceedure so that I can call a different constructor for the new object or else do some initialization before the DataGridView attempts to bind it.

The reason is that my object implements ICustomTypeDescriptor and I need to set up all the rows to have identical properties so that they can all have the same columns.

Any help here?

Thanks.

wjousts  Tuesday, December 19, 2006 6:50 PM
In simpler terms, use the BindingSource.AddingNew event and set e.NewObject to whatever new object you want (using whatever constructor you want).
wjousts  Friday, December 22, 2006 6:55 PM

Public Class Form2

WithEvents s As New BindingSource

Public Sub New()

InitializeComponent()

Dim g As New List(Of test)

s.DataSource = g

Me.DataGridView1.DataSource = s

End Sub

Private Class test

Private b As Int16

Public Property a() As Int16

Get

Return b

End Get

Set(ByVal value As Int16)

b = value

End Set

End Property

Sub New()

End Sub

Sub New(ByVal w As Int16)

a = w

End Sub

End Class

Private Sub s_AddingNew(ByVal sender As Object, ByVal e As System.ComponentModel.AddingNewEventArgs) Handles s.AddingNew

e.NewObject = New test(3)

End Sub

End Class

hrubesh  Wednesday, December 20, 2006 5:26 AM
In simpler terms, use the BindingSource.AddingNew event and set e.NewObject to whatever new object you want (using whatever constructor you want).
wjousts  Friday, December 22, 2006 6:55 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView Multiple Selections
• Working with Datasets & DataGrids
• DataGridBoolColumn Question
• Update Problem in Formatted DateTime Field in DataGridView
• loading defaults after "AddNewItem" clicked on a Details form
• Unable to add ubound image columns to datagridview
• Need function to allow only numbers
• DataGridViewColumnHeaderCell
• Textbox updated by listbox selection
• Adding New Records