Programatically assigning gridviews boundfield datafields?

binici

Active member
Joined
Sep 11, 2006
Messages
26
Programming Experience
Beginner
Hello all:
I hope I can make sense of this. I have exhausted all resources, MSDN and books, but cannot understand or figure how I can programmatically change and write out a different value of a datafield?
One of my datafields returns an on/off switch, (0,1), so depending on the value, I want to then output a string to that qualified datafield. Here is my codebehind:
VB.NET:
Private Function TestDataSet()
Dim iMemberId As String = Request.QueryString("member_id")
Dim connString As String = ConfigurationManager.AppSettings("connectionstring")
Dim con As New SqlConnection(connString)
Dim sql As String = "SELECT rm.recip_id, rm.member_id, rm.mls_id, rm.agent_name, rm.office_name, rm.street_number + ' ' + rm.street_name + ' ' + rm.city + ',' + rm.state AS recip_address, " _
& "rm.expiration_date, rm.received_by, rm.fee, rm.payment_type, rm.check_number, rm.recip_to, rm.recip_date, rm.last_updated, " _
& "rm.recip_type, rm.recip_status, m.Office_Number " _
& "FROM callcenterdev..tRecipMaster rm WITH(NOLOCK) LEFT OUTER JOIN rapdata..Member m WITH(NOLOCK) ON rm.member_id = m.Member_Number " _
& "WHERE rm.member_id = '" & iMemberId & "' "
Dim objDataAdapter As New SqlDataAdapter(sql, connString)
Dim objDataSet As New DataSet()
Dim objConnect As New SqlConnection(connString)
objDataAdapter.Fill(objDataSet, "Recip")
grdResults.DataSource = objDataSet.Tables("Recip").DefaultView
grdResults.DataBind()
Return objDataSet
End Function
here is the aspx:
HTML:
<asp:GridView ID="grdResults" runat="server" Visible="False" AutoGenerateColumns="False" AllowPaging="True" PageSize="3" BorderWidth="1px" CellPadding="2" CellSpacing="2">
<Columns>
<asp:HyperLinkField HeaderText="View" NavigateUrl="/_MembersOnly/RecipListing/RecipListingForm.aspx" 
Text="Listing In" DataNavigateUrlFields="recip_id" >
<ControlStyle CssClass="LinkNormal" />
<HeaderStyle HorizontalAlign="Center" />
</asp:HyperLinkField>
<asp:BoundField DataField="recip_to" HeaderText="To" />
<asp:BoundField DataField="recip_status" HeaderText="Status" />
<asp:BoundField DataField="agent_name" HeaderText="Agent" />
<asp:BoundField DataField="office_name" HeaderText="Office" />
<asp:BoundField DataField="recip_address" HeaderText="Address" />
<asp:BoundField DataField="mls_id" HeaderText="MLS #" />
<asp:BoundField DataField="recip_date" DataFormatString="{0:dd/MM/yyyy}" HeaderText="Date Entered" HtmlEncode="False" />
<asp:BoundField DataField="last_updated" DataFormatString="{0:d}" HeaderText="Last Updated"
HtmlEncode="False" />
</Columns>
</asp:GridView>
Any ideas or help would be appreciated! Thank you!
 
Last edited by a moderator:
I figured this out. I used a CASE statement in the stored procedure to output the string. Also to make it much more easier on the coding side, I switched to a sqldatasource. Also someone mentioned that you can use RowDataBound event to change the text before the data is bounded.
 
I cleaned up the code, perhaps it will make sense.
Here is the gridview with the columns bound to my sqldatasource:
VB.NET:
<asp:GridView ID="RecipHistoryGridView" runat="server" DataSourceID="sqlRecipHistory" AutoGenerateColumns="False" DataKeyNames="recip_id,member_id" DataMember="DefaultView" AllowPaging="True" CssClass="TextSmall" Width="555px" Visible="False" AllowSorting="True" PageSize="5" OnRowDataBound="RecipHistoryGridView_RowDataBound">
    <Columns>
        <asp:HyperLinkField DataTextField="recip_id" HeaderText="View" NavigateUrl="/_MembersOnly/RecipListing/RecipListingForm.aspx"
            SortExpression="recip_id" />
        <asp:BoundField DataField="recip_to" HeaderText="To" SortExpression="recip_to" />
        <asp:BoundField DataField="recip_status" HeaderText="Status" SortExpression="recip_status" />
        <asp:BoundField DataField="agent_name" HeaderText="Agent" SortExpression="agent_name" />
        <asp:BoundField DataField="office_name" HeaderText="Office" SortExpression="office_name" />
        <asp:BoundField DataField="recip_address" HeaderText="Address" SortExpression="recip_address" />
        <asp:BoundField DataField="mls_id" HeaderText="MLS #" SortExpression="mls_id" />
        <asp:BoundField DataField="recip_date" DataFormatString="{0:d}" HeaderText="Entered"
            HtmlEncode="False" SortExpression="recip_date" />
        <asp:BoundField DataField="recip_type" HeaderText="RecipType" SortExpression="recip_type"
            Visible="False" />
    </Columns>
    <HeaderStyle BackColor="#5C6F8D" HorizontalAlign="Center" />
    <AlternatingRowStyle BackColor="#8B9FC4" />
    <PagerSettings Mode="NextPreviousFirstLast" />
    <PagerStyle HorizontalAlign="Center" />
</asp:GridView>
The code behind logic to change the text of the first column if the hidden field value is something:
VB.NET:
Protected Sub RecipHistoryGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles RecipHistoryGridView.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' Change the first column (recip_id) to a status string.
            If e.Row.Cells(8).Text = "1" Then
                e.Row.Cells(0).Text = "Listing In"
            ElseIf e.Row.Cells(8).Text = "0" Then
                e.Row.Cells(0).Text = "Listing Out"
            End If
        End If
    End Sub
The problem is, when the Recip Type column in hidden the logic doesn't work? But when i set the column to visible the logic fires off correctly.
Any ideas?
Thank you!
 
Back
Top