Link to home
Start Free TrialLog in
Avatar of g9support
g9support

asked on

Customizing My Datagrid? Is this possible?

I've searched High and Low, near and far and I've yet to find anything on customizing a data grid so that I can make it look however I want. I'd hate to have to hand program the HTML Table controls and add them to the panel, but if thats what I have to do, then thats what I have to do. But... I assume that you can do this with the Datagrid because its "such a great tool". Sooo... Here's what i'm trying to do.

I have some data in a table, job employment history. It shows old history of someones jobs.
I want the data to display like this: (This is a basic HTML Representation)

------------CODE --------------------------

<table bgcolor="#eae9e7">
<tr valign="top">
      <td align="right"><b>Company Name</b></td><td>ACME Clothing</td>
</tr>
<tr valign="top">
      <td align="right"><b>Position</b></td><td>Screen Printer</td>
</tr>
<tr valign="top">
      <td align="right"><b>Dates Employed</b></td><td>(12/01) - (01/04)</td>
</tr>
<tr valign="top">
      <td align="right"><b>Supervisor Name</b></td><td>John Doe</td>
</tr>
<tr valign="top">
      <td align="right"><b>Salary</b></td><td>25,000</td>
</tr>
<tr valign="top">
      <td align="right"><b>Reponsibilities</b></td><td>Set up screens, burn screens, set up ink and screen print. Manage shop.</td>
</tr>
<tr valign="top">
      <td align="right"><b>Reason For Leaving</b></td><td>Poor Working Conditions</td>
</tr>
</table>
------------------------------ END CODE------------------------------------

But the only thing i've found online is something that looks like this: (this is just s snippet)

-------------------------------CODE----------------------------------

                        <asp:DataGrid id="dgEmpHistory" Runat="server" AutoGenerateColumns="False" BorderWidth="0" CellPadding="10">
                              <AlternatingItemStyle BackColor="#eae9e7"></AlternatingItemStyle>
                              <Columns>
                                    <asp:TemplateColumn>
                                          <ItemTemplate>
                                                <b>Title:</b><%# DataBinder.Eval(Container.DataItem, "job_title") %><br>
                                                <b>Employer:</b><%# DataBinder.Eval(Container.DataItem, "company_name") %><br>
                                                <b>Address:</b><%# DataBinder.Eval(Container.DataItem, "address1") %><br>
                                                <%# DataBinder.Eval(Container.DataItem, "address2") %>
                                                <br>
                                                <b>Phone:</b><%# DataBinder.Eval(Container.DataItem, "phone_number") %><br>
                                          </ItemTemplate>
                                    </asp:TemplateColumn>
                              </Columns>
                        </asp:DataGrid>

---------------------------------------END CODE------------------------------------------


As you can see, the bottom most piece of code will construct one column and i'm breaking the data apart with <Br> Tags. I dont want to do this. I want display it very cleanly in a table format that is listed above.

How can I do this? Is this even possible?

Thanks for the help!

Avatar of AerosSaga
AerosSaga

you would want to use a datalist to get that level of control over the html rendering.
ASKER CERTIFIED SOLUTION
Avatar of AerosSaga
AerosSaga

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or you can add the attributes to the datagrid:
You can control the appearance of the DataGrid control by programmatically adding attributes to the <td> and <tr> tags rendered by the control on the browser. Attributes can be programmatically added by providing code in the event handler for the OnItemCreated or OnItemDataBound event.

To add an attribute to the <td> tag, first get the TableCell object that represents the cell in the DataGrid control you want to add the attribute to. The Control.Controls collection for the Item property of the DataGridItemEventArgs object passed into the event handler can be used to get the desired TableCell object. You can then use the AttributeCollection.Add method of the Attributes collection for the TableCell object to add attributes to the <td> tag.

To add an attribute to the <tr> tag, first get the DataGridItem object that represents the row in the DataGrid control you want to add the attribute to. The Item property of the DataGridItemEventArgs object passed into the event handler can be used to get the desired DataGridItem object. You can then use the AttributeCollection.Add method of the Attributes collection for the DataGridItem object to add attributes to the <tr> tag.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatagridclasstopic.asp
Agree with QAeros [Quick Aeros].. what you need here is DataList.. check out some samples on DataList here also..

http://www.csharpfriends.com/quickstart/aspplus/samples/webforms/ctrlref/webctrl/datalist/doc_datalist.aspx

-tushar
Hi g9support,

i would suggest you the Repeater control, except if you want to rearange the visualization of data - horiozontally/vertically
actually the main function of the repeater is to render information exactly how you want - not generated
see the description of Repeater and DataList

Repeater Displays information from a data source using a set of HTML elements and controls you specify, repeating the elements once for each record in the data set.
DataList Like the Repeater control, but with more formatting and layout options, including the ability to display information in a table. The DataList control also allows you to specify editing behavior.


Regards!
B..M
Avatar of g9support

ASKER

If I need to test a value such as

If i = 1 then
  ' do stuff
else
  'd ont
end if

how would I do that? would I need to play with the dataset a little bit?

Thanks!
It depends upon:
 What is i in i = 1
& what exacly you want to do in ' do stuff..

Based on your requirements you can do that on different level.. either on ItemDataBound event of your DataList or on DataSet..

-tushar
also, start a new question for a different topic for that one.