0

我的GridView填充如下:

<asp:Panel ID="pnlGrid" runat="server">
    <div class="m_container" style="margin-bottom:20px;">
        <asp:GridView ID="grdView" runat="server" CssClass="GridViewStyle"
            AutoGenerateColumns="False" GridLines="None" Width="125%" onrowdatabound="builderGridView_RowDataBound" >
            <Columns>
                <asp:BoundField DataField="id" Visible="False" />
                <asp:BoundField HeaderText="Info" />
                <asp:TemplateField>
                  <ItemTemplate>
                    <asp:LinkButton name='<%#Eval("status") %>' CommandArgument='<%#Eval("id")%>' runat="server" Text='<%#Eval("status")%>' CommandName='<%#Eval("status")%>' ID="statusLink" />
                  </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <RowStyle CssClass="RowS" />
            <EmptyDataRowStyle CssClass="EmptyS" />
            <PagerStyle CssClass="PagerStyle" />
            <SelectedRowStyle CssClass="SelectedS" />
            <HeaderStyle CssClass="HeaderS" />
            <EditRowStyle CssClass="EditRowS" />
            <AlternatingRowStyle CssClass="AltRowS" />
        </asp:GridView>
    </div>
</asp:Panel>
</asp:Content>

后面的代码是:

Private Sub LoadData(ByRef theStatus As Integer)
    Dim objConn As MySqlConnection
    Dim objCmd As MySqlCommand

    objConn = New MySqlConnection(strConnString)
    objConn.Open()

    Dim strSQL As String
    strSQL = "SELECT * FROM theTable WHERE status=" & theStatus & " ORDER BY created_on, status DESC;"

    Dim dtReader As MySqlDataReader
    objCmd = New MySqlCommand(strSQL, objConn)
    dtReader = objCmd.ExecuteReader()

    grdView.DataSource = dtReader
    grdView.DataBind()

    dtReader.Close()
    dtReader = Nothing

    objConn.Close()
    objConn = Nothing
End Sub

然后在上面我为每一行创建一个LinkBut​​ton ,如下所示:

Sub builderGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim lbtn As LinkButton = DirectCast(e.Row.FindControl("statusLink"), LinkButton)

        e.Row.Cells(1).Text = "TEST!"

        If lbtn.Text = 0 Then
            Dim lb As New LinkButton()
            lb.Text = "Accept"
            lb.CommandName = lbtn.CommandName
            lb.CommandArgument = lbtn.CommandArgument
            lb.Attributes.Add("class", "nounderline")
            lb.ForeColor = System.Drawing.Color.Green
            lb.Font.Bold = True
            lb.Font.Size = 12

            lb.OnClientClick = "location.href = '/page.aspx?ID=" & pageID & "&dbid=" & lb.CommandArgument & "&ACCEPT=yes'; return false;"
            e.Row.Cells(4).Controls.Add(lb)
        End If
    End If
End Sub

如何从数据库中获取值(姓名、地址、电子邮件地址等)并将其放在当前行中?

例子:

(当它遍历行时)

e.Row.Cells(1).Text = database("FName") & "<BR />" & database("LName") & "<BR />"...etc etc

在第一行列中看起来像这样:

Bob
Barker

我怎样才能从数据库中获取当前正在读取的内容?

4

1 回答 1

2

builderGridView_RowDataBound 事件填充被处理来自数据库的每一行。

e.Row.DataItem 将包含来自数据库的项目

编辑:更好的方法是

Using adap As New MySqlDataAdapter(objCmd)
    Dim table As New DataTable()
    adap.Fill(table)
grdView.DataSource = table;
grdview.DataBind();

Adapter 会将所有记录加载到 DataTable。

然后你的 e.Row.DataItem 将是 DataRow 类型

于 2012-09-11T20:16:00.777 回答