[英]Get column value from a gridview using VB.net
I have in my GridView the Column(Validate), this column in contains two values 'v' or 'notv' I want to get the value ' v ' from myGridview and change it to ' VALIDATE ' and the other ' notv ' to ' Not validate '.. I have Tried GridView1.Rows(i).Cells(10).Text
But nothing has been changed when my Gridview is displayed. 我在GridView中有Column(Validate),这个列包含两个值'v'或'notv'我想从myGridview获取值' v '并将其更改为' VALIDATE '而另一个' notv '更改为' 不验证 '..我试过
GridView1.Rows(i).Cells(10).Text
但是当我的Gridview显示时没有任何改变。
Here is my code: 这是我的代码:
The GridView is displyed whan I click on Button: 我点击Button时显示GridView:
<asp:GridView ID="GridView1" runat="server" CssClass="table" GridLines="None">
</asp:GridView>
'page.aspx.vb “page.aspx.vb
cmd.Connection = cx
cmd.CommandText = "select .., validate from table"
da = New SqlDataAdapter(cmd)
da.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
For i = 0 To GridView1.Rows.Count - 1
GridView1.Rows(i).Cells(0).ForeColor = System.Drawing.Color.Blue
If GridView1.Rows(i).Cells(10).Text = "v" Then
GridView1.Rows(i).Cells(10).Text = "VALIDATE"
GridView1.Rows(i).Cells(10).ForeColor = System.Drawing.Color.Green
ElseIf GridView1.Rows(i).Cells(10).Text = "Notv" Then
GridView1.Rows(i).Cells(10).Text = "Not VALIDATE"
GridView1.Rows(i).Cells(10).ForeColor = System.Drawing.Color.Red
End If
Next
Thank you for your Time! 感谢您的时间!
firstly this should be on rowdatabound event 首先,这应该是在rowdatabound事件上
and then you can try with trimming the spaces, could be it creating problem 然后你可以尝试修剪空间,可能会造成问题
If GridView1.Rows(i).Cells(10).Text = "v" Then
GridView1.Rows(i).Cells(10).Text = "VALIDATE"
change to 改成
If GridView1.Rows(i).Cells(10).Text.Trim() = "v" Then
GridView1.Rows(i).Cells(10).Text = "VALIDATE"
also check for the header row in rowdatabound event 还检查rowdatabound事件中的标题行
if gridview is having bound filed then it should work, if you are using templates, then you should use find control to find the specific control and then change its value 如果gridview有绑定字段然后它应该工作,如果你使用模板,那么你应该使用查找控件来找到特定的控件,然后更改其值
It works for me just Like that without using the RowDataBound: 这对我来说就像没有使用RowDataBound那样:
Here is the code behind: 这是后面的代码:
For i = 0 To GridView1.Rows.Count - 1
GridView1.Rows(i).Cells(0).ForeColor = System.Drawing.Color.Blue
If GridView1.Rows(i).Cells(10).Text = "v" Then
GridView1.Rows(i).Cells(10).Text = "VALIDATE"
GridView1.Rows(i).Cells(10).ForeColor = System.Drawing.Color.Green
ElseIf GridView1.Rows(i).Cells(10).Text = "Notv" Then
GridView1.Rows(i).Cells(10).Text = "Not VALIDATE"
GridView1.Rows(i).Cells(10).ForeColor = System.Drawing.Color.Red
End If
Next
Thank you all for your answer! 谢谢大家的回答!
Try making your changes in GridView's RowDataBound event eg 尝试在GridView的RowDataBound事件中进行更改,例如
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
'Do stuff with e.Row.Cells(0)
'Do stuff with e.Row.Cells(10)
'etc...
End Sub
It fires for every row bound to your datatable so you don't have to loop thru rows. 它会为绑定到数据表的每一行触发,因此您不必循环遍历行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.