如何更改活动单元格行和列的背景颜色 [英] How can I change active cell row's and column's backcolor

查看:174
本文介绍了如何更改活动单元格行和列的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi I'm new in vb and I want to change row and column backcolor for active cell but my code not work well 
:





我的尝试:





What I have tried:

Dim col_index As Byte
Dim row_index As Byte
Private Sub dgv1_CellEnter_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv1.CellEnter
    Try
        If dgv1.CurrentCell.ColumnIndex > 0 Then
            TextBox1.Text = dgv1.CurrentRow.Cells.Item("stu_sery").Value
            index = dgv1.CurrentRow.Index
            ComboBox1.SelectedIndex = dgv1.CurrentCell.ColumnIndex - 1
            dgv1.Columns(dgv1.CurrentCell.ColumnIndex).DefaultCellStyle.BackColor = Color.Aqua
            dgv1.Rows(dgv1.CurrentCell.RowIndex).DefaultCellStyle.BackColor = Color.Aqua
            dgv1.Columns(col_index).DefaultCellStyle.BackColor= Color.White
            dgv1.Rows(row_index).DefaultCellStyle.BackColor = Color.White

            row_index = dgv1.CurrentCell.RowIndex
            col_index = dgv1.CurrentCell.ColumnIndex
        End If
    Catch ex As Exception

    End Try
End Sub

推荐答案

如果更改CellEnter事件处理程序中单元格的背景颜色,则在您进入单元格时会触发该事件。在您重新加载网格或更改颜色之前,这将保持不变,以下任何一种都可以工作;

a)在CellEnter事件处理程序中循环遍历所有行和&将颜色设置为默认值,但当前行除外

Bad Way - 不必要的循环

b)添加CellLeave事件处理程序。在此事件处理程序中将单元格颜色设置回默认值

正确方式



有关示例代码,请参阅以下MSDN文章; DataGridView.CellEnter事件(System.Windows.Forms) [ ^ ]



亲切的问候
If you change the background color of a cell within the CellEnter event handler the event is fired when you enter the cell. This will be maintained until you reload the grid or until you change the color, either of the following will work;
a) Within your CellEnter Event Handler loop through all the rows & set the color to default, with the exception of the current row
Bad Way - unnecessary loop
b) Add a CellLeave Event Handler. Set the cell color back to default within this event handler
Correct Way

Refer to the following MSDN Article for example code; DataGridView.CellEnter Event (System.Windows.Forms)[^]

Kind Regards


这篇关于如何更改活动单元格行和列的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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