我的datagridview打印有问题。 [英] Having trouble with my datagridview print.

查看:82
本文介绍了我的datagridview打印有问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我知道这些是非常棒的问题,但我有两个。其中之一是,在当前的代码中,我无法弄清楚如何使用文本换行调整单元格高度,并想知道是否有人可以引导我朝着正确的方向去做。另一个问题是我不知道为什么这只是显示第一行。循环有什么问题吗?



我确定我让它变得更加困难然后必要的就是找不到这类东西的预制类将在我的VS2010上运行。似乎只有C#类。



在此先感谢,这是代码部分:



< pre lang =vb> ' 使用内容绘制datagridview并设置起点
< span class =code-keyword> Dim x As Integer = e.MarginBounds。左
Dim y 作为 整数 = 230

' 实例化标题计数
Dim j 作为 整数 = 0

while (j< .DataGridView1.Columns.Count)

' 声明新的datagrid的矩形
Dim rect As Rectangle = 矩形(x,y, 165 .DataGridView1.ColumnHeadersHeight )

' 设置字符串格式并对齐
Dim sf As StringFormat = StringFormat
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Center

' 绘制并填充矩形
e.Graphics.FillRectangle(Br ushes.LightGray,rect)
e.Graphics.DrawRectangle(Pens.Black,rect)

' 检查以确保列存在
如果 Me .DataGridView1.Columns(j).HeaderText) 没什么然后

' 绘制列标题
e.Graphics.DrawString( Me .DataGridView1.Columns(j).HeaderText,SystemFonts.DefaultFont ,Brushes.Black,rect,sf)

结束 如果

' 增加rectanlge和col的宽度umn数字
x =(x + rect.Width)
j =(j + 1

循环

' 设置矩形开始点
x = e.MarginBounds.Left
y =(y + Me .DataGridView1.ColumnHeadersHeight)

' 实例化行数
Dim i As 整数 = 0

' 绘制行
For 每个 As DataGridViewRow Me .DataGridView1.Rows

' 循环直到counter等于行数
(i< .DataGridView1.Columns.Count)

' 识别单元格
Dim 单元格 As DataGridViewCell

' 将单元格与行相关联
cell = row.Cells(i )

' 创建新矩形
Dim rect As Rectangle = 矩形(x,y,< span class =code-digit> 165 ,cell.Size.Height)

' 设置字符串格式并对齐
Dim sf As StringFormat = StringFormat
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Center

' 绘制
e.Graphics.DrawRectangle(钢笔.Black,rect)

' 如果有值,则绘制单元格值
如果(cell.Value) Nothing 然后
e.Graphics.DrawString(cell.Value.ToString ,SystemFonts.DefaultFont,Brushes.Black,rect,sf)
结束 如果

' 增加矩形和计数
x =(x + rect.Width)
i =(i + 1
循环

x = 180
y =(y + row.Height)

下一步

解决方案

...请查看我对该问题的最后评论。



再次,请勿打印 DataGridView 本身,打印用于填充它的数据......数据库用作数据层。将数据从数据层移动到UI并进行打印,而不是从UI移动到打印。它降低了系统的刚度,使其更加松散耦合

http: //en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA


So, I know these are really noob questions, but I have two. One of them is that in the current code I cannot figure out how to adjust the cell with with the text wrap for height and was wondering if someone could lead me in the right direction to do that. The other question is that I don't know why this is only showing the first row. Is there something wrong with the loop?

I'm sure I am making it more difficult then necessary just can't find a premade class for this type of thing that will run on my VS2010. Seems there are only C# classes.

Thanks in advance and here is the code section:

'Draw the datagridview with contents and set start point
        Dim x As Integer = e.MarginBounds.Left
        Dim y As Integer = 230

        'Instantiate header count
        Dim j As Integer = 0

        Do While (j < Me.DataGridView1.Columns.Count)

            'Declare new rectangle for datagrid
            Dim rect As Rectangle = New Rectangle(x, y, 165, Me.DataGridView1.ColumnHeadersHeight)

            'Set string format and align
            Dim sf As StringFormat = New StringFormat
            sf.LineAlignment = StringAlignment.Center
            sf.Alignment = StringAlignment.Center

            'Draw and fill the rectangle
            e.Graphics.FillRectangle(Brushes.LightGray, rect)
            e.Graphics.DrawRectangle(Pens.Black, rect)

            'Check to make sure column exists
            If (Not (Me.DataGridView1.Columns(j).HeaderText) Is Nothing) Then

                'Draw column header
                e.Graphics.DrawString(Me.DataGridView1.Columns(j).HeaderText, SystemFonts.DefaultFont, Brushes.Black, rect, sf)

            End If

            'Increment the width of rectanlge and column number
            x = (x + rect.Width)
            j = (j + 1)

        Loop

        'Set the point of rectangle start
        x = e.MarginBounds.Left
        y = (y + Me.DataGridView1.ColumnHeadersHeight)

        'Instantiate row count
        Dim i As Integer = 0

        'Draw rows
        For Each row As DataGridViewRow In Me.DataGridView1.Rows

            'Loop through until counter is equal to number of rows
            Do While (i < Me.DataGridView1.Columns.Count)

                'Identify the cell
                Dim cell As DataGridViewCell

                'Associate cell with row
                cell = row.Cells(i)

                'Create new rectangle
                Dim rect As Rectangle = New Rectangle(x, y, 165, cell.Size.Height)

                'Set string format and align
                Dim sf As StringFormat = New StringFormat
                sf.LineAlignment = StringAlignment.Center
                sf.Alignment = StringAlignment.Center

                'Draw the
                e.Graphics.DrawRectangle(Pens.Black, rect)

                'Draw cell values if there is value
                If (Not (cell.Value) Is Nothing) Then
                    e.Graphics.DrawString(cell.Value.ToString, SystemFonts.DefaultFont, Brushes.Black, rect, sf)
                End If

                'Increment the rectangle and count
                x = (x + rect.Width)
                i = (i + 1)
            Loop

            x = 180
            y = (y + row.Height)

        Next

解决方案

…Please see my last comment to the question.

Again, don't print DataGridView itself, print the data you use to populate it… The database serves as a data layer. Move data from data layer to UI and to print, not from UI to print. It reduces stiffness of the system, makes it more loosely coupled:
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


这篇关于我的datagridview打印有问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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