Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have set active and deactive status in database for gridview button. when I load gridview on page load i want to color button according to database value.
Posted

C#
foreach (GridViewRow row in GridView1.Rows)
        {
            Button BTN = (Button)row.FindControl("XXXXX");
            BTN.BackColor = System.Drawing.Color.Bisque;
        }
 
Share this answer
 
Comments
Dhanashree Dive 12-Feb-13 8:32am    
foreach (GridViewRow row in griduser.Rows)
{
Button BTN = (Button)row.FindControl("lnkstatus");
ClassConnection csc = new ClassConnection();
SqlConnection con = new SqlConnection(csc.connmethod());
SqlDataReader dr;
string sstr = "Select flag from Registration";
SqlCommand cmd = new SqlCommand(sstr, con);
con.Open();
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
string flag = dr[0].ToString();
if (flag == "1")
{

BTN.BackColor = System.Drawing.Color.Green;
}
else
{

BTN.BackColor = System.Drawing.Color.Red;
}
}
}
dr.Close();
con.Close();

}

By this code it is coloring all buttons with same color. I want to color buttons according to database value
Hi,

try as below.

Add, one hidden label "lblStatus" in gridview.
ASP.NET
<templatecolumn visible="false">
<itemtemplate>
<asp:label id="lblStatus" runat="server" text="<%#Eval("Status") %>" xmlns:asp="#unknown"></asp:label>
</itemtemplate>
</templatecolumn>

C#
//In Row data bound event, add below lines.

if(e.Row.RowType == DataControlRowType.DataRow)
{
Button btn = (Button)e.Row.FindControl("btn");
Label lbl = (Label)e.Row.FindControl("lblStatus");
if(Convert.ToBoolean(lbl.Text))
{
  btn.BackColor = System.Drawing.Color.Gray;
}
else
{
  btn.BackColor = System.Drawing.Color.Green;
}
}


The label text will be binded by datasource from database. based on that status value, the button color can be changed.

Note that, the text for status would be "True" or "False" otherwise you can compare with data of your choice.

hope it helps.
 
Share this answer
 
v2
Comments
Dhanashree Dive 12-Feb-13 8:13am    
protected void griduser_DataBound(object sender, EventArgs e)
{
if(e.Row.RowType == RowType.DataRow)
{
Button btn = (Button)e.Row.FindControl("btn");
Label lbl = (Label)e.Row.FindControl("lblStatus");
if(Convert.ToBoolean(lbl.Text))
{
btn.BackColor = System.Drawing.Color.Gray;
}
else
{
btn.BackColor = System.Drawing.Color.Green;
}
}
Thank you for your reply. But I am getting these two errors.

Error 39 'System.EventArgs' does not contain a definition for 'Row' and no extension method 'Row' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)

Error 40 The name 'RowType' does not exist in the current context
Karthik Harve 12-Feb-13 12:38pm    
Are you checking in row databound event ? method signature would be something like this
protected void griduser_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
also check updated answer with above method signature for the event.
Dhanashree Dive 12-Feb-13 23:20pm    
Error 40 The name 'RowType' does not exist in the current context
now this error is coming. Please hepl me out.
Karthik Harve 12-Feb-13 23:22pm    
is it a gridview or datagrid ?
Dhanashree Dive 12-Feb-13 23:24pm    
Gridview.

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