Results 1 to 6 of 6

Thread: [RESOLVED] Datagridview column datatypes

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    Resolved [RESOLVED] Datagridview column datatypes

    I'm having a slight issue displaying images in my DataGridView...

    I am importing a CSV file to a datatable No problem.
    I am then naming the column names like so dt.columns(0) = "Name" etc..

    no problem so far..

    I have a routine that looks for text and replaces it with an image, this also works as long as I create the column like so:

    .Columns.AddRange( _
    New DataGridViewTextBoxColumn(), _
    New DataGridViewTextBoxColumn(), _
    New DataGridViewTextBoxColumn(), _
    New DataGridViewTextBoxColumn(), _
    New DataGridViewTextBoxColumn(), _
    New DataGridViewTextBoxColumn(), _
    New DataGridViewTextBoxColumn(), _
    New DataGridViewImageColumn()) ' Note that my last column is an image column...


    Now, I use this command to import the datatable in to the datagrid:

    dataGridView1.DataSource = dt

    Of course I can't use the above method to create the image column as it will just add columns after ones I've already created so how can I change the datagridview column to display images before I use:
    dataGridView1.DataSource = dt??

    I need to be able to bind to the datatable and still be able to display images.

    Any help would be great.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagridview column datatypes

    If you create the columns yourself and you want to bind data then you have to set the DataPropertyName of each column to tell it which column/property of the data source to bind to. You should create the columns in the designer rather than in code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    Re: Datagridview column datatypes

    When I try to create the columns using the designer then a new set of columns are added at the end because I use dataGridView1.DataSource = dt This is the problem.

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    Re: Datagridview column datatypes

    Ah, I think I understand... How do I bind a datatable column to a datagrid column?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagridview column datatypes

    Quote Originally Posted by ElliotHC View Post
    Ah, I think I understand... How do I bind a datatable column to a datagrid column?
    How I told you to do it in post #2. If you want an example then follow the CodeBank link in my signature and check out thread on putting a ComboBox Column In A DataGridView.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    13

    Re: Datagridview column datatypes

    Works great, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width