DataGridView custom function?

G

greg

I've got a DataGridView which has a bunch of DataRow's that represent rows in
a table in my database. No brainer :)

Some of these rows are things such as "province ID" which are keys to a
province table. So I'd like to store these as-is since it makes database
transactions straightforward.

However, is it possible in my DataGridView, to have it format these cells as
text, using a function instead?

eg. for a cell with value 4, I'd like it to call a function
ProvinceCache.getProvinceName(cellValue); which would return a string for the
corresponding province.

Please note that this gridview is read-only, modifying data through the
gridview is not a requirement.


Thanks,
Greg
 
R

RobinS

The best thing would be if the original data could do a join on whatever
table has the text data, and return that for display. That's going to be the
most performant.

Barring that, you could make that column a combobox and bind it to the id in
the table, and a list of povinces.

Barring that, capture the Format event on each cell and go get the province.
This converts 75000 to "75k" for display. You can just add a call to get the
province description right there.

-------------------
CellFormatting happens between the data source and the screen.

Try this:

AddHandler myGrid.CellFormatting, AddressOf OnCellFormatting

Private Sub OnCellFormatting(ByVal object as Sender, _
ByVal e as DataGridViewCellFormattingEventArgs)

If e.ColumnIndex = myGrid.Columns("columnIcareAbout") Then
'setting this to true signals the grid that this
' column is being dynamically updated. If you don't
' do this, you will get an infinite loop if you have
' also set the column to have its size automatically
' determined.
e.FormattingApplied = True

'get the row being populated; format this cell
'Dim row as DataGridViewRow = myGrid.Rows(e.RowIndex)
If e.Value = "75000" Then
e.Value = "75K"
End If
End If
End Sub
 
J

Jeffrey Tan[MSFT]

Hi Greg,

Have you reviewed Robin's reply to you? Does it make sense to you? If you
still need any help, please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

greg

Thanks Robin, just did it and it worked like a charm!! I overrode the event
handler. This is because the user is adding new entries, some from dropdowns
which are cached from the database. Hitting "Add", adds that entry to the
datagridview.. no need to hit the database to join the ID to the province,
it's already cached.. so I just have the formatting event call a function to
get the appropriate name for that ID!

Thanks again,
Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top