c#windows应用程序中的垂直标题 [英] vertical headings in c# windows application

查看:69
本文介绍了c#windows应用程序中的垂直标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在c#data中显示我的43个动态列在c#中从左到右从上到下自上而下,但是在打印过程中我无法打印它已经在datagridview中显示了它。



 strFormat1.FormatFlags = StringFormatFlags.DirectionVertical;< br /> 



通过使用上面的行,它打印从右到左,从上到下,这很难读。

我的问题是如何从左到右垂直标题打印。

要在gridview中显示我用过以下代码: -



 DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; 
DataGridView1.ColumnHeadersHeight = 50 ;
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;

// 这里我们将一个事件处理程序附加到单元格绘制事件
DataGridView1.CellPainting + = new DataGridViewCellPaintingEventHandler(DataGridView1_CellPainting);

private void DataGridView1_CellPainting( object sender,DataGridViewCellPaintingEventArgs e)
{

// check我们在标题单元格中!
if (e.RowIndex == -1&& e.ColumnIndex > = 0
{
e.PaintBackground(e.ClipBounds,);
Rectangle rect = this .DataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, true );
大小titleSize = TextRenderer.MeasureText(e.Value.ToString(),e.​​CellStyle.Font);
if this .DataGridView1.ColumnHeadersHeight < ; titleSize.Width)
{
this .DataGridView1.ColumnHeadersHeight = titleSize.Width;
}

e.Graphics.TranslateTransform( 0 ,titleSize.Width);
e.Graphics.RotateTransform(-90.0F);

// 这是底部对齐的关键线 - 我们根据
// ColumnHeadersHeight减去当前文本宽度。 ColumnHeadersHeight是
// 所有列的最大值,因为我们绘制了两次单元格 - 尽管这样事实
// 在所有用法中可能都不正确!
e.Graphics.DrawString(e.Value.ToString(), this .Font,Brushes.Black, new PointF(rect.Y - (sbsDataGridView1.ColumnHeadersHeight - titleSize.Width),rect.X));

// 用于比较的旧行
// e.Graphics.DrawString(e.Value.ToString(),this.Font,Brushes.Black,new PointF(rect。 Y,rect.X));


e.Graphics.RotateTransform( 90 .0F);
e.Graphics.TranslateTransform( 0 ,-titleSize.Width);

e.Handled = true ;
}

}



打印: -



< pre lang =cs> foreach (DataGridViewColumn GridCol in DataGridView1.Columns)
{




e.Graphics.FillRectangle( new SolidBrush(Color.LightGray),
new Rectangle(( int )arrColumnLefts [iCount],iTopMargin,
int )arrColumnWidths [iCount],iHeaderHeight));

e.Graphics.DrawRectangle(Pens.Black,
new Rectangle(( int )arrColumnLefts [iCount],iTopMargin,
int )arrColumnWidths [iCount],iHeaderHeight));



e.Graphics.DrawString(GridCol.HeaderText,GridCol.InheritedStyle.Font,
new SolidBrush(GridCol.InheritedStyle.ForeColor),
new RectangleF(( int )arrColumnLefts [ iCount],iTopMargin,
int )arrColumnWidths [iCount],iHeaderHeight), strFormat1 );


iCount ++;
}
私有 void printDocument1_BeginPrint( object sender,PrintEventArgs e)

