打印 DataGrid 时文本换行? 。网

发布于 2024-07-21 21:07:15 字数 3540 浏览 3 评论 0原文

我正在尝试在 Windows 窗体应用程序中打印 DataGrid,当列的宽度设置(可自定义)太窄而无法容纳文本时,它只会截断文本而不是换行。 DataGrid 中是否有设置文本换行的属性?

我添加了一些代码,也许可以帮助诊断问题。

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);
    }

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?

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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2024-07-28 21:07:16

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

如何在 Windows 应用程序中的数据网格的文本框列中换行文本。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文