简体   繁体   English

根据单元格值将行的值更改为图像

[英]Change value of row to image based on cell value

Hi everyone i am doing a windows form which is created in visual studio 2005, wherein it displays data in datagridview. 大家好,我正在做一个在Visual Studio 2005中创建的Windows窗体,其中它在datagridview中显示数据。 I have a column "colImg" that will display 1 and 0. But i need to display a red image when the value of cell of colImg is 0 and green image when the value is 1. I have a code but the problem is it only displays image which is green but i have a values which is 0. Is there any problem on my code? 我有一列“ colImg”,将显示1和0。但是当colImg的单元格的值为0时,我需要显示红色图像,而当值为1时,我需要显示绿色图像。我有一个代码,但问题是它仅显示绿色的图像,但我的值为0。我的代码有问题吗?

Private Sub grdView_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles grdView.CellFormatting
    If grdView.Columns(e.ColumnIndex).Name.Equals("colImg") Then
        Dim value As Integer
        If TypeOf e.Value Is Integer Then
            value = DirectCast(e.Value, Integer)
            e.Value = My.Resources.Resources.NotYet
        Else
            For i As Integer = 0 To grdView.RowCount
                If value = 0 Then
                    e.Value = My.Resources.Resources.Red

                Else
                    e.Value = My.Resources.Resources.Green
                End If
            Next

        End If
    End If

There are several solutions for your question, I will provide one of them. 您的问题有几种解决方案,我将提供其中一种。

  1. You need two columns in your DataGrid . 您在DataGrid需要两列。
    One is to hold the raw data (0 or 1); 一种是保存原始数据(0或1);另一种是保存原始数据。 in my example I called it colValue . 在我的示例中,我将其称为colValue
    The other is to hold only the image (red or green); 另一种是仅保留图像(红色或绿色); named colImg . 名为colImg
    colValue is not shown in the grid : colValue未显示在网格中:

     'Set colValue invisible which is first column in my example DataGridView1.Columns(0).Visible = False 
  2. Use the CellValueChanged event to set the image of the colImg cell: 使用CellValueChanged事件设置colImg单元格的图像:

     If e.ColumnIndex = 0 AndAlso Not isInit Then 'Value column has changed and we are not in Form Initializing Dim valueCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex) Dim imgCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex + 1) 'Whatever index your colImg is If Integer.Parse(valueCell.Value.ToString()) = 1 Then imgCell.Value = My.Resources.Green Else imgCell.Value = My.Resources.Red End If End If 
    1. To avoid that the event code breaks when the Form and thus the DataGridView is being initilized I created a local variable isInit and set it before and after the initialization: 为了避免在初始化Form并因此初始化DataGridView时事件代码中断,我创建了一个局部变量isInit ,并在初始化之前和之后进行设置:

       Public Class Form1 Private isInit As Boolean Public Sub New() isInit = True InitializeComponent() isInit = False ... End Sub ... End Class 

Sample Data: 样本数据:

 DataGridView1.Rows(0).Cells(0).Value = 1
 DataGridView1.Rows(1).Cells(0).Value = 0
 DataGridView1.Rows(2).Cells(0).Value = 0
 DataGridView1.Rows(3).Cells(0).Value = 1

Outcome: 结果:
在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM