如何在特定的datagridview列中插入不同的值 [英] How to insert different values in a specific datagridview Column

查看:75
本文介绍了如何在特定的datagridview列中插入不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

btn_Click()

For Each row As DatagridviewRow In DGV.Rows
    row.Cells("Column2").Value = "Boy "
    row.Cells(Column2").Value = "Girl"
    row.Cells(Column2").Value = "Boy"
    row.Cells(Column2").Value = "Girl"
    row.Cells(Column2").Value = "Girl"
Next





这是代码,但是当我执行代码时,它只插入第一个值为Boy的值。但我想插入不同的值



This is the code but when I execute the code it inserts only the first value which is "Boy" throughout. But I want to insert different values

推荐答案

您发布的代码甚至无法编译,因为您有基于字符串的标识符,其中包含不匹配的引号。



删除那些,是的,你的代码会将男孩放在每一行,因为这就是你告诉代码要做的事情。您没有更改为每行中的不同值。你没有任何关于任何其他值的代码。



如果你想在每一行中加入一个不同的值,你必须在循环中包含代码确定要在循环指向的行中放置哪个值或者您需要删除循环并为DGV中的每一行显式放置一行代码,以便在该行的相应列中设置其值。
The code you posted won't even compile as you have string-based identifiers with mismatch quote marks.

Removing those, yeah, your code would put "Boy" in every row because that's what you're telling the code to do. You're not changing to different values in each row. You have no code that is saying anything about any other values.

If you want to put a different value in each row, you either have to have code inside the loop that determines which value to place in the row pointed to by the loop OR you need to remove the loop and explicitly put a line of code for each row in the DGV to set its value in the appropriate column for that row.


它看起来你的代码不会按预期工作,因为你在每一行上都有特定单元格的循环,它将导致在所有单元格中只设置一个值。您需要按如下方式更改代码

it looks your code will not work as expected, cause you have throw loop on each row with specific cell, it will result in only one value set in all cell. you need to change your code as follows
DGV.Rows(0).Cells(2).Value = "Boy"
DGV.Rows(1).Cells(2).Value = "Girl"
DGV.Rows(2).Cells(2).Value = "Boy"
DGV.Rows(3).Cells(2).Value = "Girl"
DGV.Rows(4).Cells(2).Value = "Girl"


这篇关于如何在特定的datagridview列中插入不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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