locked
formatting data grid RRS feed

  • Question

  • User-578779873 posted

    Hello I hope there is an easy answer to this. I have a data grid that gets data from sql. I want to change how the columns are laid out. Attached is an example of what I would like to do. Any help would be appreciated. Thank you

    Thursday, July 13, 2017 4:30 AM

All replies

  • User-1716253493 posted

    Afaik,  you can use TemplateField, you can design your own headertemplate, itemtemtemplate, edititemtemplate, footertemplate etc

    Thursday, July 13, 2017 7:06 AM
  • User-271186128 posted

    Hi quar1nteen ,

    I suggest you use Repeater control. Then you could easily format the header and item as a custom table.
    Please see the example below:

           <div>
               <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:TestDbConnectionString %>" SelectCommand="SELECT * FROM Info"/>
               <asp:Repeater ID="Repeater3" runat="server" DataSourceID="SqlDataSource4">
                   <HeaderTemplate>
                       <table border="1">
                           <tr>
                               <th>First Name</th>
                               <th rowspan="2">DOB</th>
                               <th rowspan="2">Age</th>
                               <th rowspan="2">Weight</th>
                               <th>Address</th>
                           </tr>
                           <tr>
                               <th>Last Name</th>
                               <th>Phone Number</th>
                           </tr>                   
                   </HeaderTemplate>
                   <ItemTemplate>
                       <tr>
                           <td><%# Eval("First Name") %></td>
                           <td rowspan="2"><%# string.Format("{0:d}", Eval("DOB"))%></td>
                           <td rowspan="2"><%# Eval("Age") %></td>
                           <td rowspan="2"><%# Eval("Weight") %></td>
                           <td><%# Eval("Address") %></td>
                       </tr>
                       <tr>
                           <td><%# Eval("Last Name") %></td>
                           <td><%# Eval("Phone Number") %></td>
                       </tr>
                   </ItemTemplate>
               </asp:Repeater>
           </div>
    

    The SQL table as below:

    The result as below:

    Best regards,
    Dillion

    Friday, July 14, 2017 3:21 AM