Results 1 to 9 of 9

Thread: [RESOLVED] Default value of String.Byte[] datacolumn

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Resolved [RESOLVED] Default value of String.Byte[] datacolumn

    Hi everyone,
    in my datatable I've a String.Byte[] column which contains the array of bytes of an image. So, when the datatable is binded to the datagrid, I can see the picture, if present, and the red cross if is not presente.
    So I can remove the red cross, by assign a value to the DefaultValue property of the datacolumn. But what default value I've to assign??
    I tried with system.dbnull.value but the red cross is still present.

    Thank you in advance for any reply.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Default value of String.Byte[] datacolumn

    Use an image as the default image. This image can be any image that you think fit your purpose... For example, if you want to show the message "No Image" to your user, create a image file that reads "No Image" in MS Paint (or any image editing software) and use it in your program.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Re: Default value of String.Byte[] datacolumn

    I don't think is a good idea for my application.
    I need to see nothing in my column, but I don't want to put a blank image into.
    Thank you.

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

    Re: Default value of String.Byte[] datacolumn

    If I had a $ for every time I've posted this Id' be a rich man. Absolutely EVERY time you have a question about a particular type or member, the first you MUST do is read the documentation for that type or member. If you'd done that then you'd have had the answer in about 30 seconds, because that's how long it took me to look it up. This is from the second paragraph of the Remarks section of the documentation for the DataGridViewImageColumn class:
    By default, empty cells display a default error graphic. To prevent this graphic from appearing for cell values equal to a null reference (Nothing in Visual Basic) or DBNull.Value, set the DataGridViewCellStyle.NullValue property of the cell style object returned by the DefaultCellStyle property to a null reference (Nothing in Visual Basic) before adding rows to the control.
    That's an explicit answer to your question in exactly the place it should be.
    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Re: Default value of String.Byte[] datacolumn

    Hi jmcilhinney, this time I think you miss..because my question is about DataColumn of Datatable and not about DataGridViewImageColumn of Datagridview. I know the property you are refering, because I used it in another kind of project, but it doesn't work for datacolumn.
    So I read first this article and then this article about my problem. The second one tells that that The default value of an integral type is equivalent to the literal 0, in the paragraph about the primitive types in vb.net. I tried to assign:
    Code:
    oCol.DefaultValue = System.Text.Encoding.ASCII.GetBytes("0")
    but it doesn't work.

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

    Re: Default value of String.Byte[] datacolumn

    because my question is about DataColumn of Datatable and not about DataGridViewImageColumn of Datagridview.
    Um, no it isn't. This has got NOTHING to do with the DataTable. It is purely the grid. Whenever you have a DataGridViewImageColumn in a DataGridView, each cell that doesn't contain a value, i.e. whose Value property is either Nothing or DBNull.Value, will display an error image containing a red cross by default. That is true no matter whether the grid is bound or unbound and, if it is bound, what it is bound to. This is purely behaviour of the grid and, if you want to change that behaviour, you do as the documentation I quoted says. Then you don't have to try to hack your DataTable, which wouldn't work anyway. Each field in your DataTable's image column that doesn't contain a Byte array will contain DBNull.Value. Once you've told the grid how to handle that by doing as I've already suggested, you'll see what you want to see.
    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Re: Default value of String.Byte[] datacolumn

    Quote Originally Posted by jmcilhinney View Post
    Um, no it isn't. This has got NOTHING to do with the DataTable. It is purely the grid. Whenever you have a DataGridViewImageColumn in a DataGridView, each cell that doesn't contain a value, i.e. whose Value property is either Nothing or DBNull.Value, will display an error image containing a red cross by default. That is true no matter whether the grid is bound or unbound and, if it is bound, what it is bound to. This is purely behaviour of the grid and, if you want to change that behaviour, you do as the documentation I quoted says. Then you don't have to try to hack your DataTable, which wouldn't work anyway. Each field in your DataTable's image column that doesn't contain a Byte array will contain DBNull.Value. Once you've told the grid how to handle that by doing as I've already suggested, you'll see what you want to see.
    Thank you for the clear explanation. I assign the property to DataGridViewImageColumn and works great.
    Sorry for my arrogance in previous post.
    Have a good day!

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

    Re: [RESOLVED] Default value of String.Byte[] datacolumn

    I guess you didn't realise that the behaviour you were trying to avoid was a function of the grid rather then the table. For future reference, any visual behaviour is almost certainly going to be the responsibility of a control, so that's where you should look first.
    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Re: [RESOLVED] Default value of String.Byte[] datacolumn

    Quote Originally Posted by jmcilhinney View Post
    I guess you didn't realise that the behaviour you were trying to avoid was a function of the grid rather then the table. For future reference, any visual behaviour is almost certainly going to be the responsibility of a control, so that's where you should look first.
    Yes, it's exactly my error!

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