Вопрос

I have my GridView populating like so:

<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>

And the code behind is:

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

Then after the above i create a LinkButton for each row like so:

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

How can i get values from the database (name, address, email address, etc etc) and place it within the current row its going through?

example:

(when its looping through the rows)

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

Would look like this in the first row column:

Bob
Barker

How can i grab whats currently being read from the DB?

Это было полезно?

Решение

builderGridView_RowDataBound event fill be handled for each row from the database.

e.Row.DataItem will contain the item from the database

Edit: Better way would be

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

Adapter will load all records to the DataTable.

Then your e.Row.DataItem will be of type DataRow

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top