C# 打印DataGrid?时的文本换行。网

C# 打印DataGrid?时的文本换行。网,c#,.net,printing,datagrid,word-wrap,C#,.net,Printing,Datagrid,Word Wrap,我试图在windows窗体应用程序中打印DataGrid,当列的宽度设置(可自定义)太窄而无法容纳文本时,它只会截断文本而不是将其环绕。DataGrid中是否有设置文本换行的属性 我添加了一些代码来帮助诊断问题 private void PrintRow(PointF location, PrintPageEventArgs e) { Graphics g = e.Graphics; PointF curLocation = location;

我试图在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);
    }
private void PrintRow(PointF位置,PrintPageEventArgs e)
{
图形g=e.图形;
点F位置=位置;
//测量一排的高度
SizeF charSize=g.MeasureString(MEASURE_CHAR.ToString(),this.Grid.Font);
浮动行高=字符大小。高度+单元格填充*2;
//打印第一个单元格左侧的垂直网格线
//请注意,我们只打印垂直网格线到底部
//最后一行的
int maxRowsOnPage=(int)数学地板(e.MarginBounds.Height/rowHeight);
int rowsRemaining=this.Grid.Rows.Count-\u curRowIdx;
int rowstorprint=Math.Min(maxRowsOnPage,rowsRemaining);
浮动底部=e.MarginBounds.Top+(行停止打印*行高);
g、 抽绳(钢笔。黑色,卷曲位置。X,e。边缘边界。顶部,卷曲位置。X,底部);
DataGridViewRow行=this.Grid.Rows[_curRowIdx];
foreach(查询字段在_字段中)
{                
foreach(row.Cells中的DataGridViewCell单元格)
{
//如果这不是正确的单元格,请提前退出
如果(this.Grid.Columns[cell.ColumnIndex].HeaderText!=field.FieldLabel)继续;
//计算我们需要在哪里绘制下一个单元格
int maxChars=field.MaxLength>0?field.MaxLength:field.FieldLabel.Length;
SizeF maxSize=g.MeasureString(string.Empty.PadLeft(maxChars,MEASURE\u CHAR),this.Grid.Font);
矩形F boundingRect=新矩形F(curLocation,maxSize);
//确保我们没有超出正确的边际
如果(boundingRect.Left>=e.MarginBounds.Right)
{
打破
}
如果(boundingRect.Right>e.MarginBounds.Right)
{
boundingRect.Width=boundingRect.Width-(boundingRect.Right-e.MarginBounds.Right);
}
//获取字段值
string fieldValue=string.Empty;
if(cell.Value!=null)
{
fieldValue=cell.Value.ToString();
}
//绘制字段值
g、 抽绳(fieldValue,this.Grid.Font,Brush.Black,(矩形F)boundingRect,sf);
curLocation.X+=边界矩形宽度;
curLocation.X+=单元格填充;
//打印此单元格和下一单元格之间的垂直网格线

如果(boundingRect.Right尝试此线程。有完全相同问题的人。希望它有帮助


非常感谢您的回复。这确实非常有帮助,并引导我解决了我的问题。很抱歉,标记为已回答花费了这么长时间。=)