Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Condition with TemplateField 1

Status
Not open for further replies.

sd110404

Programmer
Nov 3, 2004
63
US
Hello Everyone,

I have a gridview, with both BoundField and TemplateField, both displaying values from a DataTable.

When I try the following code on OnRowDataBound property it works fine

foreach (TableCell cell in e.Row.Cells)
{
if (e.Row.Cells.GetCellIndex(cell) == 1 && e.Row.DataItemIndex > 0)
{
strCellVal = strCellVal + cell.Text.Trim();
//Response.Write(strCellVal);
}
}

The above code works fine when I GetCellIndex is set to BoundField.
But If i try to check with TemplateField it doesnt work.

I hope my question was clear.
Has anyone faced the same issue? Or know a workaround? Any suggestion would greatly appreciated.

Thanks!
 
I'm assuming this is a webform. place a WebControl (Label, place holder, panel, etc.) in the cell you want. then simply find that control and set it's value.

not sure why you are looping through table cells. the gridview abstracts this for you. code could look something like this
Code:
protected override void OnLoad(EventArgs e)
{
   base.OnLoad(e);
   if(IsPostBack) return;
   GridView.DataSource = GetData();
   DataBind();
}

protected void GridView_RowDataBound(object sender, GridViewRowDataBoundEventArgs e)
{
   if(e.RowIndex < 0) return;
   PlaceHolder ctrl = e.Row.FindControl("name of control") as PlaceHolder;
   if(ctrl == null) return;
   ctrl.Text = ((DataViewRow)e.DataItem)["name of column"].ToString();
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank you Jason for your reply.

Below is part of my Gridview :

<asp:GridView id="grd" runat="server" OnRowDataBound="grd_RowDataBound">

<asp:TemplateField HeaderText="Closed" SortExpression="Closed">
<ItemTemplate>
<asp:Label ID="lblClosed_Total" runat="server" Text='<%# Bind("Closed_Total") %>' ForeColor="Blue"></asp:Label>
[
<asp:Label ID="lblClosed_High" runat="server" Text='<%# Bind("Closed_High") %>' ForeColor="Red"></asp:Label>
]
</ItemTemplate>

</GridView>

Where lblClosed_Total is the label control. Which when viewed has values from 0, 1 0r 2.

O/P:
Closed
------
0[2]
2[1]
1[0]

The above values in the gridviews are based on certain condition.

WHAT I AM TRYING : I want to change the background color if lblClosed_Total is >0

I tried the following code:

protected void grd_RowDataBound(object sender, GridViewRowDataBoundEventArgs e)
{
lbl = (Label)(rowItem.Cells[3].FindControl("lblClosed_Total"));
if (Convert.ToUInt32(lbl.Text) > 0))
{
//Change the cell color.
}
}

Can you tell me Where I am wrong? lbl.text is erroring out.

Thanks for any help.
 
a couple things.
1. you need to get the row index. Headers, footers are pagers are also processed in this event.
2. don't check against the html (bl.Text). check against the data e.Row.DataItem. You will need to cast this to the appropiate type before getting the value.
Code:
protected void grd_RowDataBound(object sender, GridViewRowDataBoundEventArgs e)
{
    if(e.Row.RowIndex < 0) return;
    Label lbl = rowItem.Cells[3].FindControl("lblClosed_Total") as Label;
    TheDataType data = e.DataItem as TheDataType;
    if(data != null && lbl != null)
    lbl.CssClass = (data.Total > 0) ? "good" : "bad";
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Jason,

Working!!!
Code:
if (e.Row.RowType != DataControlRowType.DataRow)
            {
                return;
            }

            Label lbl = (Label)e.Row.FindControl("lblClosed_Total");
            string strLbl;
            strLbl = lbl.Text;
            if (strLbl != "0")
            {
                e.Row.Cells[3].BackColor = Color.Black;

            }

Thank you for all your time.
 
Sorry Jason,

I didnt notice your reply when I posted mine.
I will change my code based on your input.

Thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top