Click here to Skip to main content
16,015,900 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I tried adding image onrowdatabound

What I have tried:

C#
string imgAsc = @" <img src='../Images\asc.png' title='Ascending' />";
string imgDes = @" <img src='../Images\des.png' title='Descendng' />";

if (e.Row.RowType == DataControlRowType.Header)
{
    foreach (TableCell cell in e.Row.Cells)
    {
        LinkButton lbSort = (LinkButton)cell.Controls[0];
        if (lbSort.Text == GridView1.SortExpression)
        {
            if (GridView1.SortDirection == SortDirection.Ascending)
                lbSort.Text += imgAsc;
            else
                lbSort.Text += imgDes;
        }
    }
}
Posted
Updated 28-Jul-16 21:27pm
v2

1 solution

The error message tells you that there is zero Controls.
Check the Count of Controls before assuming that Controls[0] exist.

[Update]
Use the debugger to see exactly where is the problem and the variables contain at the problem.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]


The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2
Comments
Member 12652110 29-Jul-16 10:06am    
how do i do it?
Patrice T 29-Jul-16 10:08am    
Try Controls.Count
Member 12652110 29-Jul-16 10:50am    
int a = Controls.Count;
a= 0 and count = 3?
What should i put then
Patrice T 29-Jul-16 11:11am    
Look at your code, Controls is not alone, it is cell.Controls.
So cell.Controls.Count
Member 12652110 29-Jul-16 11:54am    
i have this method
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell cell in e.Row.Cells)

{
int c = cell.Controls.Count;
LinkButton lbSort = (LinkButton)cell.Controls[1];
if (lbSort.Text == GridView1.SortExpression)
{
if (GridView1.SortDirection == SortDirection.Ascending)
lbSort.Text += imgAsc;
else
lbSort.Text += imgDes;
}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900