tags:

views:

48

answers:

1

I have a DataGridView that I'm trying to populate using a For loop:

    Dim serverName As String = SQLServerName + "\" + Instance
    Dim server As Server = New Server(serverName)

    Dim Datatable1 As New DataTable

    For Each database As Database In server.Databases
        Dim row As DataRow = Datatable1.NewRow

        row("Database") = database.Name
        row("Version") = DBVersionCheck(serverName, database.Name)
        row("Status") = My.Resources.My_Image
        Datatable1.Rows.Add(row)
    Next

    DataGridView1.DataSource = Datatable1

The DGV has been designed with the designer (columns, layout etc). Using the above the DGV does not populate. I was using a ListView for this but I need images in a subitem so have switched to using a DGV. Any advice?

+3  A: 

You need to add the columns to the DataTable.

I've got some code (which is C#) but you should be able to convert it:

var columnSpec = new DataColumn
                    {
                        DataType = string,
                        ColumnName = "Database Name"
                    };
this.dataTable.Columns.Add(columnSpec);

which will add a column of type string with the name "Database Name".

ChrisF
Sorry, written from memory - original corrected.
madlan
@madlan - answer updated.
ChrisF
The problem appears to be converting the image: row("Status") = My.Resources.My_Image will not work, I've created a new datacolumn with .DataType = GetType(System.Byte()).
madlan
Type of value has a mismatch with column typeCouldn't store <System.Drawing.Bitmap> in Status Column. Expected type is Byte[].
madlan
@madlan - What type is `My.Resources.My_Image` and what type have you used on the `DataGridView`?
ChrisF
System.Drawning.Bitmap - the DataTable is .DataType = GetType(System.Byte())
madlan
If I convert the iamge to a byte with MemoryStream it works, I thought it was possible to load a bitmap directly rather than convert to a byte array?
madlan
@madlan - I'm not sure how that would work with `DataTables`.
ChrisF
I've made a function that returns a byte array for the given image - this works fine when added to the datatable. Thanks Chris.
madlan