vb.net 如何使用asp vb.net根据条件更改gridview中绑定的单元格值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21174832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change cell value binded in gridview based on condition using asp vb.net?
提问by J-J
I have problem on changing cell value binded in gridview based on condition. The data was binded first to gridview from a csv file. What I want to do is whenever a specific cell on a specific column meets the condition I put, it will change the value of that specific cell.
我在根据条件更改 gridview 中绑定的单元格值时遇到问题。数据首先从 csv 文件绑定到 gridview。我想要做的是每当特定列上的特定单元格满足我放置的条件时,它就会更改该特定单元格的值。
For example, if cell value is equal to "PHP" then it will changed into "Philippines". I tried below codes on Gridview RowDataBound event but nothing happens:
例如,如果单元格值等于“PHP”,则它将更改为“Philippines”。我在 Gridview RowDataBound 事件上尝试了以下代码,但没有任何反应:
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim myVal As String = GridView1.Rows(i).Cells(0).Text
If myVal = "PHP" Then
GridView1.Rows(i).Cells(0).Text = "Philippines"
End If
Next
End Sub
Please help me.
请帮我。
Thanks in advance.
提前致谢。
回答by ekad
If you use RowDataBoundevent for GridView1, then GridView1_RowDataBoundwill be executed for each row, not just once. Here's what you should do instead:
如果您对 GridView1使用RowDataBound事件,则将对每一行GridView1_RowDataBound执行,而不仅仅是一次。这是你应该做的:
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim myVal As String = e.Row.Cells(0).Text
If myVal = "PHP" Then
e.Row.Cells(0).Text = "Philippines"
End If
End If
End Sub
回答by Nefariis
If it's bound to a CSV you have two options:
如果它绑定到 CSV,您有两个选择:
- Either change the data in the CSV and rebind it or
- Instead of binding the table to the CSV, just input the data from the CSV and then you will be able to change the table independently from the CSV.
- 更改 CSV 中的数据并重新绑定它或
- 无需将表格绑定到 CSV,只需从 CSV 输入数据,然后您就可以独立于 CSV 更改表格。
If you want to change both the CSV and the table choose option 1. If you don't care about the CSV and or want to create a separate output then choose option 2.
如果您想同时更改 CSV 和表格,请选择选项 1。如果您不关心 CSV 和/或想要创建单独的输出,请选择选项 2。
*edited to add some code
*编辑添加一些代码
You just read it into the table like normal. Since it's a CSV I would personally just change it to a .txt file to make it easier. Then:
您只需像往常一样将其读入表格。由于它是 CSV,我个人会将其更改为 .txt 文件以使其更容易。然后:
dim reader as string() = input.txt
for x as integer = 0 to reader.length -1
dim split as string() = reader(x).split(","c)
for y as integer from 0 to split.length - 1
datagridview.cells(y+1,x+1) = split(y)
next
next
This is untested, but it should go together something along these lines (I don't know the format of the CSV). If you have uneven rows it would probably be better if you did something like this
这是未经测试的,但它应该按照这些方式组合在一起(我不知道 CSV 的格式)。如果你有不均匀的行,如果你做这样的事情可能会更好
for each str as String in Split
add to datagrid...
next

