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.
.NET ProgrammingC#

Avatar of undefined
Last Comment
bibmed2

8/22/2022 - Mon
MogalManic

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

ASKER
bibmed2

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

exactly row.DataItem
MogalManic

use Page.GetDataItem() to get the current item being binded.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
bibmed2

Could You give me full code because I`m new in this subject.
MogalManic

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

ASKER
bibmed2

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
bibmed2

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
MogalManic

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
bibmed2

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