none
Convert DataTable column width in pixels RRS feed

  • Question

  • Hi

    Let us suppose I know a DataTable column width (number of characters got from sql server nvarchar)

    Now i need to convert this width (characters)  in pixels using Segue UI font

    How to convert it ?

    Thursday, May 10, 2018 9:24 PM

All replies

  • Create an instance of the Font from the name then use TextRender.MeasureString with the string and the font you created.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by Cor Ligthert Friday, May 11, 2018 12:46 PM
    Thursday, May 10, 2018 9:50 PM
    Moderator
  • What is the purpose for calculating the width of the string?  If it is so that you can set the column width in a DataGridView when the data is loaded,  you can just use the DataGridViewColumn.AutoSizeMode Property to do that.  You can specify the column(s) by the (column index) or by the (column name).  For example, the below code will set the "Comments" column width to fit the widest string in the column.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
            dt.Columns.Add("Comments", GetType(String))
            dt.Rows.Add("Short comment")
            dt.Rows.Add("Some longer comment")
    
            DataGridView1.DataSource = dt 'load the data to the DataGridView
    
            DataGridView1.Columns("Comments").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells 'auto size the width of the column to fit the longest string in the column
        End Sub
    


    If you say it can`t be done then i`ll try it

    Thursday, May 10, 2018 9:56 PM
  • I have to create a User Control with a list view with one column and this column should have a widht = to Column DataTable width(that is in number of characters)
    Thursday, May 10, 2018 10:05 PM
  • I have to create a User Control with a list view with one column and this column should have a widht = to Column DataTable width(that is in number of characters)

     Well then,  you can fully load all your data strings into the ListView and then use the ColumnHeader.AutoResize Method as shown below.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListView1.View = View.Details
            ListView1.Columns.Add("User Comment")
    
            ListView1.Items.Add("Short comment")
            ListView1.Items.Add("Some longer comment")
    
            ListView1.Columns(0).AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent)
        End Sub
     

     Or....

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListView1.View = View.Details
            ListView1.Columns.Add("Comments", "User Comment")
    
            ListView1.Items.Add("Short comment")
            ListView1.Items.Add("Some longer comment")
    
            ListView1.Columns("Comments").AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent)
        End Sub
    


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Thursday, May 10, 2018 10:21 PM
    Thursday, May 10, 2018 10:16 PM
  • I have to create a User Control with a list view with one column and this column should have a widht = to Column DataTable width(that is in number of characters)
    Segoe UI or Segoe UI Mono?  You can't calculate the width of a 'number of characters' of a proportional font.  You can calculate the width of a specific string of characters.  If you are trying to set a column width to the width of a number of characters the best you can do is choose a character that is of average width, create a string of the correct length using that character, and calculate the length of that string.  For some combinations of characters it will be too wide, and for others it will be too narrow.
    Thursday, May 10, 2018 10:20 PM
  • Ok Acanar, but let us suppose I have a DataTable Column(from sql) large nvarchar(120)

    If every field in the column is full of character "A" I would like to compute the string width in pixel based on a particular font. (i.e.SEGUE UI or others)

    Which is the statement to convert the values ?

    Friday, May 11, 2018 11:04 AM
  • If every field in the column is full of character "A" I would like to compute the string width in pixel based on a particular font. (i.e.SEGUE UI or others)

    Which is the statement to convert the values ?

    That's the first response above: "Create an instance of the Font from the name then use TextRender.MeasureString with the string and the font you created."

    Friday, May 11, 2018 11:57 AM
  • Reed gave already the solution in the first reply, 

    But here in the way of the spoon.

    Dim textSize As Size = TextRenderer.MeasureText("aaaaa", New System.Drawing.Font("Segue UI", 16))
    The width = TextSize.Width 

    Success
    Cor

    Friday, May 11, 2018 12:48 PM