This is a migrated thread and some comments may be shown as answers.

Onclick-Change of image of a button inside GridButtonColumn

1 Answer 142 Views
Grid
This is a migrated thread and some comments may be shown as answers.
K.
Top achievements
Rank 1
K. asked on 20 Oct 2008, 02:18 PM
Hello telerik-Team,

i am using a GridButtonColumn with an ImageButton as follows.

<telerik:GridButtonColumn CommandName="MyCommand" ButtonType="ImageButton" UniqueName="ButtonColumn"></telerik:GridButtonColumn>

In Radgrid_ItemDataBound i am setting the ImageUrl of this Button like this:

            if (e.Item is GridDataItem)
            {
                GridDataItem dataItem = e.Item as GridDataItem;
                if (enableBtn)
                {
                    (dataItem["ButtonColumn"].Controls[0] as ImageButton).ImageUrl = "icon_ok.gif";
                }
                else
                {
                    (dataItem["ButtonColumn"].Controls[0] as ImageButton).ImageUrl = "icon_not_ok.gif";
                }
            }

I want to change the image of the button depending on a value in my database/one of the gridcells.
I am using RadGrid_ItemCreated for loading the grid

if (e.Item is GridEditableItem)
            {
                System.Data.DataRowView rowView = (System.Data.DataRowView)e.Item.DataItem;

                String enabled = rowView.Row["M_ENABLE"].ToString();

                if (enabled == "0")
                {
                    enableBtn = false;
                }
                if (enabled == "1")
                {
                    enableBtn = true;
                }
            }

This is where i get into trouble. I now want to click on the button and change the image aswell as the value of "M_ENABLE" in the Database. But after clicking the button rowView is null. I think i need to replace the line System.Data.DataRowView rowView = (System.Data.DataRowView)e.Item.DataItem;
I just can't find out with what :D

1 Answer, 1 is accepted

Sort by
0
K.
Top achievements
Rank 1
answered on 20 Oct 2008, 03:33 PM
Ok got it...

Dont need ItemCreated anymore.

Changed ItemDataBound though to

            if (e.Item is GridDataItem)
            {
                GridDataItem dataItem = e.Item as GridDataItem;
                string enabled = dataItem["M_ENABLE"].Text as string;

                if (enabled == "0")
                {
                    (dataItem["ButtonColumn"].Controls[0] as ImageButton).ImageUrl = "icon_not_ok.gif";
                }
                if (enabled == "1")
                {
                    (dataItem["ButtonColumn"].Controls[0] as ImageButton).ImageUrl = "icon_ok.gif";
                }
            }

Now i dont get a null-value anymore.
Tags
Grid
Asked by
K.
Top achievements
Rank 1
Answers by
K.
Top achievements
Rank 1
Share this question
or