如何在Datagridview中动态地交换图像的字符串? [英] How to dynamically swap a string for an image in Datagridview?

查看:97
本文介绍了如何在Datagridview中动态地交换图像的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想做的是将字符串true或false更改为资源中的图像(a漂亮的绿色勾号和红色x)。



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



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



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

解决方案

Salvete!好的,经过一些艰苦的痛苦,我想出来了。



首先,我以表格硬编码列结构,而不是动态加载。我不会在csv文件中需要更多的列数据,所以这是我的一个选择。但是这可以让我指定哪些列是图像列,哪些列是文本列,它可以让我在VS的GUI设计器中进行所有这些。



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



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



私人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
如果System.IO.File.Exists(whatfile)= True然后
Dim objReader As New System.IO.StreamReader(whatfile)
Do While objReader.Peek()<> -1
TextLine = objReader.ReadLine()
SplitLine = Split(TextLine,,)
如果SplitLine(0)=false,那么thisobject0 = My.Resources.markfalse否则If SplitLine(0)=trueThen thisobject0 = My.Resources.marktrue Else thisobject0 = My.Resources.blank
如果SplitLine(1)=false,那么thisobject1 = My.Resources.markfalse否则如果SplitLine(1 )=trueThen thisobject1 = My.Resources.marktrue Else thisobject1 = My.Resources.blank
whatgrid.Rows.Add(thisobject0,SplitLine(0),thisobject1,SplitLine(1),SplitLine(2), SplitLine(3))
循环
objReader.Close()
blankNewRow()
Else
MsgBox(文件不存在)
结束If
End Sub


Salvete! In my windows form I have an empty datagridview that contains strings loaded from a csvfile. The datagridview has no initial column collection. There are four columns in the csv file. In the csv file, columns 1 and 2 have values of true orfalse. So far I am able to load the text perfectly.

What I want to do is change the strings "true" or "false" into an image in my resources (a pretty green checkmark and a red x).

I would like to leave the csv file text as it is, and then change the columns dynamically.

Then, when I save the information back to the csv file, the images must be converted to text strings to accomodate the csv.

Thanks to anyone who can help me!

解决方案

Salvete! Okay, after some grueling agony, I figured it out.

First, I hard-coded the column structure in the form, instead of loading it dynamically. I won't ever need more column data in the csv file, so this is an option for me. But this allows me to specify exactly which columns are image columns and which are text columns, and it lets me do all of that in VS's GUI designer.

As I loaded the csv, I created two extra image columns and set their corresponding data values to not visible.

Later, when I save the csv file, I skip the image rows and save what is in the data rows.

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

这篇关于如何在Datagridview中动态地交换图像的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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