By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,359 Members | 1,481 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,359 IT Pros & Developers. It's quick & easy.

childgrid > Not sorting

100+
P: 145
The sort on the childgrid is not working; nothing happens when I click on the each column header for sort. (I followed Satay's sample: http://www.codeproject.com/KB/aspnet...dGridView.aspx) and i am using object for datasource. please suggest.

= = = default.aspx = =
Expand|Select|Wrap|Line Numbers
  1.  
  2. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5.  
  6. <html xmlns="http://www.w3.org/1999/xhtml" >
  7. <head id="Head1" runat="server">
  8.     <title>Editable Nested Grid View</title>
  9.   <script language="javascript" src="ExpandCollapse.js" mce_src="ExpandCollapse.js"></script>   
  10. </head>
  11. <body>
  12.     <form id="form1" runat="server">
  13.         <asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true" DataTextField="location_name"
  14.             DataValueField="location_id" Font-Names="Tahoma" Font-Size="8pt" OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged"
  15.             Style="position: relative" Width="288px">
  16.         </asp:DropDownList>11<br />
  17.     <div>
  18.         <asp:GridView ID="GridView1" BackColor="#f1f1f1" DataKeyNames="subCatID" 
  19.             AutoGenerateColumns=false  style="Z-INDEX: 101; LEFT: 8px;" ShowFooter=true Font-Size=XX-Small
  20.             Font-Names="Verdana" runat="server" GridLines=None OnRowDataBound="GridView1_RowDataBound" 
  21.             OnRowCommand = "GridView1_RowCommand" BorderStyle=Outset AllowSorting=true>
  22.             <RowStyle BackColor="Gainsboro" />
  23.             <AlternatingRowStyle BackColor="White" />
  24.             <HeaderStyle BackColor="#0083C1" ForeColor="White"/>
  25.             <FooterStyle BackColor="White" />
  26.             <Columns>
  27.                 <asp:TemplateField>
  28.                     <ItemTemplate>
  29.                         <a href="javascript:expandcollapse('div<%# Eval("subCatID") %>', 'one');">
  30.                             <img id="imgdiv<%# Eval("subCatID") %>" alt="Click to show/hide <%# Eval("subCatID") %>"  width="9px" border="0" src="plus.gif" mce_src="plus.gif"/>
  31.                         </a>
  32.                     </ItemTemplate>
  33.                 </asp:TemplateField>
  34.  
  35.  
  36.                 <asp:TemplateField HeaderText="subCatID" SortExpression="subCatID" Visible="False">
  37.                     <ItemTemplate>
  38.                         <asp:Label ID="lblsubCatID" Text='<%# Eval("subCatID") %>' runat="server"></asp:Label>
  39.                     </ItemTemplate>
  40.                 </asp:TemplateField>
  41.  
  42.  
  43.                 <asp:TemplateField HeaderText="Sub Location Name" SortExpression="subCatName">
  44.                     <ItemTemplate><%# Eval("subCatName")%></ItemTemplate>
  45.                     <EditItemTemplate>
  46.                         <asp:TextBox ID="txtsubCatName" Text='<%# Eval("subCatName") %>' runat="server"></asp:TextBox>
  47.                     </EditItemTemplate>
  48.                     <FooterTemplate>
  49.                         <asp:TextBox ID="txtsubCatName" Text='' runat="server"></asp:TextBox>
  50.                     </FooterTemplate>
  51.                 </asp:TemplateField>
  52.  
  53.  
  54.  
  55.        <asp:TemplateField>
  56.            <ItemTemplate>
  57.                <tr>
  58.                             <td colspan=100%>
  59.                                 <div id="div<%# Eval("subCatID") %>" style="display:none;position:relative;left:15px;OVERFLOW: auto;WIDTH:97%" >
  60.                                     <asp:GridView ID="GridView2" AllowSorting="true" BackColor="White" Width=70% Font-Size=X-Small
  61.                                         AutoGenerateColumns=false Font-Names="Verdana" runat="server" DataKeyNames="LocationID" ShowFooter=true
  62.                                         OnPageIndexChanging="GridView2_PageIndexChanging" OnRowUpdating = "GridView2_RowUpdating"
  63.                                         OnRowCommand = "GridView2_RowCommand" OnRowEditing = "GridView2_RowEditing" GridLines=None
  64.                                         OnRowUpdated = "GridView2_RowUpdated" OnRowCancelingEdit = "GridView2_CancelingEdit" OnRowDataBound = "GridView2_RowDataBound"
  65.                                         OnRowDeleting = "GridView2_RowDeleting" OnRowDeleted = "GridView2_RowDeleted" OnSorting = "GridView2_Sorting"
  66.                                         BorderStyle=Double BorderColor="#0083C1">
  67.                                         <RowStyle BackColor="Gainsboro" />
  68.                                         <AlternatingRowStyle BackColor="White" />
  69.                                         <HeaderStyle BackColor="#0083C1" ForeColor="White"/>
  70.                                         <FooterStyle BackColor="White" />
  71.                                         <Columns>
  72.  
  73.                                             <asp:TemplateField HeaderText="ID" SortExpression="ID" Visible="false">
  74.                                                 <ItemTemplate><%# Eval("LocationID")%></ItemTemplate>
  75.                                                 <ItemStyle Width="100%" />
  76.                                                 <EditItemTemplate>
  77.                                                     <asp:TextBox ID="txtLocationID" Text='<%# Eval("LocationID")%>' runat="server"></asp:TextBox>
  78.                                                 </EditItemTemplate>
  79.                                                 <FooterTemplate>
  80.                                                     <asp:TextBox ID="txtLocationID" Text='' runat="server"></asp:TextBox>
  81.                                                 </FooterTemplate>
  82.                                             </asp:TemplateField>
  83.  
  84.  
  85.                                             <asp:TemplateField HeaderText="Name" SortExpression="Name">
  86.                                                 <ItemTemplate><%# Eval("Name")%></ItemTemplate>
  87.                                                 <ItemStyle Width="100%" />
  88.                                                 <EditItemTemplate>
  89.                                                     <asp:TextBox ID="txtName" Text='<%# Eval("Name")%>' runat="server"></asp:TextBox>
  90.                                                 </EditItemTemplate>
  91.                                                 <FooterTemplate>
  92.                                                     <asp:TextBox ID="txtName" Text='' runat="server"></asp:TextBox>
  93.                                                 </FooterTemplate>
  94.                                             </asp:TemplateField>
  95.  
  96.                                             <asp:TemplateField HeaderText="Part" SortExpression="Part">
  97.                                                 <ItemTemplate><%# Eval("Part")%></ItemTemplate>
  98.                                                 <ItemStyle Width="100%" />                                                
  99.                                                 <EditItemTemplate>
  100.                                                     <asp:TextBox ID="txtPart" Text='<%# Eval("Part")%>' runat="server"></asp:TextBox>
  101.                                                 </EditItemTemplate>
  102.                                                 <FooterTemplate>
  103.                                                     <asp:TextBox ID="txtPart" Text='' runat="server"></asp:TextBox>
  104.                                                 </FooterTemplate>
  105.                                             </asp:TemplateField>
  106.  
  107.                                             <asp:TemplateField HeaderText="Serial" SortExpression="Serial">
  108.                                                 <ItemTemplate><%# Eval("Serial")%></ItemTemplate>
  109.                                                 <ItemStyle Width="100%" />                                                
  110.                                                 <EditItemTemplate>
  111.                                                     <asp:TextBox ID="txtSerial" Text='<%# Eval("Serial")%>' runat="server"></asp:TextBox>
  112.                                                 </EditItemTemplate>
  113.                                                 <FooterTemplate>
  114.                                                     <asp:TextBox ID="txtSerial" Text='' runat="server"></asp:TextBox>
  115.                                                 </FooterTemplate>
  116.                                             </asp:TemplateField>
  117.  
  118.                                             <asp:TemplateField HeaderText="Expiration" SortExpression="Expiration">
  119.                                                 <ItemTemplate><%# Eval("Expiration")%></ItemTemplate>
  120.                                                 <ItemStyle Width="100%" />                                                
  121.                                                 <EditItemTemplate>
  122.                                                     <asp:TextBox ID="txtExpiration" Text='<%# Eval("Expiration")%>' runat="server"></asp:TextBox>
  123.                                                 </EditItemTemplate>
  124.                                                 <FooterTemplate>
  125.                                                     <asp:TextBox ID="txtExpiration" Text='' runat="server"></asp:TextBox>
  126.                                                 </FooterTemplate>
  127.                                             </asp:TemplateField>   
  128.  
  129.                                             <asp:TemplateField HeaderText="Quantity" SortExpression="Quantity">
  130.                                                 <ItemTemplate><%# Eval("Quantity")%></ItemTemplate>
  131.                                                 <EditItemTemplate>
  132.                                                     <asp:TextBox ID="txtQuantity" Text='<%# Eval("Quantity")%>' runat="server"></asp:TextBox>
  133.                                                 </EditItemTemplate>
  134.                                                 <FooterTemplate>
  135.                                                     <asp:TextBox ID="txtQuantity" Text='' runat="server"></asp:TextBox>
  136.                                                 </FooterTemplate>
  137.                                             </asp:TemplateField>   
  138.  
  139.                                             <asp:TemplateField HeaderText="Location" SortExpression="Location">
  140.                                                 <ItemTemplate><%# Eval("Location")%></ItemTemplate>
  141.                                                 <EditItemTemplate>
  142.                                                     <asp:TextBox ID="txtLocation" Text='<%# Eval("Location")%>' runat="server"></asp:TextBox>
  143.                                                 </EditItemTemplate>
  144.                                                 <FooterTemplate>
  145.                                                     <asp:TextBox ID="txtLocation" Text='' runat="server"></asp:TextBox>
  146.                                                 </FooterTemplate>
  147.                                             </asp:TemplateField>                                               
  148.  
  149.                                             <asp:TemplateField HeaderText="Flight" SortExpression="Flight">
  150.                                                 <ItemTemplate><%# Eval("Flight")%></ItemTemplate>
  151.                                                 <EditItemTemplate>
  152.                                                     <asp:TextBox ID="txtFlight" Text='<%# Eval("Flight")%>' runat="server"></asp:TextBox>
  153.                                                 </EditItemTemplate>
  154.                                                 <FooterTemplate>
  155.                                                     <asp:TextBox ID="txtFlight" Text='' runat="server"></asp:TextBox>
  156.                                                 </FooterTemplate>
  157.                                             </asp:TemplateField>                                               
  158.  
  159.                                               <asp:TemplateField HeaderText="Status" SortExpression="Status">
  160.                                                 <ItemTemplate><%# Eval("Status")%></ItemTemplate>
  161.                                                 <EditItemTemplate>
  162.                                                     <asp:TextBox ID="txtStatus" Text='<%# Eval("Status")%>' runat="server"></asp:TextBox>
  163.                                                 </EditItemTemplate>
  164.                                                 <FooterTemplate>
  165.                                                     <asp:TextBox ID="txtStatus" Text='' runat="server"></asp:TextBox>
  166.                                                 </FooterTemplate>
  167.                                             </asp:TemplateField>                                           
  168.  
  169.                                    <asp:CommandField HeaderText="Edit" ShowEditButton="True" />
  170.                                    <asp:TemplateField HeaderText="Delete">
  171.                                                  <ItemTemplate>
  172.                                                     <asp:LinkButton ID="linkDeleteProd" CommandName="Delete" runat="server">Delete</asp:LinkButton>
  173.                                                  </ItemTemplate>
  174.                                                  <FooterTemplate>
  175.                                                     <asp:LinkButton ID="linkAddProd" CommandName="AddProd" runat="server">Add</asp:LinkButton>
  176.                                                  </FooterTemplate>
  177.                                             </asp:TemplateField>
  178.                                         </Columns>
  179.                                    </asp:GridView>
  180.                                 </div>
  181.                              </td>
  182.                         </tr>
  183.            </ItemTemplate>          
  184.        </asp:TemplateField>       
  185.    </Columns>
  186.         </asp:GridView>
  187.         <br />
  188.         <br />
  189.  
  190.     </div>
  191.  
  192.     </form>
  193. </body>
  194. </html>
  195.  
  196.  


= = = default.aspx.cs = = =
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Data.OleDb;
  11.  
  12. public partial class _Default : System.Web.UI.Page
  13. {
  14.  
  15.     #region Variables
  16.     string gvUniqueID = String.Empty;
  17.     int gvNewPageIndex = 0;
  18.     int gvEditIndex = -1;
  19.     string gvSortExpr = String.Empty;
  20.     private string gvSortDir
  21.     {
  22.  
  23.         get { return ViewState["SortDirection"] as string ?? "ASC"; }
  24.  
  25.         set { ViewState["SortDirection"] = value; }
  26.  
  27.     }
  28.     #endregion
  29.  
  30.     //This procedure returns the Sort Direction
  31.     private string GetSortDirection()
  32.     {
  33.         switch (gvSortDir)
  34.         {
  35.             case "ASC":
  36.                 gvSortDir = "DESC";
  37.                 break;
  38.  
  39.             case "DESC":
  40.                 gvSortDir = "ASC";
  41.                 break;
  42.         }
  43.         return gvSortDir;
  44.     }
  45.  
  46.     //This procedure prepares the query to bind the child GridView
  47.     private AccessDataSource ChildDataSource(string strCustometId, string strSort)
  48.     {
  49.         string strQRY = "";
  50.         AccessDataSource dsTemp = new AccessDataSource();
  51.         dsTemp.DataFile = "App_Data/Northwind.mdb";
  52.         strQRY = "SELECT [Orders].[CustomerID],[Orders].[OrderID]," +
  53.                                 "[Orders].[ShipAddress],[Orders].[Freight],[Orders].[ShipName] FROM [Orders]" +
  54.                                 " WHERE [Orders].[CustomerID] = '" + strCustometId + "'" +
  55.                                 "UNION ALL " +
  56.                                 "SELECT '" + strCustometId + "','','','','' FROM [Orders] WHERE [Orders].[CustomerID] = '" + strCustometId + "'" +
  57.                                 "HAVING COUNT(*)=0 " + strSort;
  58.  
  59.         dsTemp.SelectCommand = strQRY;
  60.         return dsTemp;
  61.     }
  62.  
  63.     protected void Page_Load(object sender, EventArgs e)
  64.     {            //
  65.  
  66.         if (!Page.IsPostBack)
  67.         {           
  68.             FillCategories();                   
  69.         }
  70.         else
  71.         {
  72.             //
  73.         }
  74.  
  75.     }
  76.  
  77.     #region GridView1 Event Handlers
  78.     //This event occurs for each row
  79.     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  80.     {
  81.         GridViewRow row = e.Row;
  82.         string strSort = string.Empty;
  83.  
  84.         // Make sure we aren't in header/footer rows
  85.         if (row.DataItem == null)
  86.         {
  87.             return;
  88.         }
  89.  
  90.         //Find Child GridView control
  91.         GridView gv = new GridView();
  92.         gv = (GridView)row.FindControl("GridView2");
  93.  
  94.         //Check if any additional conditions (Paging, Sorting, Editing, etc) to be applied on child GridView
  95.         if (gv.UniqueID == gvUniqueID)
  96.         {
  97.             gv.PageIndex = gvNewPageIndex;
  98.             gv.EditIndex = gvEditIndex;
  99.             //Check if Sorting used
  100.             if (gvSortExpr != string.Empty)
  101.             {
  102.                 GetSortDirection();
  103.                 strSort = " ORDER BY " + string.Format("{0} {1}", gvSortExpr, gvSortDir);
  104.             }
  105.             //Expand the Child grid
  106.             ClientScript.RegisterStartupScript(GetType(), "Expand", "<SCRIPT LANGUAGE='javascript'>expandcollapse('div" + ((DataRowView)e.Row.DataItem)["subCatID"].ToString() + "','one');</script>");
  107.         }
  108.  
  109.         clsDataBindGrid dsProducts = new clsDataBindGrid();
  110.  
  111.         //Prepare the query for Child GridView by passing the Customer ID of the parent row
  112.         int catID = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "subcatid")); 
  113.         gv.DataSource = dsProducts.FillProducts(catID); 
  114.             //ChildDataSource(((DataRowView)e.Row.DataItem)["subcatID"].ToString(), strSort);
  115.         //(e.Item.DataItem as DataRowView).Row.Table.Columns.Contains("colName1" )
  116.  
  117.         gv.DataBind();
  118.  
  119.  
  120.  
  121.     }
  122.  
  123.     //This event occurs for any operation on the row of the grid
  124.     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  125.     {
  126.         //Check if Add button clicked
  127.         if (e.CommandName == "AddProduct")
  128.         {
  129.             try
  130.             {
  131.                 //Get the values stored in the text boxes
  132.                 string strCompanyName = ((TextBox)GridView1.FooterRow.FindControl("txtCompanyName")).Text;
  133.                 string strContactName = ((TextBox)GridView1.FooterRow.FindControl("txtContactName")).Text;
  134.                 string strContactTitle = ((TextBox)GridView1.FooterRow.FindControl("txtContactTitle")).Text;
  135.                 string strAddress = ((TextBox)GridView1.FooterRow.FindControl("txtAddress")).Text;
  136.                 string strCustomerID = ((TextBox)GridView1.FooterRow.FindControl("txtCustomerID")).Text;
  137.  
  138.                 //Prepare the Insert Command of the DataSource control
  139.                 string strSQL = "";
  140.                 strSQL = "INSERT INTO Customers (CustomerID, CompanyName, ContactName, " +
  141.                         "ContactTitle, Address) VALUES ('" + strCustomerID + "','" + strCompanyName + "','" +
  142.                         strContactName + "','" + strContactTitle + "','" + strAddress + "')";
  143.  
  144.                 //AccessDataSource1.InsertCommand = strSQL;
  145.                 //AccessDataSource1.Insert();
  146.                 ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Customer added successfully');</script>");
  147.  
  148.                 //Re bind the grid to refresh the data
  149.                 //GridView1.DataBind();
  150.                 FillSubCategories();
  151.             }
  152.             catch (Exception ex)
  153.             {
  154.                 ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");
  155.             }
  156.         }
  157.     }
  158.  
  159.     //This event occurs on click of the Update button
  160.     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  161.     {
  162.         //Get the values stored in the text boxes
  163.         string strCompanyName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCompanyName")).Text;
  164.         string strContactName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtContactName")).Text;
  165.         string strContactTitle = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtContactTitle")).Text;
  166.         string strAddress = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAddress")).Text;
  167.         string strCustomerID = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblCustomerID")).Text;
  168.  
  169.         try
  170.         {
  171.             //Prepare the Update Command of the DataSource control
  172.             string strSQL = "";
  173.             strSQL = "UPDATE Customers set CompanyName = '" + strCompanyName + "'" +
  174.                      ",ContactName = '" + strContactName + "'" +
  175.                      ",ContactTitle = '" + strContactTitle + "'" +
  176.                      ",Address = '" + strAddress + "'" +
  177.                      " WHERE CustomerID = '" + strCustomerID + "'";
  178.             //AccessDataSource1.UpdateCommand = strSQL;
  179.             //AccessDataSource1.Update();
  180.             ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('updated successfully');</script>");
  181.         }
  182.         catch { }
  183.     }
  184.  
  185.     //This event occurs after RowUpdating to catch any constraints while updating
  186.     protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
  187.     {
  188.         //Check if there is any exception while deleting
  189.         if (e.Exception != null)
  190.         {
  191.             ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('" + e.Exception.Message.ToString().Replace("'", "") + "');</script>");
  192.             e.ExceptionHandled = true;
  193.         }
  194.     }
  195.  
  196.     //This event occurs on click of the Delete button
  197.     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  198.     {
  199.         //Get the value        
  200.         string strCustomerID = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblCustomerID")).Text;
  201.  
  202.         //Prepare the delete Command of the DataSource control
  203.         string strSQL = "";
  204.  
  205.         try
  206.         {
  207.             strSQL = "DELETE from Customers WHERE CustomerID = '" + strCustomerID + "'";
  208.             //AccessDataSource1.DeleteCommand = strSQL;
  209.             //AccessDataSource1.Delete();
  210.             ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Customer deleted successfully');</script>");
  211.         }
  212.         catch { }
  213.     }
  214.  
  215.     //This event occurs after RowDeleting to catch any constraints while deleting
  216.     protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
  217.     {
  218.         //Check if there is any exception while deleting
  219.         if (e.Exception != null)
  220.         {
  221.             ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('" + e.Exception.Message.ToString().Replace("'", "") + "');</script>");
  222.             e.ExceptionHandled = true;
  223.         }
  224.     } 
  225.     #endregion
  226.  
  227.     #region GridView2 Event Handlers
  228.     protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
  229.     {
  230.         GridView gvTemp = (GridView)sender;
  231.         gvUniqueID = gvTemp.UniqueID;
  232.         gvNewPageIndex = e.NewPageIndex;
  233.         //GridView1.DataBind(); // NO GOOD, the page goes back blank and only dropdown is displayed
  234.         FillSubCategories();
  235.     }
  236.  
  237.     protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
  238.     {
  239.         if (e.CommandName == "AddOrder")
  240.         {
  241.             try
  242.             {
  243.                 GridView gvTemp = (GridView)sender;
  244.                 gvUniqueID = gvTemp.UniqueID;
  245.  
  246.                 //Get the values stored in the text boxes
  247.                 string strCustomerID = gvTemp.DataKeys[0].Value.ToString();  //Customer ID is stored as DataKeyNames
  248.                 string strFreight = ((TextBox)gvTemp.FooterRow.FindControl("txtFreight")).Text;
  249.                 string strShipperName = ((TextBox)gvTemp.FooterRow.FindControl("txtShipperName")).Text;
  250.                 string strShipAdress = ((TextBox)gvTemp.FooterRow.FindControl("txtShipAdress")).Text;
  251.  
  252.                 //Prepare the Insert Command of the DataSource control
  253.                 string strSQL = "";
  254.                 strSQL = "INSERT INTO Orders (CustomerID, Freight, ShipName, " +
  255.                         "ShipAddress) VALUES ('" + strCustomerID + "'," + float.Parse(strFreight) + ",'" +
  256.                         strShipperName + "','" + strShipAdress + "')";
  257.  
  258.                 //AccessDataSource1.InsertCommand = strSQL;
  259.                 //AccessDataSource1.Insert();
  260.                 ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Order added successfully');</script>");
  261.  
  262.                 //GridView1.DataBind();
  263.                 FillSubCategories();
  264.             }
  265.             catch (Exception ex)
  266.             {
  267.                 ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");
  268.             }
  269.         }
  270.     }
  271.  
  272.     protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
  273.     {
  274.         GridView gvTemp = (GridView)sender;
  275.         gvUniqueID = gvTemp.UniqueID;
  276.         gvEditIndex = e.NewEditIndex;
  277.         //GridView1.DataBind();
  278.         FillSubCategories();
  279.     }
  280.  
  281.     protected void GridView2_CancelingEdit(object sender, GridViewCancelEditEventArgs e)
  282.     {
  283.         GridView gvTemp = (GridView)sender;
  284.         gvUniqueID = gvTemp.UniqueID;
  285.         gvEditIndex = -1;
  286.         GridView1.DataBind();
  287.     }
  288.  
  289.     protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
  290.     {
  291.         try
  292.         {
  293.             GridView gvTemp = (GridView)sender;
  294.             gvUniqueID = gvTemp.UniqueID;
  295.  
  296.             //Get the values stored in the text boxes
  297.             string strOrderID = ((Label)gvTemp.Rows[e.RowIndex].FindControl("lblOrderID")).Text;
  298.             string strFreight = ((TextBox)gvTemp.Rows[e.RowIndex].FindControl("txtFreight")).Text;
  299.             string strShipperName = ((TextBox)gvTemp.Rows[e.RowIndex].FindControl("txtShipperName")).Text;
  300.             string strShipAdress = ((TextBox)gvTemp.Rows[e.RowIndex].FindControl("txtShipAdress")).Text;
  301.  
  302.             //Prepare the Update Command of the DataSource control
  303.             AccessDataSource dsTemp = new AccessDataSource();
  304.             dsTemp.DataFile = "App_Data/Northwind.mdb";
  305.             string strSQL = "";
  306.             strSQL = "UPDATE Orders set Freight = " + float.Parse(strFreight) + "" +
  307.                      ",ShipName = '" + strShipperName + "'" +
  308.                      ",ShipAddress = '" + strShipAdress + "'" +
  309.                      " WHERE OrderID = " + strOrderID;
  310.             dsTemp.UpdateCommand = strSQL;
  311.             dsTemp.Update();
  312.             ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Order updated successfully');</script>");
  313.  
  314.             //Reset Edit Index
  315.             gvEditIndex = -1;
  316.  
  317.             GridView1.DataBind();
  318.         }
  319.         catch { }
  320.     }
  321.  
  322.     protected void GridView2_RowUpdated(object sender, GridViewUpdatedEventArgs e)
  323.     {
  324.         //Check if there is any exception while deleting
  325.         if (e.Exception != null)
  326.         {
  327.             ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('" + e.Exception.Message.ToString().Replace("'", "") + "');</script>");
  328.             e.ExceptionHandled = true;
  329.         }
  330.     }
  331.  
  332.     protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
  333.     {
  334.         GridView gvTemp = (GridView)sender;
  335.         gvUniqueID = gvTemp.UniqueID;
  336.  
  337.         //Get the value        
  338.         string strOrderID = ((Label)gvTemp.Rows[e.RowIndex].FindControl("lblOrderID")).Text;
  339.  
  340.         //Prepare the Update Command of the DataSource control
  341.         string strSQL = "";
  342.  
  343.         try
  344.         {
  345.             strSQL = "DELETE from Orders WHERE OrderID = " + strOrderID;
  346.             AccessDataSource dsTemp = new AccessDataSource();
  347.             dsTemp.DataFile = "App_Data/Northwind.mdb";
  348.             dsTemp.DeleteCommand = strSQL;
  349.             dsTemp.Delete();
  350.             ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Order deleted successfully');</script>");
  351.             GridView1.DataBind();
  352.         }
  353.         catch { }
  354.     }
  355.  
  356.     protected void GridView2_RowDeleted(object sender, GridViewDeletedEventArgs e)
  357.     {
  358.         //Check if there is any exception while deleting
  359.         if (e.Exception != null)
  360.         {
  361.             ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('" + e.Exception.Message.ToString().Replace("'", "") + "');</script>");
  362.             e.ExceptionHandled = true;
  363.         }
  364.     }
  365.  
  366.     protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
  367.     {
  368.         //Check if this is our Blank Row being databound, if so make the row invisible
  369.         if (e.Row.RowType == DataControlRowType.DataRow)
  370.         {
  371.             if (((DataRowView)e.Row.DataItem)["LocationID"].ToString() == String.Empty) e.Row.Visible = false;
  372.         }
  373.     }
  374.  
  375.     protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
  376.     {
  377.         GridView gvTemp = (GridView)sender;
  378.         gvUniqueID = gvTemp.UniqueID;
  379.         gvSortExpr = e.SortExpression;        
  380.         //GridView1.DataBind();  // NO GOOD, the page goes back blank and only dropdown is displayed
  381.         FillSubCategories();
  382.     } 
  383.     #endregion
  384.  
  385.     protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
  386.     {
  387.         if ((ddlCategory.SelectedValue == "-- Select a Location --") | (ddlCategory.SelectedValue == "0"))
  388.         {
  389.             Response.Redirect("default.aspx");
  390.         }
  391.         else
  392.         {
  393.             int scID = Convert.ToInt32(ddlCategory.SelectedValue);
  394.             clsDataBindGrid fsc = new clsDataBindGrid();
  395.             GridView1.DataSource = fsc.FillSubCategories(scID);
  396.             GridView1.DataBind();
  397.         }
  398.     }
  399.  
  400.     private void FillSubCategories()
  401.     {
  402.         if ((ddlCategory.SelectedValue == "-- Select a Location --") | (ddlCategory.SelectedValue == "0"))
  403.         { }
  404.         else
  405.         {
  406.             int scID = Convert.ToInt32(ddlCategory.SelectedValue);
  407.             if (scID!=0)
  408.             {
  409.                 clsDataBindGrid fsc = new clsDataBindGrid();
  410.                 GridView1.DataSource = fsc.FillSubCategories(scID);
  411.                 GridView1.DataBind();
  412.             }
  413.             //else
  414.             //{
  415.             //    clsDataBindGrid fsc = new clsDataBindGrid();
  416.             //    GridView1.DataSource= fsc.FillSubCategories(scID);
  417.             //    GridView1.DataBind();
  418.             //}
  419.         }
  420.     }
  421.  
  422.     void FillCategories()
  423.     {
  424.         ddlCategory.SelectedIndex = 0;
  425.         clsDataBindGrid fc = new clsDataBindGrid();
  426.         ddlCategory.DataSource = fc.FillCategories();
  427.         ddlCategory.DataBind();
  428.         ddlCategory.Items.Insert(0, new ListItem("-- Select a Location --"));
  429.         ddlCategory.SelectedIndex = 0;
  430.  
  431.     }
  432.  
  433. }
  434.  
  435.  
= = = here is the javascript = = =
Expand|Select|Wrap|Line Numbers
  1.    function expandcollapse(obj,row)
  2.     {
  3.         var div = document.getElementById(obj);
  4.         var img = document.getElementById('img' + obj);
  5.  
  6.         if (div.style.display == "none")
  7.         {
  8.             div.style.display = "block";
  9.             if (row == 'alt')
  10.             {
  11.                 img.src="minus.gif" mce_src="minus.gif";
  12.             }
  13.             else
  14.             {
  15.                 img.src="minus.gif" mce_src="minus.gif";
  16.             }
  17.             img.alt = "Close to view other Records";
  18.         }
  19.         else
  20.         {
  21.             div.style.display = "none";
  22.             if (row == 'alt')
  23.             {
  24.                 img.src="plus.gif" mce_src="plus.gif";
  25.             }
  26.             else
  27.             {
  28.                 img.src="plus.gif" mce_src="plus.gif";
  29.             }
  30.             img.alt = "Expand to show Records";
  31.         }
  32.     }
Jun 5 '09 #1
Share this Question
Share on Google+
1 Reply


100+
P: 145
It's now resolved,please mark this post "resolved" thanks
Jun 12 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.