0

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?

StealthRT
  • 10,108
  • 40
  • 183
  • 342
  • The best thing you can do is storing the dtReader in a Session variable. Then in the RowdataBound event filter the dtReader based on the current row and add the fields to the current row.. – Sushanth -- Sep 11 '12 at 20:16
  • Otherwise why don't you use a Template field to populate those fields.. Make sure you get that data from the Query itself. this will make sure all the fields are available when the Grid is being bound.. – Sushanth -- Sep 11 '12 at 20:18
  • I'm customizing how it is formatted. Not just placing the value into the row as-is. – StealthRT Sep 11 '12 at 20:23
  • Then best way is to use a Session and populate the values accordingly – Sushanth -- Sep 11 '12 at 20:31

1 Answers1

2

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

Roland
  • 972
  • 7
  • 15