Click here to Skip to main content
15,948,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello there,

i need to set backcolor of row as green which is i am exporting from gridview.
it must be green where start_date=system.date.add(1)
i did it in gridview but i need to do the same while exporting it in excel
my code is as under please help me with this.


[Formatted Code - Start]
C#
protected void btnShow_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtsdate.Text != "" && txtedate.Text != "")
                {

                    string[] dtarr1 = new string[3];
                    dtarr1 = txtsdate.Text.Split('-');
                    string dfmain = dtarr1[1] + "-" + dtarr1[0] + "-" + dtarr1[2];

                    string[] dtarr2 = new string[3];
                    dtarr2 = txtedate.Text.Split('-');
                    string dtmain1 = dtarr2[1] + "-" + dtarr2[0] + "-" + dtarr2[2];

                    DataSet ds1 = new DataSet();
                    Hashtable ht = new Hashtable(2);
                    ht.Add("@ddate", Convert.ToDateTime(dfmain));
                    ht.Add("@edate", Convert.ToDateTime(dtmain1));

                    ds1 = c.getstoredata("DailyVacancyReport", ht);
                    if (ds1.Tables[0].Rows.Count > 0)
                    {
                        GridView1.DataSource = ds1;
                        GridView1.DataBind();
                        Button6.Visible = true;
                    }
                    else
                    {
                        Response.Write("<script>alert('There is no record found. Please try another Date!')</script>");
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label l = (Label)e.Row.FindControl("lblsdate");

                string dttm = Convert.ToString(DateTime.Now.AddDays(1).ToShortDateString());
                string[] dtarr1 = new string[3];
                dtarr1 = dttm.Split('/');
                string dfmain = dtarr1[1] + "-" + dtarr1[0] + "-" + dtarr1[2];

                string dttm1 = l.Text;
                string[] dtarr2 = new string[3];
                dtarr2 = dttm1.Split('-');
                string dfmain1 = dtarr2[0] + "-" + Convert.ToInt16(dtarr2[1]) + "-" + dtarr2[2];

                if (dfmain1 == dfmain)
                {
                    e.Row.BackColor = System.Drawing.Color.Green;
                }

                if ((DataBinder.Eval(e.Row.DataItem, "Hours") != DBNull.Value))
                {
                    doc += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Hours"));
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lbldoctors = (Label)e.Row.FindControl("lbltotal");
                lbldoctors.Text = doc.ToString();
            }

        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            try
            {
                string[] dtarr1 = new string[3];
                dtarr1 = txtsdate.Text.Split('-');
                string dfmain = dtarr1[1] + "-" + dtarr1[0] + "-" + dtarr1[2];

                string[] dtarr2 = new string[3];
                dtarr2 = txtedate.Text.Split('-');
                string dtmain1 = dtarr2[1] + "-" + dtarr2[0] + "-" + dtarr2[2];

                fdoc = 0;
                DataSet ds1 = new DataSet();
                Hashtable ht = new Hashtable(2);
                ht.Add("@ddate", Convert.ToDateTime(dfmain));
                ht.Add("@edate", Convert.ToDateTime(dtmain1));

                ds1 = c.getstoredata("DailyVacancyReport", ht);

                DataTable dtTemp = new DataTable();

                //Creating Header Row
                
                dtTemp.Columns.Add(" ");
                dtTemp.Columns.Add("EvolveID");
                dtTemp.Columns.Add("HospitalName");
                dtTemp.Columns.Add("ContactName");
                dtTemp.Columns.Add("Job Title");
                //dtTemp.Columns.Add("Speciality");
                dtTemp.Columns.Add("Hours");

                DataRow drAddItem;
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    string s = ds1.Tables[0].Rows[i]["JobTitleName"].ToString();
                    string t = ds1.Tables[0].Rows[i]["Speciality"].ToString();
                    string vs = string.Concat(s,"-", t);
                    
                    drAddItem = dtTemp.NewRow();

                    drAddItem[0] = " ";
                    drAddItem[1] = ds1.Tables[0].Rows[i]["EvolveID"].ToString();
                    drAddItem[2] = ds1.Tables[0].Rows[i]["HospitalName"].ToString();
                    drAddItem[3] = ds1.Tables[0].Rows[i]["ContactName"].ToString();
                    drAddItem[4] = vs;
//                    drAddItem[5] = ds1.Tables[0].Rows[i]["Speciality"].ToString();
                    drAddItem[5] = ds1.Tables[0].Rows[i]["Hours"].ToString();
                    
                    if (ds1.Tables[0].Rows[i]["Hours"].ToString() != "")
                    {
                        fdoc += Convert.ToDouble(ds1.Tables[0].Rows[i]["Hours"].ToString());
                    }

                    dtTemp.Rows.Add(drAddItem);
                    
                }
                if (ds1.Tables[0].Rows.Count > 0)
                {
                    drAddItem = dtTemp.NewRow();
                    drAddItem[4] = "Total";

                    drAddItem[5] = "" + fdoc + "";
                    
                    dtTemp.Rows.Add(drAddItem);
                    
                }
                //Temp Grid
                DataGrid dg = new DataGrid();
                dg.DataSource = dtTemp;
                dg.DataBind();
                ExportToExcel("DataSheet.xls", dg);
                dg = null;
                dg.Dispose();
            }

            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

        }
        
        
        private void ExportToExcel(string strFileName, DataGrid dg)
        {
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
            Response.ContentType = "application/excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            dg.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }

[Formatted Code - End]
Posted
Updated 26-Jun-12 21:44pm
v2

thanks everybody i solved it many days ago.. thanks again
 
Share this answer
 

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