0

MS strikes again. In my tiny little mind this should be a no brainer but I just don't see how to make this work.
I have an unbound DataGridView control where I have defined the columns at design time.
Currently to add a new row I use a like this:

gridname.row.add(value1, value2, value3, value4)

This offends my sense of good programming because if I add a column to the grid or change the order of the columns then I need to alter the line above. What I want to do is to create a new datagridviewrow complete with columns and then populate the values like so:

dr.cells("FirstName").value = value1
dr.cells("LastName").value = value2
dr.cells("Address").value = value3

Just like I do when editing an existing row in the grid.

I thought I could do the following:

Dim dr as New DataGridViewRow
dr = MyGrid.RowTemplate

I thought this would give me a new row complete with cells but no values. Appartently this is the wrong property. Is there a property that will give me a blank row, but with the cell names/types that have already been defined in the grid defined in the row?

Yes, I know I can take an existing row and just clear the values but when this program load, there are not always existing rows. Please don't tell me I am going to have to add the columns myself. dr.cell.add(cell definition)

2
Contributors
1
Reply
4
Views
5 Years
Discussion Span
Last Post by Oxiegen
0

You were very close to the solution, my friend.

Dim dr As New DataGridViewRow
dr.CreateCells(gridname)
gridname.Rows.Add(dr)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.