文本打印DataGrid时包装? 。净

时间:2022-10-26 22:18:32

I'm trying to print a DataGrid in a windows forms app and when the width of the columns is set (it's customizable) too narrow to fit the text it just truncates the text instead of wrapping it. Is there a property in DataGrid that sets text wrapping?

我试图在Windows窗体应用程序中打印一个DataGrid,当列的宽度设置(可自定义)太窄而不适合文本时,它只是截断文本而不是包装它。 DataGrid中是否有一个属性可以设置文本换行?

I've added some code to perhaps help with diagnosis of the issue.

我添加了一些代码来帮助诊断问题。

private void PrintRow(PointF location, PrintPageEventArgs e)
    {
        Graphics g = e.Graphics;
        PointF curLocation = location;

        //Measure the height of one row
        SizeF charSize = g.MeasureString(MEASURE_CHAR.ToString(), this.Grid.Font);
        float rowHeight = charSize.Height + CELL_PADDING * 2;

        //Print the vertical gridline on the left side of the first cell
        //Note that we only print the vertical gridlines down to the bottom 
        //of the last printed row
        int maxRowsOnPage = (int)Math.Floor(e.MarginBounds.Height / rowHeight);            
        int rowsRemaining = this.Grid.Rows.Count - _curRowIdx;
        int rowsToPrint = Math.Min(maxRowsOnPage, rowsRemaining);
        float bottom = e.MarginBounds.Top + (rowsToPrint * rowHeight);
        g.DrawLine(Pens.Black, curLocation.X, e.MarginBounds.Top, curLocation.X, bottom);

        DataGridViewRow row = this.Grid.Rows[_curRowIdx];
        foreach (QueryField field in _fields)
        {                
            foreach (DataGridViewCell cell in row.Cells)
            {
                //Exit early if this is not the correct cell
                if (this.Grid.Columns[cell.ColumnIndex].HeaderText != field.FieldLabel) continue;                    

                //Calculate where we need to draw the next cell
                int maxChars = field.MaxLength > 0 ? field.MaxLength : field.FieldLabel.Length;
                SizeF maxSize = g.MeasureString(string.Empty.PadLeft(maxChars, MEASURE_CHAR), this.Grid.Font);
                RectangleF boundingRect = new RectangleF(curLocation, maxSize);                    

                //Make sure we don't overshoot the right margin
                if (boundingRect.Left >= e.MarginBounds.Right)
                {
                    break;
                }
                if (boundingRect.Right > e.MarginBounds.Right)
                {
                    boundingRect.Width = boundingRect.Width - (boundingRect.Right - e.MarginBounds.Right);
                }

                //Get the field value
                string fieldValue = string.Empty;
                if (cell.Value != null)
                {
                    fieldValue = cell.Value.ToString();
                }

                //Draw the field value                    
                g.DrawString(fieldValue, this.Grid.Font, Brushes.Black, (RectangleF)boundingRect, sf);

                curLocation.X += boundingRect.Width;
                curLocation.X += CELL_PADDING;

                //Print the vertical gridline between this cell and the next
                if (boundingRect.Right <= e.MarginBounds.Right)
                {
                    g.DrawLine(Pens.Black, curLocation.X, e.MarginBounds.Top, curLocation.X, bottom);                       
                }

                //Move the current location to the next position
                curLocation.X += CELL_PADDING;
            }
        }

        //Draw the top gridline                 
        g.DrawLine(Pens.Black, e.MarginBounds.Left, e.MarginBounds.Top, curLocation.X, e.MarginBounds.Top);

        //Draw the bottom gridline     
        curLocation.Y += charSize.Height;                
        curLocation.Y += CELL_PADDING;
        g.DrawLine(Pens.Black, e.MarginBounds.Left, curLocation.Y, curLocation.X, curLocation.Y);
    }

1 个解决方案

#1


Try this thread. Someone with the exact same problem. Hope it helps!

试试这个帖子。有人遇到完全相同的问题。希望能帮助到你!

How to Wrap text in a text box column of datagrid in windows application.

如何在Windows应用程序中的datagrid的文本框列中包装文本。

#1


Try this thread. Someone with the exact same problem. Hope it helps!

试试这个帖子。有人遇到完全相同的问题。希望能帮助到你!

How to Wrap text in a text box column of datagrid in windows application.

如何在Windows应用程序中的datagrid的文本框列中包装文本。