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

link button color in gridview

1 Answer 51 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Arun
Top achievements
Rank 1
Arun asked on 02 Jul 2013, 05:11 AM
Hi,

       I Want to change the color of link button  in grid view based on values from database.

For eg :
        value1 - color-green
       value 0 -color red. 


my Query:

 protected void RadGrid1_DataBound(object sender, EventArgs e)
        {
            DataTable dt = objbl.viewenableuser();
            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
string value = dt.Rows[i]["empstatus"].ToString();
                if (value.ToString() == "1")
                {

                    LinkButton lbk = (LinkButton)RadGrid1.TemplateControl.FindControl("lnkenable");
                    lbk.ForeColor = System.Drawing.Color.Red;

 if (value.ToString() == "0")

{
}



Thanks ,

Arun.

















                {
                    //RadGrid1.MasterTableView.FindControl("lnkdisable").Visible = false;
                }
            

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 02 Jul 2013, 06:01 AM
Hi Arun,

You can change the color of the LinkButton in ItemDataBound that is the better way.We cant access a control using
"LinkButton lbk = (LinkButton)RadGrid1.TemplateControl.FindControl("lnkenable");" .
Have a look at the following code snippet.Please try with this code and check if it helps.

ASPX:
<telerik:RadGrid ID="Radgrid1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
      onitemdatabound="Radgrid1_ItemDataBound">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn HeaderText="UserID" DataField="UserID" UniqueName="UserID">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn HeaderText="ShipCity">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server"> <%#Eval("ShipCity")%></asp:LinkButton>
            </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void Radgrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        if (dataItem["UserID"].Text == "1")
        {
            LinkButton lbk = (LinkButton)dataItem.FindControl("LinkButton1");
            lbk.ForeColor = System.Drawing.Color.Red;
        }
        if (dataItem["UserID"].Text == "0")
        {
            LinkButton lbk = (LinkButton)dataItem.FindControl("LinkButton1");
            lbk.ForeColor = System.Drawing.Color.Green;
        }
    }     
}

Thanks,
Princy
Tags
Grid
Asked by
Arun
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Share this question
or