简体   繁体   English

使用VB.net从gridview获取列值

[英]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.

相关问题 尝试使用vb.net在asp.net中获取gridview的当前单元格值 - Trying to get current cell value of gridview in asp.net using vb.net VB.Net - 如何让 GridView 只显示 DateTime SQL 列中的日期? - VB.Net - How to get GridView to only show Date from DateTime SQL column? 使用VB.net的gridView列中的类图标 - class icon in gridView column using VB.net 使用VB.Net获取Gridview中订单ID的行号 - Get Row Number of an Order ID in Gridview using VB.Net 使用 VB.NET 将 Gridview 导出到 Excel - Exporting a Gridview to Excel using VB.NET 在“ RowUpdating”事件ASP.NET/VB中从Gridview中的指定列获取单元格值 - Get cell value from specified column in a Gridview upon “RowUpdating” Event ASP.NET/VB 如何在ASP.Net上使用VB.Net从DataTable获取1列数据并将其显示给Lable - How to get 1 column data from DataTable and display it to Lable using VB.Net on ASP.Net 如何使用asp.net + vb.net从数据库列中获取数据并显示在下拉列表中 - How to get data from database column and display in dropdownlist using asp.net + vb.net 如何使用vb.net将两列相乘并在gridview的另一列中显示 - How to multiply two columns and display that in another column in a gridview using vb.net 如何使用vb.net更新特定日期的gridview的特定列值? - How to update the particular column values of gridview on specific dates using vb.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM