0

I am starting a new project and getting back to an old question.

My application will display the user some information in a DataGridView. The DataGridView will get its data from a database as DataTable. The database-table contains a column called "icon" which is of the type Integer. The column "icon" is referring to my Image Array()-Index. So when item("icon").Equals(2) then ImageArray(2) should be shown.

What is the best way, to make my DataGridView display that icon instead of its index?


What I used to use in pseudocode;

Dim dt as DataTable = GetDataFromDatabase()
dt.columns.add("Icon")
For each row as DataRow in dt.Rows
  row.item("Icon") = IconArray(row.Item("IconIndex"))
End For
dt.columns.remove("IconIndex")
MyDataGridView.DataSource = dt

Obviously this performes terribly when called every few seconds to keep the data up to date.


What I have also tried:
- Played around with the CellFormattingEvent
- Played around with DisplayValue-Attribute

Luke
  • 751
  • 2
  • 7
  • 32
  • 1
    You dont need to loop - use the CellFormatting event to display the desired image either in place of the integer or hide the int column and use your own image column. `keep the data up to date` seems worth explaining since it has a bearing especially given that `GetDataFromDatabase` method – Ňɏssa Pøngjǣrdenlarp Nov 09 '16 at 15:10
  • Pre-Defining the columns of my DataGridView might be a good way to go. If i disallow my DataGridView to generate columns it might work out with the cell formatting event. haven't thought about it this way yet. – Luke Nov 09 '16 at 15:13
  • 1
    you can let it generate columns then hide the int column and add a new imagecolumn – Ňɏssa Pøngjǣrdenlarp Nov 09 '16 at 15:14
  • Keeping data up to date yet always has been creating new datatables and replacing the old datasource which probably isnt the best way - not even a good one at all. GetDataFromDatabase is just an `SQLAdapter.fill(DataTable)` – Luke Nov 09 '16 at 15:16
  • definitely not the most efficient. if you configure it correctly you can simply refresh the data - I presume you are trying to pick up changes from other users? – Ňɏssa Pøngjǣrdenlarp Nov 09 '16 at 15:18
  • You're on point. Yes i do try to pick up changes from other users. Refreshing data instead of creating new sources is my next step to get a better performing application than my last one. – Luke Nov 09 '16 at 15:20
  • 1
    [Searching values via a datagridview](http://stackoverflow.com/q/33701262/1070452) shows how to configure a DataAdapter to be able to refresh. There is somewhat of a "trick" to removing extra rows on the client who just added them, but that may be down the road – Ňɏssa Pøngjǣrdenlarp Nov 09 '16 at 15:25
  • @Plutonix I know this is chat and not appreciated on SO but a huge thanks. You didn't just help me with that icon problem but also a lot with an other task that I didnt even mention in the question (Refreshing data). – Luke Nov 09 '16 at 15:30

0 Answers0