views:

28

answers:

2

Hello,

Just wondering if anyone has ever managed to find a way to have a datagridview image button column in a template field which display different images on different rows depending on some data element in the row. Our application needs to display a series of fees charged against an account column which will be either less then 500, 500 to 1000 or >1000. Some of those fees are charged at a full rate, others at a partial rate, and some are not charged at all. The user would like to see a simple 3-state graphic which would convey the simple info: full-charge, partial-charge, or no-charge. Sounds easy enough but I've not yet managed to find a way to do this. Every time I attempt to modify the individual cell's graphic it seems to want to change the graphic for all the rows.

Any way around this issue?

Thanks,

A: 

You can do this with

<TemplateField>
    <ItemTemplate>
        <asp:ImageButton runat="server" ImageUrl='<%# GetImage(Eval("check") as string) %>' />
    </ItemTemplate>
</TemplateField>

Where GetImage() is a method in your code-behind:

protected string GetImage(string check)
{
    if ((check == "2") || (check == "null") || (check == "3") || (check == "-1"))
    {
        return "../Interbacs/images/error.gif";
    }
    else if (check == "1") 
    {
        return "../Interbacs/images/ok.png"
    }

    return "what?";
}

This can probably be done a little "prettier" but I hope you get the idea.

veggerby
Well I sorted out this issue :
used gridview_rowdatabound to solve d problem.. any way thnx guys for ur help ... Really appreciate that.
however by using gridview_rowdatabound you'll mess up your ViewState needlessly. whenever possible always go for having it specified in the template/databinding directly. see this http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx
veggerby
A: 

U can change the image in datagridview CellPainting.

eg:

 private void datagridview_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        { 
           if (e.ColumnIndex == 1 && e.RowIndex >= 0)
                {        

                  if (Convert.ToInt32 (datagridview.Rows[e.RowIndex].Cells["columnName"].Value)<500)
                        {
                            e.Paint(e.CellBounds, DataGridViewPaintParts.All);
                            e.Graphics.DrawImage('Give Image path here', e.CellBounds.Left + 5, e.CellBounds.Top + 5);

                        }
                 }
         } 

OR check this http://www.codeproject.com/KB/grid/DGV_ImageButtonCell.aspx/

Vyas
well I cant use DataGridViewCellPaintingEventArgs in asp.net