2011-12-24 50 views
0

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

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

我想离开csv文件原样,然后动态更改列。

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

感谢任何能够帮助我的人!

回答

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