Update control from .NET DataGrid on row select

90 views Asked by At

I have what seems on the surface a really simple requirement. I want to update a label with a value from the data source when a user clicks a field in a datagrid control.

My page has an SqlDataSource control on it, which returns (for example) 6 columns. I display 5 of them in the datagrid, and want the sixth column shown in a label when the user selects a row.

I have tried various things, with limited success. One way I was convinced would work was to include the column in the datagrid, but set it to Visible="false". However, it turns out that if you do this, the value of row(5).text is ""... Not what I expected.

Any quick way of achieving this?

-- EDIT - Added in code samples --

<asp:GridView ID="gridL250Tickets" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Ridge" BorderWidth="2px" Caption="Last 250 Tickets" CellPadding="3" CellSpacing="1" DataKeyNames="TICKETID" DataSourceID="sqlSlxL250Tickets" AllowPaging="True" AllowSorting="True" HorizontalAlign="Center" Width="75%" PageSize="6">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="SUBJECT" HeaderText="Ticket" SortExpression="SUBJECT" />
        <asp:BoundField DataField="RECEIVEDDATE" DataFormatString="{0:d}" HeaderText="Received" SortExpression="RECEIVEDDATE" />
        <asp:BoundField DataField="COMPLETEDDATE" DataFormatString="{0:d}" HeaderText="Completed" SortExpression="COMPLETEDDATE" />
        <asp:BoundField DataField="AREA" HeaderText="Area" SortExpression="AREA" />
        <asp:BoundField DataField="CATEGORY" HeaderText="Category" SortExpression="CATEGORY" />
        <asp:BoundField DataField="ISSUE" HeaderText="Issue" SortExpression="ISSUE" />
        <asp:BoundField DataField="notes1" HeaderText="Notes" SortExpression="notes1" Visible="False" />
        <asp:BoundField DataField="USERNAME" HeaderText="Ass. To" SortExpression="USERNAME" />
        <asp:BoundField DataField="notes" HeaderText="Notes - l" SortExpression="notes" Visible="false" />
    </Columns>
    <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
    <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
    <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#594B9C" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

VB Code:

Private Sub gridL250Tickets_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gridL250Tickets.SelectedIndexChanged

    Dim row As GridViewRow = gridL250Tickets.SelectedRow
    Label1.Text = row.Cells(9).Text

End Sub
2

There are 2 answers

0
j.f. On BEST ANSWER

You could use a hidden <asp:TemplateField> with a label instead. I tested that out and it seems to work.

<asp:GridView ID="gridL250Tickets" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Ridge" BorderWidth="2px" Caption="Last 250 Tickets" CellPadding="3" CellSpacing="1" DataKeyNames="TICKETID" DataSourceID="sqlSlxL250Tickets" AllowPaging="True" AllowSorting="True" HorizontalAlign="Center" Width="75%" PageSize="6">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="SUBJECT" HeaderText="Ticket" SortExpression="SUBJECT" />
        <asp:BoundField DataField="RECEIVEDDATE" DataFormatString="{0:d}" HeaderText="Received" SortExpression="RECEIVEDDATE" />
        <asp:BoundField DataField="COMPLETEDDATE" DataFormatString="{0:d}" HeaderText="Completed" SortExpression="COMPLETEDDATE" />
        <asp:BoundField DataField="AREA" HeaderText="Area" SortExpression="AREA" />
        <asp:BoundField DataField="CATEGORY" HeaderText="Category" SortExpression="CATEGORY" />
        <asp:BoundField DataField="ISSUE" HeaderText="Issue" SortExpression="ISSUE" />
        <asp:BoundField DataField="notes1" HeaderText="Notes" SortExpression="notes1" Visible="False" />
        <asp:BoundField DataField="USERNAME" HeaderText="Ass. To" SortExpression="USERNAME" />
        <%--<asp:BoundField DataField="notes" HeaderText="Notes - l" SortExpression="notes" Visible="false" />--%>
        <asp:TemplateField Visible="false">
            <ItemTemplate>
                <asp:Label ID="lblNotes" runat="server" Text='<%# Eval("notes") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

    //...

Then just find the label in your event and use its text.

Private Sub gridL250Tickets_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gridL250Tickets.SelectedIndexChanged

    Dim row As GridViewRow = gridL250Tickets.SelectedRow
    Dim lblNotes As Label = row.FindControl("lblNotes")
    Label1.Text = lblNotes.Text

End Sub
0
CJ Williams On

Instead of setting Visible = "False", try setting the actual CSS of the given column. Display = "none", and switch it back when the grid control is clicked.

Something along the lines of lblNotes.Style.Item("display") = "none". You could also do that in the Javascript if you didn't want to have a pageback.