Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 9.23 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to remove the whole first column in the GridView when the value of the DropDownList Filter is rather than ALL?
  2. <asp:DropDownList ID="ddlDivision" runat="server" AppendDataBoundItems="True"
  3.         AutoPostBack="True" DataSourceID="sqlDataSourceDivision" DataTextField="DivisionName"
  4.         DataValueField="DivisionName"  
  5.         Width="275px" EnableViewState="False">
  6.         <asp:ListItem Value="%">All</asp:ListItem>
  7.     </asp:DropDownList>
  8.  
  9.  
  10.      <br />  <br />  
  11.         <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
  12.             <ItemTemplate>
  13.  
  14.                 <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("GroupID")%>' />
  15.  
  16.                 <asp:SqlDataSource ID="SqlDataSource1" runat="server"
  17.                                     ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
  18.                                     SelectCommandType="StoredProcedure" SelectCommand="kbiReport"
  19.                                     FilterExpression="[DivisionName] like '{0}%'">
  20.  
  21.                     <FilterParameters>
  22.                         <asp:ControlParameter ControlID="ddlDivision" Name="DivisionName"
  23.                                                  PropertyName="SelectedValue" Type="String" />
  24.                     </FilterParameters>
  25.  
  26.                     <SelectParameters>
  27.                         <%--ControlParameter is linked to the HiddenField above to generate different GridView based on different values
  28.                             of GroupID--%>
  29.                         <asp:ControlParameter ControlID="HiddenField1" Name="GroupID" PropertyName="Value" />
  30.                     </SelectParameters>
  31.                 </asp:SqlDataSource>
  32.                 <div style="width:700px; overflow:auto; overflow-y:hidden;">
  33.  
  34.                 <asp:GridView ID="GridView1" runat="server"
  35.                                 AllowSorting="True"
  36.                                 CellPadding="3"
  37.                                 DataSourceID="SqlDataSource1"
  38.                                 ClientIDMode="Static" class="fixedTables" Width="600" AutoGenerateColumns="true"
  39.                                 AlternatingRowStyle-CssClass="alt"
  40.                                 RowStyle-HorizontalAlign="Center"
  41.                                 OnRowDataBound="GridView1_RowDataBound" OnPreRender="GridView1_PreRender" OnRowCreated="GridView1_RowCreated"
  42.                                 OnDataBound="GridView1_DataBound">
  43.                     <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  44.                     <HeaderStyle Font-Bold = "true" ForeColor="Black"/>
  45.                     <Columns>
  46.                     </Columns>
  47.                     <EditRowStyle BackColor="#999999" />
  48.                     <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  49.                     <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  50.                     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
  51.                     <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  52.                     <SortedAscendingCellStyle BackColor="#E9E7E2" />
  53.                     <SortedAscendingHeaderStyle BackColor="#506C8C" />
  54.                     <SortedDescendingCellStyle BackColor="#FFFDF8" />
  55.                     <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
  56.                 </asp:GridView>
  57.                 </div>
  58.                 <br />
  59.             </ItemTemplate>
  60.         </asp:Repeater>
  61.  
  62.         <asp:SqlDataSource ID="SqlDataSource1" runat="server"
  63.                            ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
  64.                            SelectCommand="SELECT DISTINCT GroupID FROM courses">
  65.         </asp:SqlDataSource>
  66.  
  67.         <%--Filtering by Division--%>
  68.         <asp:SqlDataSource ID="sqlDataSourceDivision" runat="server"
  69.         ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
  70.         SelectCommand="SELECT [DivisionName] FROM [Divisions]"></asp:SqlDataSource>
  71.        
  72. protected void Page_Load(object sender, EventArgs e)
  73.     {
  74.  
  75.         //Repeater1.DataBind();
  76.  
  77.  
  78.     }
  79.  
  80.  
  81.     //protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
  82.     //{
  83.     //    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
  84.     //    {
  85.     //        GridView gv = e.Item.FindControl("GridView1") as GridView;
  86.     //        if (gv != null)
  87.     //        {
  88.  
  89.     //            gv.DataBind();
  90.     //            if (ddlDivision.SelectedValue != "ALL")
  91.     //            {
  92.     //                if (gv.Columns.Count > 0)
  93.     //                    gv.Columns[0].Visible = false;
  94.     //                else
  95.     //                {
  96.     //                    gv.HeaderRow.Cells[0].Visible = false;
  97.     //                    foreach (GridViewRow gvr in gv.Rows)
  98.     //                    {
  99.     //                        gvr.Cells[0].Visible = false;
  100.     //                    }
  101.     //                }
  102.     //            }
  103.  
  104.     //        }
  105.     //    }
  106.  
  107.     //}
  108.  
  109.  
  110.  
  111.     //protected void ddlDivision_SelectedIndexChanged(object sender, EventArgs e)
  112.     //{
  113.     //    if (ddlDivision.SelectedItem.Text == "All")
  114.     //    {
  115.     //        GridView1.Columns[0].Visible = true;
  116.     //    }
  117.     //    else
  118.     //    {
  119.     //        GridView1.Columns[0].Visible = false;
  120.     //    }
  121.     //}
  122.  
  123.     //This method is for deleting the first column in the GridView
  124.     protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  125.     {
  126.        // e.Row.Cells[0].Visible = false; // hides the first column
  127.     }
  128.  
  129.     protected void GridView1_DataBound(object sender, EventArgs e)
  130.     {
  131.         //GridView GridView1 = (GridView)sender;
  132.         //foreach (GridViewRow gvr in GridView1.Rows)
  133.         //{
  134.         //    if (ddlDivision.SelectedValue != "All")
  135.         //    {
  136.         //        gvr.Cells[0].Visible = false;
  137.  
  138.         //    }
  139.         //}
  140.  
  141.  
  142.  
  143.  
  144.     }
  145.  
  146.  
  147.     //This function is for checking each cell in each row.
  148.     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  149.     {
  150.         //var gv = sender as GridView;
  151.         //if (ddlDivision.SelectedValue == "All")
  152.         //    gv.Columns[0].Visible = false;
  153.  
  154.         if (e.Row.RowType == DataControlRowType.DataRow)
  155.         {
  156.             foreach (TableCell c in e.Row.Cells)
  157.             {
  158.                 // Check if the cell vlaue = Yes
  159.                 // if it is Yes, the cell will be colored with Light Green
  160.                 if (c.Text.Contains(", Yes"))
  161.                 {
  162.                     c.BackColor = System.Drawing.Color.LightGreen;
  163.                     c.Text = "&bull;";
  164.                 }
  165.                 else if (c.Text.Contains(", NO"))
  166.                 {
  167.  
  168.                     c.Text = "";
  169.                 }
  170.             }
  171.         }
  172.  
  173.          //The following is for changing the color of headers in each GridView based on the value of the HiddenFild
  174.          //BTW, the value of the HiddenField is the value of the GroupID in Group Table in the Database
  175.         else if (e.Row.RowType == DataControlRowType.Header)
  176.         {
  177.             switch (((HiddenField)((GridView)sender).Parent.FindControl("HiddenField1")).Value)
  178.             {
  179.                 case "1":
  180.                     for (int i = 4; i < e.Row.Cells.Count; i++)
  181.                         e.Row.Cells[i].BackColor = System.Drawing.Color.LightBlue;
  182.                     break;
  183.  
  184.                 case "2":
  185.                     for (int i = 4; i < e.Row.Cells.Count; i++)
  186.                         e.Row.Cells[i].BackColor = System.Drawing.Color.LightYellow;
  187.                     break;
  188.  
  189.                 case "3":
  190.                     for (int i = 4; i < e.Row.Cells.Count; i++)
  191.                         e.Row.Cells[i].BackColor = System.Drawing.Color.Orange;
  192.                     break;
  193.             }
  194.         }
  195.  
  196.     }
  197.  
  198.  
  199.     protected void GridView1_PreRender(object sender, EventArgs e)
  200.     {
  201.         var gv = sender as GridView;
  202.         if (gv.Rows.Count > 0)
  203.         {
  204.             gv.UseAccessibleHeader = true;
  205.             gv.HeaderRow.TableSection = TableRowSection.TableHeader;
  206.         }
  207.     }
  208.        
  209. protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
  210. {
  211.     if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
  212.     {
  213.         GridView gv = e.Item.FindControl("GridView1") as GridView;
  214.         if (gv != null)
  215.         {
  216.  
  217.             gv.DataBind();
  218.             if (ddlDivision.SelectedValue != "ALL")
  219.             {
  220.                 if (gv.Columns.Count > 0)
  221.                     gv.Columns[0].Visible = false;
  222.                 else
  223.                 {
  224.                     gv.HeaderRow.Cells[0].Visible = false;
  225.                     foreach (GridViewRow gvr in gv.Rows)
  226.                     {
  227.                         gvr.Cells[0].Visible = false;
  228.                     }
  229.                 }
  230.             }
  231.  
  232.         }
  233.     }
  234.  
  235. }
  236.        
  237. if (ddlDivision.SelectedValue == "ALL") { // hide column 0 }
  238.        
  239. if (ddlDivision.SelectedValue != "ALL") { // hide column 0 }
  240.        
  241. protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
  242. {
  243.     ...
  244.     if (e.Row.RowType != DataControlRowType.Pager)
  245.     {
  246.         if (ddlDivision.SelectedItem.Text != "ALL")
  247.         {
  248.            // only check for pager row, all other rows including header/footer should be hidden
  249.            e.Row.Cells[0].Visible = false;
  250.         }
  251.     }
  252.     ...    
  253. }