Results 1 to 8 of 8

Thread: [RESOLVED] Add column width to DataTable

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2018
    Posts
    61

    Resolved [RESOLVED] Add column width to DataTable

    Hello every one
    I need your help .. please
    To create my datatable column I use this code for the column names and the titles of these columns headers
    Code:
     Dim DT As New DataTable
         DT.Columns.Add(New DataColumn With {.ColumnName = "Product_Name", .Caption = "Product"})
         DataGridView1.DataSource = DT
    To adjust the width of these columns I add this code below
    Code:
    DataGridView1.Columns(0).Width = 40
    How to make for add column Width to this line .. so that they will be in the same line :
    Code:
     DT.Columns.Add(New DataColumn With {.ColumnName = "Product_Name", .Caption = "Product"})
    Thank you in advance for help

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,431

    Re: Add column width to DataTable

    What you're asking for doesn't make any sense. A DataTable is a data structure for the storage of data. A DataGridView is a control, for the display of data. The Width that you're setting in that second code snippet is the number of pixels over which that DataGridViewColumn is displayed on the screen. That only makes sense for something that displays data, so it makes sense for a DataGridViewColumn but it makes no sense for a DataColumn.

    If you are talking about the number of characters that can be stored in a field in that column, e.g. like a varchar(40) column in a database, then what you want is the MaxInputLength property of the DataGridViewTextBoxColumn and the MaxLength property of the DataColumn.

    You should already know all this though, because you should have read the documentation for both those classes and read about those properties. You should use this site for the stuff that you can't work out for yourself but anything that is written in the documentation of the classes you're using is definitely doesn;t qualify. You just have to make the effort. Given that you can click a type or member name in the code editor and press F1 to go directly to the relevant documentation, it's not even that much effort.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2018
    Posts
    61

    Re: Add column width to DataTable

    Thank you master for this lesson but I just wanted to put these two codes together

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,431

    Re: Add column width to DataTable

    Quote Originally Posted by ABIDINE View Post
    Thank you master for this lesson but
    But nothing, really. If you want to write software then you should be able to use software and that means reading the documentation that it's developers provide. Wouldn't you expect users of your software to read the documentation you provide? Not only should you be reading the documentation anyway, you'll be the one to benefit from doing so, so I accept your thanks.
    Quote Originally Posted by ABIDINE View Post
    I just wanted to put these two codes together
    I don't even know what that means. Like I said, what you asked for is nonsensical.

    If you actually are talking about the two properties that I pointed out then it may be the case that setting the MaxLength of a DataColumn and then binding it to a DataGridViewTextBoxColumn will automatically set the MaxInputLength. I've never tested that so I don't know but you can test it. If it doesn't then you have no choice but to set it yourself.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,461

    Re: Add column width to DataTable

    It is a simple misconception based on the fact that both a DGV and a datatable have a concept of a column. They are not the same thing, nor are they interchangeable in most ways. The column in a datatable is really just a field. It has no visual representation, so the very concept of size is meaningless outside of limiting the data that can go into one. A DGV column is a visible UI element, so it has some representation on the screen and therefore, size is meaningful for a DGV column.

    You can't combine the two because, despite both being called columns, they are not the same thing. You can't set the DGV column when setting a datatable column, and that's what you are trying to do.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2018
    Posts
    61

    Re: Add column width to DataTable

    Thank you very much Shaggy Hiker
    Why I can name the column and his title ( caption )

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,461

    Re: Add column width to DataTable

    Well, the name is very convenient. You can reference a field in a datarow by ordinal, or by name. The latter is much better in the long run, since dr("MyColumn") is very clear, whereas dr(1) may not be so clear. However, you must have a name for you to be able to reference a field by name, hence the name has to be possible for a column.

    I'd say that the caption is there for convenience. You often don't use it, but if it wasn't there, and you bound a datatable to a DGV for display purposes (cause why else would you bind the two?), you'd have to jump through hoops if the caption isn't already available. In fact, you'd then have to go through every column in the DGV and set the caption, if you wanted to see anything other than the field name. Since datatables most often come from database queries, and fields in databases are often not suitable for display to humans, you'd likely want captions. Still, that's mostly just convenience, as far as I can tell. Personally, I can't recall ever setting a caption for a column in a datatable, though it seems like I must have, at times.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2018
    Posts
    61

    Re: Add column width to DataTable

    Quote Originally Posted by Shaggy Hiker View Post
    Well, the name is very convenient. You can reference a field in a datarow by ordinal, or by name. The latter is much better in the long run, since dr("MyColumn") is very clear, whereas dr(1) may not be so clear. However, you must have a name for you to be able to reference a field by name, hence the name has to be possible for a column.

    I'd say that the caption is there for convenience. You often don't use it, but if it wasn't there, and you bound a datatable to a DGV for display purposes (cause why else would you bind the two?), you'd have to jump through hoops if the caption isn't already available. In fact, you'd then have to go through every column in the DGV and set the caption, if you wanted to see anything other than the field name. Since datatables most often come from database queries, and fields in databases are often not suitable for display to humans, you'd likely want captions. Still, that's mostly just convenience, as far as I can tell. Personally, I can't recall ever setting a caption for a column in a datatable, though it seems like I must have, at times.
    Thank you very much Gentelman

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