数据网格视图事件 [英] Data grid view events

查看:62
本文介绍了数据网格视图事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用datagrid视图中的复选框列.
在选中特定行的复选框时,我想将该行的背景色设置为其他颜色,然后取消选中,将其设置为白色.
我应该在哪个datagrid事件上编写代码?

还有另一件事如何获取复选框的选中状态的值,即在任何datagrid事件中是否选中(真或假)?

希望找到解决方案.

I am Using a checkbox column in datagrid view.
On checking the checkbox of a particular row I want to set the back color of that row to different color and on uncheck, back to white.
On which event of datagrid should I write the code?

And one more thing how to get value of checked state of checkbox i.e. whether it is checked or not (true or false) in any event of datagrid?

Hoping for a solution.

推荐答案

Manoj,

在单元格内容上单击.

Hi Manoj,

On cell content click.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                if (e.ColumnIndex != 1)
                {
                    if (dataGridView1.Columns[e.ColumnIndex].Name = "colCheckBox")
                    {
                        if ( Convert.ToBoolean (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == true )
                        {
                            //do this
                        }
                        else
                        {
                            //do do that
                        }
                    }
                }
            }
        }


您想要这个吗?

You want this?

Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
  If e.RowIndex < 0 Or e.ColumnIndex < 0 Then Exit Sub
  If e.ColumnIndex = 0 Then
    If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value Then
      For Each c As DataGridViewCell In DataGridView1.Rows(e.RowIndex).Cells
        c.Style.BackColor = Color.Black
      Next
    Else
      For Each c As DataGridViewCell In DataGridView1.Rows(e.RowIndex).Cells
        c.Style.BackColor = Color.White
      Next
    End If
  End If
End Sub


我得到了答案单元格内容单击,我创建了该单元格的一个对象,并进行了进一步的编码.
I got the answer on cell content click i made an object of that cell and coded further like.....
Dim oCheck As DataGridViewCheckBoxCell
oCheck = datagrid.CurrentRow.Cells("colunmname")







If oCheck.EditingCellFormattedValue = True Then
 For iCount = 0 To datagrid.CurrentRow.Cells.Count - 1
  datagrid.CurrentRow.Cells(iCount).Style.BackColor = Color.Teal
 Next iCount
Else
 For iCount = 0 To datagrid.CurrentRow.Cells.Count - 1
  datagrid.CurrentRow.Cells(iCount).Style.BackColor = Color.White
  Next iCount
End If


这篇关于数据网格视图事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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