【问题标题】:Finding the cell Value from Grid view in vb.net , front end asp.net从 vb.net 中的网格视图中查找单元格值,前端 asp.net
【发布时间】:2021-12-13 15:30:00
【问题描述】:

我现在被这个问题困扰了很长时间。 我需要从 Double 格式的网格视图表中获取值。 我使用 Asp.net 作为前端,使用 Vb.net 编写后端代码

 Dim tmpMax As Double
 tmpMax = CDbl(Me.Dgv_Result1.Rows(1).Cells(columnName).value)

我来了

*Value is not a member of System.web.UI.webcontrols.TableCell*

嗯,可能我试过这个:

tmpMax = Me.Dgv_Result1.Rows(1).Cells(columnName).ToString

这样,一旦我得到字符串中的值,我就可以将其转换为 Double。 但在这里我得到的是实际的列名,而不是该列名的值。

如果社区中的任何人都可以为我解决这个问题,那就太好了。

提前谢谢:)

【问题讨论】:

    标签: c# asp.net vb.net datagridview


    【解决方案1】:

    这将取决于您如何制作网格。

    如果您使用默认(自动生成列),或者您使用数据绑定字段,那么您可以使用单元格。但是,如果您有自定义模板字段,则必须使用查找控件。

    仅供参考:单元格仅按列号 - 您不能使用列名!!!

    这是一个简单的网格。我们将显示整数“ID”列两次。我会这样做,这样你就可以看到如何通过两种方式获得价值。

    因此,如何获取值取决于您在网格中使用的字段模板类型。

    快速总结: 如果您使用单元格集合,那么您只能使用列号(抱歉,不允许使用列名 - 总是觉得这令人失望!!!)。但是,您可以编写一个自定义函数,让您按名称获取 cells()。

    但是,至少让我们弄清楚何时必须使用单元格,以及何时必须使用查找控件。

    所以,这个网格有一个混合。

              <asp:GridView ID="GVHotels" runat="server" class="table" AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField DataField="ID"   HeaderText="PK ID"  />
                    <asp:BoundField DataField="FirstName"   HeaderText="FirstName"  />
                    <asp:BoundField DataField="LastName"    HeaderText="LastName"   />
                    <asp:BoundField DataField="City"        HeaderText="City"       />
                    <asp:BoundField DataField="HotelName"   HeaderText="HotelName" HeaderStyle-Width="200"   />
                    <asp:BoundField DataField="Description" HeaderText="Description" />
    
                    <asp:TemplateField HeaderText="Active"  ItemStyle-HorizontalAlign="Center"  >
                        <ItemTemplate>
                            <asp:CheckBox ID="ckActive" runat="server" 
                                Checked='<%# Eval("Active") %>'  CssClass="bigcheck" />
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="ID AGAIN"   >
                        <ItemTemplate>
                            <asp:TextBox ID="txtID" runat="server" 
                                Text = '<%# Eval("ID") %>'  />
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="Show values"   >
                        <ItemTemplate>
                            <asp:Button ID="Button2" runat="server" Text="Row Click" OnClick="Button2_Click" />
                        </ItemTemplate>
                    </asp:TemplateField>
    
    
                </Columns>
            </asp:GridView>
    

    注意在这个例子中,我如何显示“ID”两次!!!

    所以,我们填充这个网格的代码是这样的:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        If Not IsPostBack Then
    
            Dim strSQL As String = 
                "SELECT * FROM tblHotels ORDER BY HotelName"
    
            GVHotels.DataSource = MyRst(strSQL)
            GVHotels.DataBind()
    
        End If
    
    End Sub
    
    Public Function MyRst(strSQL As String) As DataTable
    
        Dim rstData As New DataTable
        Using MyCon As SqlConnection = New SqlConnection(My.Settings.TEST3)
            Using cmdSQL As New SqlCommand(strSQL, MyCon)
                cmdSQL.Connection.Open()
                rstData.Load(cmdSQL.ExecuteReader)
            End Using
        End Using
        Return rstData
    
    End Function
    

    我们现在有了这个:

    好的,对于我们的行点击事件,我们有这个:

    Protected Sub Button2_Click(sender As Object, e As EventArgs)
    
        Dim btn As Button = sender
        Dim gRow As GridViewRow = btn.Parent.Parent
    
        ' get ID (non template - cells colleciton
    
        Dim intID As Integer
        intID = gRow.Cells(0).Text
        Debug.Print("ID from cells collection = " & intID)
    
        ' get id from template field (item template).
    
        Dim tBox As TextBox = gRow.FindControl("txtID")
    
        intID = tBox.Text
        Debug.Print("Id from text box on grid row = " & intID)
    
    
    End Sub
    

    输出:

    ID from cells collection = 73
    Id from text box on grid row = 73
    

    因此对于自动生成的列或非模板化控件(数据字段)。

    我们必须对列进行计数并使用一个数字来收集单元格(如上所述,您不能使用列名)。

    对于模板化控件,您必须使用 findcontrol,并且不能使用单元格集合。

    因此,请快速查看您的 gridview 标记,具体取决于您使用的是自动生成的字段还是数据绑定字段(然后您使用单元格,并且仅使用数字列号)。

    如果您在网格中有实际的 asp.net 标准控件,则必须使用查找控件。并拉高价值。注意如何有一个复选框,所以我们必须定义一个控件 CheckBox,并使用 MyCheckBox.Checked 来获取复选框值。但同样,如果我们使用数据绑定字段,那么我们使用 cells() 集合。

    【讨论】:

    • 非常感谢您的快速回复。我会遵循这个并尝试解决它。​​
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    相关资源
    最近更新 更多