【原创】.net 开发问题记录

1、让DataGridView显示行号

如下:
这里写图片描述
将以上参数设置为fill即可

2、让DataGridView显示行号

为了表示行号,我们可以在DataGridView的RowPostPaint事件中进行绘制。RowPostPaint事件,具体可以参照MSDN。

下面是实现代码:

c# 实现:
private void Form1_Load(object sender, System.EventArgs e)
{
    dataGridView1.Dock = DockStyle.Fill;
    dataGridView1.DataSource = System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders();
}

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X,
        e.RowBounds.Location.Y,
        dataGridView1.RowHeadersWidth - 4,
        e.RowBounds.Height);

    TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
        dataGridView1.RowHeadersDefaultCellStyle.Font,
        rectangle,
        dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
        TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}

vb.net 实现:
Private Sub dgvlist_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgvlist.RowPostPaint
        Dim rectangle As System.Drawing.Rectangle = New System.Drawing.Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvlist.RowHeadersWidth - 4, e.RowBounds.Height)
        TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvlist.RowHeadersDefaultCellStyle.Font, rectangle, dgvlist.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter + TextFormatFlags.Right)
    End Sub  

3、vb.net dll动态库调用

    <DllImport("coredll.dll")> _
    Public Shared Function SendMessage(ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer)
    End Function
    <DllImport("coredll.dll")> _
    Public Shared Function PostMessage(ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer)
    End Function

4、内存数据集(内存数据表)手工创建以及DataGrid列宽调整方法

Me.DGUI = New System.Windows.Forms.DataGrid
'''''''''''内存数据表的建立''''''''''''''''''
                Dim dset As DataSet = New DataSet("memdset")
                Dim dtab As DataTable = dset.Tables.Add("UI")
                dtab.Columns.Add("相别", GetType(String)) 'New  String("").GetType
                dtab.Columns.Add("测量值", GetType(String))
                dtab.Columns.Add("相位", GetType(String))
                DRow = dtab.NewRow ' 
                DRow("相别") = "Ua"
                DRow("测量值") = "220.3"
                DRow("相位") = "653.1256"
                dtab.Rows.Add(DRow)
                DRow = dtab.NewRow ' 
                DRow("相别") = "Ub"
                DRow("测量值") = "230.3"
                DRow("相位") = "3.1256"
                dtab.Rows.Add(DRow)
                DRow = dtab.NewRow ' 
                DRow("相别") = "Uc"
                DRow("测量值") = "0.3"
                DRow("相位") = "120.56"
                dtab.Rows.Add(DRow)
                DGUI.DataSource = dtab
                DGUI.BackColor = Color.Azure
                DGUI.BackgroundColor = Color.White
                '''''''''''DataGrid控件列宽自动控制方法'''''''''''''''''
                Dim dgtype As DataGridTableStyle = New DataGridTableStyle
                dgtype.MappingName = dtab.TableName '这一步不可缺少
                DGUI.TableStyles.Clear()
                DGUI.TableStyles.Add(dgtype)
                Dim dgcoltype As GridColumnStylesCollection = DGUI.TableStyles(0).GridColumnStyles
                dgcoltype(0).Width = 50
                dgcoltype(1).Width = 100
                dgcoltype(2).Width = 100
                ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                '''''''DataGrid控件上绘图方法,该绘图方法可行,但是由于DataGrid本身有数据,导致本函数退出后DataGrid自身的刷新会覆盖下面的绘图。
                SetGridLines(DGUI)
                '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''
    ''' <summary>
    ''' DataGrid上绘图,测试发现listview不可实现该功能
    ''' </summary>
    ''' <param name="lvw"></param>
    ''' <remarks></remarks>
    Public Shared Sub SetGridLines(ByVal lvw As System.Windows.Forms.DataGrid)
        Try
            ' Create image.
            Dim drawFont As New System.Drawing.Font("Arial", 10, FontStyle.Regular)
            Dim drawBrush As New  _
            System.Drawing.SolidBrush(System.Drawing.Color.Black)
            '
            '绘制坐标系
            '
            ''''''''''''''''''''''''Ua 始终在度方向''''''''''''''''''''''''''''''''''''''''''''''''''''''
            Dim newGraphics As Graphics = lvw.CreateGraphics
            'newGraphics.Clear(Color.White)
            'newGraphics.Clear(System.Drawing.Color.FromArgb(CType(CType(224, Byte), Integer), CType(CType(224, Byte), Integer), CType(CType(224, Byte), Integer)))
            Dim x As Integer = newGraphics.DpiX
            Dim y As Integer = newGraphics.DpiY
            Dim R As Integer = 100
            Dim pen As Pen = New Pen(Color.Black, 1)
            newGraphics.DrawEllipse(pen, 0, 0, 2 * R, 2 * R)
            newGraphics.DrawLine(pen, R, 0, R, 2 * R)
            newGraphics.DrawLine(pen, 0, R, 2 * R, R)
            '
            '计算角度指向的终点坐标
            '
            Dim tpoint As Point() = {New Point(10, 10), New Point(30, 30)}
            Dim clor() As System.Drawing.Color = {Color.Gold, Color.Green, Color.Red}
            newGraphics.DrawString("test", drawFont, drawBrush, tpoint(1).X, tpoint(1).Y)
            drawFont.Dispose()
            drawBrush.Dispose()
            newGraphics.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

5、.net 事件处理

    ''' <summary>
    ''' 控件动态添加函数,向FlowLayoutPanel控件flp1中动态添加子控件
    ''' </summary>
    ''' <param name="text"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function flp1_add(ByVal text As String) As Boolean
        If text.ToLower.StartsWith("system") Or String.IsNullOrEmpty(text.Trim) Then
            Return False
            Exit Function
        End If
        For i As Int16 = 0 To flp1.Controls.Count - 1
            If flp1.Controls.Item(i).Text = text Then
                RemoveHandler flp1.Controls.Item(i).MouseDoubleClick, AddressOf labclicked_DoubleClick  '指定子控件鼠标双击响应函数
                flp1.Controls.RemoveAt(i)
                Return False
                Exit Function
            End If
        Next
        Dim netctl As Control = New Label
        netctl.Text = text
        netctl.BackColor = Color.GreenYellow
        flp1.Controls.Add(netctl)
        AddHandler netctl.MouseDoubleClick, AddressOf labclicked_DoubleClick     '指定子控件鼠标双击响应函数
        Return True
    End Function
    ''' <summary>
    ''' 子控件鼠标双击响应函数
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Sub labclicked_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim lab As Label = sender
        Dim str As String = lab.Text
        flp1_add(str)
        '厂家
        Dim x As Int16 = InStr(selectcond1, str)
        If x > 0 Then
            selectcond1 = selectcond1.Remove(x - 1, str.Length + 1)
            If String.IsNullOrEmpty(selectcond1) Then
                cbCJ.Checked = False
                cobCJ.Text = ""
            End If
        End If
        '工程队
        x = InStr(selectcond2, str)
        If x > 0 Then
            selectcond2 = selectcond2.Remove(x - 1, str.Length + 1)
            If String.IsNullOrEmpty(selectcond2) Then
                cbGCD.Checked = False
                cobGCD.Text = ""
            End If
        End If
        '浮动标志
        x = InStr(str, "浮动:")
        If x > 0 Then
            selectcond3 = ""
            cbFDBZ.Checked = False
            cobFDBZ.Text = ""
        End If
        '出账
        x = InStr(str, "出账:")
        If x > 0 Then
            selectcond4 = ""
            cbCZ.Checked = False
            cobCZ.Text = ""
        End If
    End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值