Click here to Skip to main content
15,917,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a master and detail gridview where the detailed gridviews column keep on changing based on the column clicked in master gridview.What i want to do is to edit the dynamic gridview having different number of column each time.My detailed Gridview is Gridview1 and Master is
gvweeklysum.Below is snippet for master gridview based on which detailed gridview is populated.The item templates does not work out as number of col. are changing.Please suggest possible solution for this scenario?

C#
 protected void gvweeklysum_RowCommand(object sender, GridViewCommandEventArgs e)
    {

         if (e.CommandName.ToString() == "ColumnClick")
        {
            foreach (GridViewRow r in gvweeklysum.Rows)
            {
                if (r.RowType == DataControlRowType.DataRow)
                {
                    for (int columnIndex = 0; columnIndex < r.Cells.Count; columnIndex++)
                    {
                        r.Cells[columnIndex].Attributes["style"] += "background-color:White;";
                    }
                }
            }
            int selectedRowIndex = Convert.ToInt32(e.CommandArgument.ToString());
            int selectedColumnIndex = Convert.ToInt32(Request.Form["__EVENTARGUMENT"].ToString());
            gvweeklysum.Rows[selectedRowIndex].Cells[selectedColumnIndex].Attributes["style"] += "background-color:Red;";



             // get the header of gridview to check which col is clicked.
            string headertext = gvweeklysum.Columns[selectedColumnIndex].HeaderText;




            if (headertext == "Weekending")  
            {
                // null condition and a else condition for a blank col in gridview 
                string weekending = gvweeklysum.Rows[selectedRowIndex].Cells[selectedColumnIndex].Text;
                DateTime weekendingdate = Convert.ToDateTime(weekending);
                DateTime getweekbegin = weekendingdate.AddDays(-6);


                var sqlFormattedDate = weekendingdate.Date.ToString("yyyy-MM-dd HH:mm:ss");
                var weekbegin = getweekbegin.Date.ToString("yyyy-MM-dd HH:mm:ss");



                DataTable dtfrweekending = new DataTable();
                connfordata.Open();
                SqlCommand cmdfrdate = new SqlCommand("select s001_Weekending as Weekend,s001_hrsday1 as Monday,s001_hrsday2 as Tuesday,s001_hrsday3 as Wednesday,s001_hrsday4 as Thursday,s001_hrsday5 as Friday,s001_hrsday6 as Saturday,s001_hrsday7 as Sunday from dbo.s001_Timesheets where s001_weekending between  '" + weekbegin + "' and '" + sqlFormattedDate + "' ", connfordata);
                SqlDataAdapter dafrdate = new SqlDataAdapter(cmdfrdate);
                dafrdate.Fill(dtfrweekending);
                connfordata.Close();

                GridView1.DataSource = dtfrweekending;
                GridView1.DataBind();
            }
}
}
Posted
Updated 11-Jul-13 6:55am
v3

1 solution

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