代码在首次使用后不断添加其他空列 [英] Code keeps adding additional null columns after first use
问题描述
所以我终于让我的代码接近我想要它做的事,感谢理查德。现在的问题是代码不断向我添加的每个新行添加额外的空列。如果我第一次添加2张图片一切都很好,但是一旦我添加第二行有2张图片,它会增加2个额外的空列插槽,即使第一行和第二行有2张图片。
So I have finally got my code to do close to what I want it to do, thanks to Richard. Problem is now is that the code keeps adding additional null columns to each new row that I add. If I add 2 pictures the first time everything is good, but once I add a second row with 2 pictures it adds 2 additional null column slots, even though there are 2 pictures in the first and in the second row.
ofd.Multiselect = True
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim fileCount = ofd.FileNames.Length
Dim images(fileCount - 1) As Image
For i As Integer = 0 To fileCount - 1
Dim col As New DataGridViewImageColumn()
DataGridView1.Columns.Add(col)
images(i) = Image.FromFile(ofd.FileNames(i))
Dim pic As New PictureBox()
pic.Image = images(i)
pic.SizeMode = PictureBoxSizeMode.StretchImage
pic.SetBounds(wid, 20, 200, 100)
AddHandler pic.Click, AddressOf convertPic
Me.Panel1.Controls.Add(pic)
wid += 205
Next
PictureBox1.Image = images(fileCount - 1)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
DataGridView1.Rows.Add(images)
End If
解决这个小困境的任何帮助都会有很大帮助。
我尝试过的事情:
并不多因为我不确定如何更改它以便添加适当数量的列。
Any help resolving this small dilemma would be of great assistance.
What I have tried:
not much cause I am unsure exactly how to change it so that it adds the appropriate amount of columns.
推荐答案
你错过了if
阻止我的解决方案 [ ^ ],我在您(现已删除)评论后更新:
You've missed anif
block from my solution[^], which I updated after your (now deleted) comment:
If i >= DataGridView1.Columns.Count Then
Dim col As New DataGridViewImageColumn()
col.Width = 200
DataGridView1.Columns.Add(col)
End If
For i As Integer = 0 To fileCount - 1
Dim col As New DataGridViewImageColumn()
DataGridView1.Columns.Add(col)
如果这是第二次,那么你向DataGridView添加更多列。因此,如果您已经有三列,并且现在正在插入另外三个文件,那么最终会有六列。每次添加新行时,都需要跟踪网格中当前列的数量。
If this is the second time round, then you are adding more columns to your DataGridView. So if you already have three columns, and are now inserting another three files, you will end up with six columns. You need to keep track of the number of columns currently in the grid each time you add a new row.
这篇关于代码在首次使用后不断添加其他空列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!