{

try
{
iRow = 0 ;
strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Near;
strFormat1.LineAlignment = StringAlignment.Center;
strFormat1.Trimming = StringTrimming.EllipsisCharacter;
strFormat1.FormatFlags = StringFormatFlags.DirectionVertical;


arrColumnLefts.Clear();
arrColumnWidths.Clear();
iCellHeight = 0 ;
// iCount = 0;
bFirstPage = ;
bNewPage = true ;

// 计算总宽度
iTotalWidth = 0 ;
foreach (DataGridViewColumn dgvGridCol in DataGridView1.Columns)
{
iTotalWidth + = dgvGridCol.Width;
}
}
catch (例外情况)
{
MessageBox.Show(ex.Message, 错误,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}





某处,我已经读过这个 strFormat1.FormatFlags = StringFormatFlags .DirectionVertical ,我必须再次旋转它,但我不知道该怎么做。



我的问题很长,因为代码我已发布,但有必要详细说明我的问题。

请尽快回复。

解决方案

这是<的第三个转贴a href =http://www.codeproject.com/Questions/743845/how-to-print-vertically-first-row-in-csharp>如何在c#中垂直打印第一行 [ ^ ]。请停止发送垃圾邮件,并使用原始中的改善问题链接添加更多信息。


I was able to show my 43 dynamic columns in datagridview in c# vertically left to right bottom-up,but during printing I was not able to print it has I have shown it in datagridview.

strFormat1.FormatFlags = StringFormatFlags.DirectionVertical;<br />


By using the above line, it prints right to left and top to bottom, which is difficult to read.
My question is how to print from left to right vertical headings.
To show in gridview I have used the below code:-

DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
            DataGridView1.ColumnHeadersHeight = 50;
            DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;

            // Here we attach an event handler to the cell painting event
            DataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(DataGridView1_CellPainting);

private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {

            // check that we are in a header cell!
            if (e.RowIndex == -1 && e.ColumnIndex >= 0)
            {
                e.PaintBackground(e.ClipBounds, true);
                Rectangle rect = this.DataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, true);
                Size titleSize = TextRenderer.MeasureText(e.Value.ToString(), e.CellStyle.Font);
                if (this.DataGridView1.ColumnHeadersHeight < titleSize.Width)
                {
                    this.DataGridView1.ColumnHeadersHeight = titleSize.Width;
                }

                e.Graphics.TranslateTransform(0, titleSize.Width);
                e.Graphics.RotateTransform(-90.0F);

                // This is the key line for bottom alignment - we adjust the PointF based on the 
                // ColumnHeadersHeight minus the current text width. ColumnHeadersHeight is the
                // maximum of all the columns since we paint cells twice - though this fact
                // may not be true in all usages!   
                e.Graphics.DrawString(e.Value.ToString(), this.Font, Brushes.Black, new PointF(rect.Y - (sbsDataGridView1.ColumnHeadersHeight - titleSize.Width), rect.X));

                // The old line for comparison
                //e.Graphics.DrawString(e.Value.ToString(), this.Font, Brushes.Black, new PointF(rect.Y, rect.X));


                e.Graphics.RotateTransform(90.0F);
                e.Graphics.TranslateTransform(0, -titleSize.Width);
                
                e.Handled = true;
            }

        }


For printing :-

foreach (DataGridViewColumn GridCol in DataGridView1.Columns)
                        {




                            e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                                new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
                                (int)arrColumnWidths[iCount], iHeaderHeight));

                            e.Graphics.DrawRectangle(Pens.Black,
                                new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
                                (int)arrColumnWidths[iCount], iHeaderHeight));



                            e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
                                new SolidBrush(GridCol.InheritedStyle.ForeColor),
                                new RectangleF((int)arrColumnLefts[iCount], iTopMargin,
                                (int)arrColumnWidths[iCount], iHeaderHeight), strFormat1);


                            iCount++;
                        }
private void printDocument1_BeginPrint(object sender, PrintEventArgs e)

        {

            try
            {
                iRow = 0;
                strFormat1 = new StringFormat();
                strFormat1.Alignment = StringAlignment.Near;
                strFormat1.LineAlignment = StringAlignment.Center;
                strFormat1.Trimming = StringTrimming.EllipsisCharacter;
                strFormat1.FormatFlags = StringFormatFlags.DirectionVertical;


                arrColumnLefts.Clear();
                arrColumnWidths.Clear();
                iCellHeight = 0;
                 //iCount = 0;
                bFirstPage = true;
                bNewPage = true;

                 //Calculating Total Widths
                iTotalWidth = 0;
                foreach (DataGridViewColumn dgvGridCol in DataGridView1.Columns)
                {
                    iTotalWidth += dgvGridCol.Width;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



Somewhere, I have read that after using this strFormat1.FormatFlags = StringFormatFlags.DirectionVertical, I have to again rotate it but I don't know how to do it.

My question is long because of the code that I have posted but it is necessary to elaborate my problem.
Pleas reply as soon as possible.

解决方案

This is the third repost of how to print vertically first row in c#[^]. Please stop spamming, and use the Improve question link in the original to add more information.


这篇关于c#windows应用程序中的垂直标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