C# – Working with GridView and ItemTemplates (ASP.net/C#)

asp.netc++gridview

I have a GridView that is populated via a database, inside the GridView tags I have:

<Columns>
  <asp:TemplateField>
    <ItemTemplate><asp:Panel ID="bar" runat="server" /></ItemTemplate>
  </TemplateField>
</Columns>

Now, I want to be able to (in the code) apply a width attribute to the "bar" panel for each row that is generated. How would I go about targeting those rows? The width attribute would be unique to each row depending on a value in the database for that row.

Best Solution

<asp:Panel ID="bar" runat="server" Width='<%# Eval("Width") %>' />

If you like, you can change Eval("Width") to the expression that calculates the width.