Can I delete from the detail of a master/detail datagrid?

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default RE: Can I delete from the detail of a master/detail datagrid?

    Hi Mark,

    As for the delte certain items in the detailed grid items in MASTER/DETAIL
    grids. Here are my suggestions;

    The DataGrid, DataList or Repeater are all Template DataBound web controls
    which are different from the normal List Item controls such as DropDownlist
    which can manually create sub item and add into the items collection.
    Template DataBound controls populate items via databinding, that is said ,
    only when we bind datasource onto it, it can genreate its items and these
    items are bydefault stored in viewstate, they haven't provided function to
    manually remove a exising item or add a new single item from the items
    collecton.
    So the only means to modify the grid's items collection is modified the
    datasource and rebind the Template DataBound control.
    Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! [url]www.microsoft.com/security[/url]
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    [url]http://msdn.microsoft.com/asp.net/whidbey/default.aspx[/url]

    Steven Cheng[MSFT] Guest

  2. Similar Questions and Discussions

    1. Datagrid Master/Detail
      Hi, I have a Datagrid populated from a Remote Object -Select query- and I looking for a master/detail application to show the details in a...
    2. Editable datagrid in a master /detail relationship
      Hello. I am just beginning a web site with some special requirements. I need to create an editable datagrid, similar to the winforms datagrid:...
    3. datagrid(master) with a datalist(detail)
      Hello everybody, I want to do a master detail web form. I've made a datagrid as a master and put a datalist below which will show up the details...
    4. How display master/detail in DataGrid
      I have a WinForms app with a dataset containing two DataTables and a DataRelation linking the tables in a master/detail relationship. How do I...
    5. Master Detail detail
      Dear gurus, I have got a scenario where I have a master table its detail table its detail table. How to implement this using Data Grid or is...
  3. #2

    Default RE: Can I delete from the detail of a master/detail datagrid?

    Hi Mark,

    Thanks for your followup. As for capturing the event fired by the nested
    DataGrid, the general simple way is to define a event handler in the
    codebehind page class , declare it as "protected" or "public" and then
    wireup it to the nested grid in the aspx 's html source , for example, we
    define a

    protected void dgChild_DeleteCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    ....
    }

    handler in code behind and in html source of the aspx page, we wireup the
    event handler as below:
    <asp:DataGrid id="parentGrid" runat="server" AutoGenerateColumns="False">
    <Columns>
    ............ <asp:TemplateColumn HeaderText="OrderDetails">
    <ItemTemplate>
    <asp:DataGrid id="dgChild" runat="server" AutoGenerateColumns="False"
    OnDeleteCommand="dgDetails_DeleteCommand">
    .................



    That's ok. And tot make it clearly, I'm made a complete deme page which
    have two nested datagrids to display master/details datas and let the user
    delete the items in the child datagrid. You can have a test if you have
    anything unclear:

    ==============aspx page==================
    <HTML>
    <HEAD>
    <title>NestedGrid</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <table width="100%" align="center">
    <tr>
    <td>
    <asp:DataGrid id="dgOrders" runat="server"
    AutoGenerateColumns="False">
    <Columns>
    <asp:BoundColumn DataField="OrderID"
    HeaderText="OrderID"></asp:BoundColumn>
    <asp:BoundColumn DataField="CustomerID"
    HeaderText="CustomerID"></asp:BoundColumn>
    <asp:BoundColumn DataField="EmployeeID"
    HeaderText="EmployeeID"></asp:BoundColumn>
    <asp:BoundColumn DataField="OrderDate"
    HeaderText="OrderDate"></asp:BoundColumn>
    <asp:TemplateColumn HeaderText="OrderDetails">
    <ItemTemplate>
    <asp:DataGrid id="dgDetails" runat="server"
    AutoGenerateColumns="False" OnDeleteCommand="dgDetails_DeleteCommand">
    <Columns>
    <asp:TemplateColumn Visible="False" HeaderText="OrderID">
    <ItemTemplate>
    <asp:Label ID="lblOrderID" runat="server" Text='<%#
    DataBinder.Eval(Container, "DataItem.OrderID") %>'>
    </asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="ProductID">
    <ItemTemplate>
    <asp:Label runat="server" ID="lblProductID" Text='<%#
    DataBinder.Eval(Container, "DataItem.ProductID") %>'>
    </asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="UnitPrice"
    HeaderText="UnitPrice"></asp:BoundColumn>
    <asp:BoundColumn DataField="Quantity"
    HeaderText="Quantity"></asp:BoundColumn>
    <asp:ButtonColumn Text="Delete" HeaderText="Delete"
    CommandName="Delete"></asp:ButtonColumn>
    </Columns>
    </asp:DataGrid>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    </td>
    </tr>
    <tr>
    <td></td>
    </tr>
    </table>
    </form>
    </body>
    </HTML>


    ==========code behind page class===========
    public class NestedGrid : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid dgOrders;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    CacheDataSource();
    Bind_Data();
    }
    }

    protected void CacheDataSource()
    {
    //Create DataSet and Tables
    DataSet ds = new DataSet("dsOrderDetails");
    DataTable dtOrder = new DataTable("Order");
    dtOrder.Columns.Add("OrderID",typeof(int));
    dtOrder.Columns.Add("CustomerID",typeof(string));
    dtOrder.Columns.Add("EmployeeID",typeof(string));
    dtOrder.Columns.Add("OrderDate",typeof(DateTime));
    dtOrder.PrimaryKey = new DataColumn[]{dtOrder.Columns["OrderID"]};


    DataTable dtDetails = new DataTable("Details");
    dtDetails.Columns.Add("OrderID",typeof(int));
    dtDetails.Columns.Add("ProductID",typeof(int));
    dtDetails.Columns.Add("UnitPrice",typeof(System.De cimal));
    dtDetails.Columns.Add("Quantity",typeof(int));
    dtDetails.PrimaryKey = new
    DataColumn[]{dtDetails.Columns["OrderID"],dtDetails.Columns["ProductID"]};

    ds.Tables.Add(dtOrder);
    ds.Tables.Add(dtDetails);
    ds.Relations.Add(new
    DataRelation("odRelations",ds.Tables["Order"].Columns["OrderID"],ds.Tables["
    Details"].Columns["OrderID"],true));

    DataRow orderrow = null;
    DataRow detailrow = null;
    Random rnd = new Random((int)System.DateTime.Now.Ticks);

    //Fill Test Datas
    for(int i=1;i<=10;i++)
    {
    orderrow = dtOrder.NewRow();
    orderrow["OrderID"] = i;
    orderrow["CustomerID"] = "Customer_" + i;
    orderrow["EmployeeID"] = "Employee_" + i;
    orderrow["OrderDate"] = DateTime.Now.AddDays(i);
    dtOrder.Rows.Add(orderrow);

    int count = rnd.Next(1,5);

    for(int j=1;j<=count;j++)
    {
    detailrow = dtDetails.NewRow();
    detailrow["OrderID"] = i;
    detailrow["ProductID"] = j;
    detailrow["UnitPrice"] = j* 1.4567;
    detailrow["Quantity"] = rnd.Next(5,20);

    dtDetails.Rows.Add(detailrow);
    }
    }


    Session["DATASET"] = ds;

    }


    protected void Bind_Data()
    {
    DataSet ds = (DataSet)Session["DATASET"];

    dgOrders.DataSource = ds.Tables["Order"];
    dgOrders.DataBind();

    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);
    }


    private void InitializeComponent()
    {
    this.dgOrders.ItemDataBound += new
    System.Web.UI.WebControls.DataGridItemEventHandler (this.dgOrders_ItemDataBou
    nd);
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void dgOrders_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.AlternatingItem)
    {
    DataRowView drv = (DataRowView)e.Item.DataItem;
    System.Web.UI.WebControls.DataGrid dgChild =
    (System.Web.UI.WebControls.DataGrid)e.Item.Cells[4].FindControl("dgDetails")
    ;
    dgChild.DataSource = drv.CreateChildView("odRelations");
    dgChild.DataBind();
    }

    }

    protected void dgDetails_DeleteCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    DataSet ds = (DataSet)Session["DATASET"];
    DataTable dtDetails = ds.Tables["Details"];

    System.Web.UI.WebControls.DataGrid dgChild =
    (System.Web.UI.WebControls.DataGrid)source;
    Label lblOrderID =(Label)e.Item.Cells[0].FindControl("lblOrderID");
    Label lblProductID = (Label)e.Item.Cells[1].FindControl("lblProductID");
    int orderid = int.Parse(lblOrderID.Text);
    int productid = int.Parse(lblProductID.Text);

    DataRow row = dtDetails.Rows.Find(new object[]{orderid,productid});

    dtDetails.Rows.Remove(row);
    DataView dv = new DataView(dtDetails);
    dv.RowFilter = "OrderID = " + orderid;

    dgChild.DataSource = dv;
    dgChild.DataBind();


    }
    }
    ==================================

    Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! [url]www.microsoft.com/security[/url]
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    [url]http://msdn.microsoft.com/asp.net/whidbey/default.aspx[/url]


    Steven Cheng[MSFT] Guest

  4. #3

    Default RE: Can I delete from the detail of a master/detail datagrid?

    Hi Mark,

    Thanks for your followup. I'm sorry that I've no idea that you're a VB.NET
    guy, other wise I would have provided a VB.NET demo instead:)
    Yes, there are some tech articles in MSDN discussing on such hierarchial
    databinding scenario in webform. Here are two:

    #Hierarchical Data Binding in ASP.NET
    [url]http://msdn.microsoft.com/library/en-us/dnaspp/html/aspn-hierdatabinding.asp[/url]
    ?frame=true

    #Nested Grids for Hierarchical Data
    [url]http://msdn.microsoft.com/msdnmag/issues/03/10/CuttingEdge/[/url]

    But it seems that I've found any threads speicified to VB.NET code :(.
    In addition, here is a webpage which can help translate C# code to VB.NET,
    not sure whether you've ever used it:
    #C# to VB.NET Translator
    [url]http://authors.aspalliance.com/aldotnet/examples/translate.aspx[/url]

    Hope also helps. Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! [url]www.microsoft.com/security[/url]
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    [url]http://msdn.microsoft.com/asp.net/whidbey/default.aspx[/url]


    Steven Cheng[MSFT] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139