p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   .NET Framework 2.0 (http://p2p.wrox.com/forumdisplay.php?f=141)
-   -   datagrid w/checkbox highlight row onchange (http://p2p.wrox.com/showthread.php?t=16454)

fitchic77 August 5th, 2004 11:30 AM

datagrid w/checkbox highlight row onchange
 
I need to highlight a row once I've clicked on a checbox...

Anbody have any ideas?

bmains August 5th, 2004 12:16 PM

Hey,

Select the row by setting the SelectedIndex of the datagrid, or of the DataGridItem.

Brian

bmains August 5th, 2004 12:17 PM

Oh, this requires style information setup for the SelectedItem template.

Brian

fitchic77 August 5th, 2004 01:40 PM

Here is the solution when you click on a checkbox it will highlight the row. You can change the colors to your liking in the .aspx file (the four lines of code prior to the "COLUMN" heading). Conversely, when you uncheck the rows, it will conver the colors back to what you set originally.

.aspx
=====================
<div align="center">
    <asp:Label ID="lblStatus" Font-Name="verdana" Font-Size="12px" ForeColor="#ff0000" Runat="server" />
</div>
<ASP:DATAGRID id="MyDataGrid" runat="server" AutoGenerateColumns="false" HeaderStyle-Font-Size="7pt" HeaderStyle-Font-Bold="True" HeaderStyle-BackColor="#afd9e9" Font-Size="7pt" Font-Name="Verdana" ForeColor="#082083" CellSpacing="1" CellPadding="1" BorderWidth="1" BorderColor="white">
    <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#2262BA"></SelectedItemStyle>
    <ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="#082083" BackColor="#afd9e9"></HeaderStyle>
    <FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
    <Columns>
        <asp:TemplateColumn HeaderStyle-HorizontalAlign="Center" HeaderText="Document" HeaderStyle-Width="270px">
            <ItemTemplate>
                <asp:TextBox id=Document style="TEXT-ALIGN: left; font-size: 8pt" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Document") %>' Width="270px" />
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderStyle-HorizontalAlign="Center" HeaderText="Description" HeaderStyle-Width="380px">
            <ItemTemplate>
                <asp:TextBox id="Description" style="TEXT-ALIGN: left; font-size: 8pt" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' Width="380px" />
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderStyle-HorizontalAlign="Center" HeaderText="Add" HeaderStyle-Width="350px">
            <ItemTemplate>
                <asp:Checkbox ID="chkSelected" Runat="server" OnCheckedChanged="highlight_row_CheckedChanged" AutoPostBack="true" />
                <asp:Label ID="txtDocumentID" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "Document") %>' Runat=server />
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</ASP:DATAGRID>
<br>
<br>
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
    <tr>
        <td align="right">
            <table width="20%" id="Table3" cellSpacing="2" cellPadding="2" border="0">
                <tr>
                    <td align="right"><asp:Button id="cmdNew" runat="server" Width="48px" Text="New" class="button"></asp:Button></td>
                    <td align="right"><asp:Button id="cmdOK" runat="server" Text="Ok" Width="48px" class="button"></asp:Button></td>
                    <td align="right"><asp:Button id="cmdCancel" runat="server" Width="58px" Text="Cancel" class="button"></asp:Button></td>
                    <td align="right"><asp:Button id="cmdHelp" runat="server" Width="48px" Text="Help" class="button"></asp:Button></td>
                </tr>
            </table>
        </td>
    </tr>
</table>

.aspx.vb (code behind):
================================================== =======
    Public Sub highlight_row_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chkTemp As CheckBox = CType(sender, CheckBox)
        Dim dgi As DataGridItem
        dgi = CType(chkTemp.Parent.Parent, DataGridItem)
        If (chkTemp.Checked) Then
            dgi.BackColor = MyDataGrid.SelectedItemStyle.BackColor
            dgi.ForeColor = MyDataGrid.SelectedItemStyle.ForeColor

        Else
            dgi.BackColor = MyDataGrid.ItemStyle.BackColor
            dgi.ForeColor = MyDataGrid.ItemStyle.ForeColor
        End If
    End Sub


All times are GMT -4. The time now is 08:29 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.