Link to home
Start Free TrialLog in
Avatar of bibmed2
bibmed2Flag for Poland

asked on

Change GridView to Repeater

Hello,

I`d like to change my existing GridView to Repeater. Below is a code:

<asp:GridView ID="GridViewMoreNews" runat="server" DataSourceID="SqlDataSourceMoreNews" AutoGenerateColumns="False" ShowHeader="False" GridLines="None">
    <Columns>      
      <asp:TemplateField ItemStyle-CssClass="longBar text11">
        <ItemTemplate>
          <font id="fontImportance" runat="server" ondatabinding="checkImportance"><%# Eval("longNews") %></font>      
              <div id="addNewsText">Data dodania: <%# Eval("addDate", "{0:dd/MM/yyyy}") %></div>
          <br />
        </ItemTemplate>                
      </asp:TemplateField>
    </Columns>  
  </asp:GridView>
  </div>
<asp:SqlDataSource ID="SqlDataSourceMoreNews" runat="server" ConnectionString="<%$ ConnectionStrings:bmWebPageConnectionStringReader %>" SelectCommand="SELECT [idNews], [longNews], [addDate], [importance], [position], [status] FROM [news] WHERE [status] = 1 ORDER BY [addDate] DESC"></asp:SqlDataSource>

Code behind:
    protected void checkImportance(object sender, EventArgs e)
    {
        HtmlGenericControl font = (HtmlGenericControl)sender;
        GridViewRow row = (GridViewRow)font.NamingContainer;

        int colorCode = Int32.Parse(DataBinder.Eval(row.DataItem, "importance").ToString());

        if (colorCode == 1)
            font.Attributes.CssStyle.Add("color", "red");
   
    }

Simply I change row color according to "importance"

Thanks in advance.
Avatar of MogalManic
MogalManic
Flag of United States of America image

I don't know why you would want a repeater, but here it is
(What is wrong with rendering as a gridview?)
<asp:Repeater ID="RepeaterViewMoreNews" runat="server">
    <HeaderTemplate><table></HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                  <font id="fontImportance" runat="server" ondatabinding="checkImportance"><%# Eval("longNews") %></font>       
                      <div id="addNewsText">Data dodania: <%# Eval("addDate", "{0:dd/MM/yyyy}") %></div>
                  <br />
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate></table></FooterTemplate>
</asp:Repeater>

Open in new window

Avatar of bibmed2

ASKER

This line  int colorCode = Int32.Parse(DataBinder.Eval(row.DataItem, "importance").ToString());

exactly row.DataItem
use Page.GetDataItem() to get the current item being binded.
Avatar of bibmed2

ASKER

Could You give me full code because I`m new in this subject.
Here it is:
(BTW why do you want a repeater instead of a gridview?  What problem are you trying to solve?)

  protected void checkImportance(object sender, EventArgs e)
    {
        HtmlGenericControl font = (HtmlGenericControl)sender;
        GridViewRow row = (GridViewRow)font.NamingContainer;
 
        int colorCode = Convert.ToInt32((DataBinder.Eval(Page.GetDataItem(), "importance"));
 
        if (colorCode == 1)
            font.Attributes.CssStyle.Add("color", "red");
   
    }

Open in new window

Avatar of bibmed2

ASKER

Thanks for reply.

The repeater has SeparatorTemplate which I need.

When I use the code above I`ve got an error:


Unable to cast object of type 'System.Web.UI.WebControls.RepeaterItem' to type 'System.Web.UI.WebControls.GridViewRow'. 
 
GridViewRow row = (GridViewRow)font.NamingContainer;

Open in new window

Avatar of bibmed2

ASKER

So I change it to
Repeater row = (Repeater)font.NamingContainer;

and now I have error:

Unable to cast object of type 'System.Web.UI.WebControls.RepeaterItem' to type 'System.Web.UI.WebControls.Repeater'. 
 
Repeater row = (Repeater)font.NamingContainer;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of MogalManic
MogalManic
Flag of United States of America image

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
Avatar of bibmed2

ASKER

You are absolutely right. Thank You very much for help.