如何在Datagridview中动态交换图像的字符串?

时间:2011-12-24 05:20:40

标签: vb.net datagridview

Salvete!在我的Windows窗体中,我有一个空datagridview,其中包含从csvfile加载的字符串。 datagridview没有初始列集合。 csv文件中有四列。在csv文件中,第1列和第2列的值为truefalse。到目前为止,我能够完美地加载文本。

我想要做的是将字符串“true”或“false”更改为我的资源中的图像(漂亮的绿色复选标记和红色x)。

我想保留csv文件文本,然后动态更改列。

然后,当我将信息保存回csv文件时,必须将图像转换为文本字符串以容纳csv。

感谢任何可以帮助我的人!

1 个答案:

答案 0 :(得分:1)

Salvete!好吧,在经历了一些艰苦的痛苦之后,我想通了。

首先,我对表单中的列结构进行了硬编码,而不是动态加载它。我不会在csv文件中需要更多的列数据,所以这是我的选择。但这允许我准确指定哪些列是图像列,哪些列是文本列,它允许我在VS的GUI设计器中完成所有这些。

当我加载csv时,我创建了两个额外的图像列并将其相应的数据值设置为不可见。

稍后,当我保存csv文件时,我会跳过图像行并保存数据行中的内容。

Private Sub ImportCSV(ByVal whatgrid As DataGridView, ByVal whatfile As String)
    Dim TextLine As String = ""
    Dim SplitLine() As String
    whatgrid.Rows.Clear()
    Dim thisobject0 As Object
    Dim thisobject1 As Object
    If System.IO.File.Exists(whatfile) = True Then
        Dim objReader As New System.IO.StreamReader(whatfile)
        Do While objReader.Peek() <> -1
            TextLine = objReader.ReadLine()
            SplitLine = Split(TextLine, ",")
            If SplitLine(0) = "false" Then thisobject0 = My.Resources.markfalse Else If SplitLine(0) = "true" Then thisobject0 = My.Resources.marktrue Else thisobject0 = My.Resources.blank
            If SplitLine(1) = "false" Then thisobject1 = My.Resources.markfalse Else If SplitLine(1) = "true" Then thisobject1 = My.Resources.marktrue Else thisobject1 = My.Resources.blank
            whatgrid.Rows.Add(thisobject0, SplitLine(0), thisobject1, SplitLine(1), SplitLine(2), SplitLine(3))
        Loop
        objReader.Close()
        blankNewRow()
    Else
        MsgBox("File Does Not Exist")
    End If
End Sub