Link to home
Start Free TrialLog in
Avatar of xberry
xberryFlag for Germany

asked on

how to create editable datagrid within datagrid ?

Did successfully create a master-detail datagrid following the procedure
from Doug Seven on DotnetJunkies: http://dotnetjunkies.com/Article/47792CB0-0990-4BD8-BF84-B6063C4C9BBC.dcik
Even managed to add a column for edit and one for delete to the detail view.
So far it looks fine, see attached master_detail.jpg.
How to implement the functionality for edit and delete bugs me now.
I did this for single datagrid shemes before without a problem, but since
the second grid is loaded dynamically into master at run time,
postback will produce something else, i.e. clicking on edit on detail grid
only will display the master columns after reload.
How to trigger the click on edit event or delete event, so that it would work
same as in single datagrid-only procedure (such as displaying textfields where I can edit or insert data with a new cancel or update button appearing) ?  

See code below, I've included the binding event which is creating the detail grid
and the edit trigger which I had for single (master) data grid.

Uh, one thing: When I hover over the edit link in detail I notice following
command turning up in my browsers status bar:

  javascript:_doPostBack('MyDataGrid$ctl03$ctl00$ctl02$ctl00',")

So, obviously this is the command sent to server when clicking on the edit link.
Seems to come from master grid and lining up the positon of the control item where
event is trigered. Only I don't gt the clue how to use this fact for implementing
the functioning piece of code.

I know I try to run here with asp.net, before I can walk properly, but without
real a challinging project it would be no fun and you can be sure I know when it is
time to cry for help ;-)) So thanks for any advice on that.  
// Handling databinding events and dynamically inserting a detail datagrid
    public void MyDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {
        // this one worked before with the single datagrid edit:
        if (e.Item.ItemType == ListItemType.EditItem) {
            for (int i = 0; i < e.Item.Controls.Count; i++) {
                try {
                    if (e.Item.Controls[i].Controls[0].GetType().ToString() == "System.Web.UI.WebControls.TextBox") {
                        TextBox tb = (TextBox)e.Item.Controls[i].Controls[0];
                        if (i == 3) {
                            tb.Height = 30; 
                            tb.Width = 200;
                            tb.TextMode = TextBoxMode.MultiLine;
                        }
                        tb.Text = Server.HtmlDecode(tb.Text);
                    }
                }
                catch (Exception exc) {
                    Message.InnerHtml = "Error inserting row for editing";
                }
            }
        }
        //When each row is created in the DataGrid, eval the ItemType
        if (e.Item.ItemType == ListItemType.Item ||
          e.Item.ItemType == ListItemType.AlternatingItem) {
            //If the ItemType is Item or AlternatingItem,
            //Create a new DataGrid object named OrdersDataGrid
            DataGrid EvalDataGrid = new DataGrid();
 
            //Format the DataGrid to look cool.
            EvalDataGrid.BorderWidth = (Unit)1;
            EvalDataGrid.CellPadding = 4;
            EvalDataGrid.CellSpacing = 0;
            EvalDataGrid.GridLines = GridLines.Horizontal;
            EvalDataGrid.BorderColor = Color.FromName("Black");
            EvalDataGrid.ItemStyle.Font.Name = "Verdana";
            EvalDataGrid.ItemStyle.Font.Size = FontUnit.XSmall;
            EvalDataGrid.AlternatingItemStyle.BackColor = Color.FromName("LightGray");
            EvalDataGrid.ShowHeader = true;
            EvalDataGrid.HeaderStyle.BackColor = Color.FromName("Black");
            EvalDataGrid.HeaderStyle.ForeColor = Color.FromName("White");
            EvalDataGrid.HeaderStyle.Font.Bold = true;
            EvalDataGrid.HeaderStyle.Font.Size = FontUnit.XSmall;
 
            //Do not autogenerate columns.
            EvalDataGrid.AutoGenerateColumns = false;
 
            //****Add a series of BoundColumns****//
            //***Checklist ID***//
            BoundColumn bc = new BoundColumn();
            //Set the BoundColumn Values
            bc.HeaderText = "Cl Id";
            bc.DataField = "cl_Id";
            bc.ItemStyle.Wrap = false;
            //Add the BoundColumn to the OrdersDataGrid.
            EvalDataGrid.Columns.Add(bc);
 
            //***Beobachtung***//
            bc = new BoundColumn();
            bc.HeaderText = "Beobachtung";
            bc.DataField = "Beobachtung";
            bc.DataFormatString = "{0:d}";
            bc.ItemStyle.Wrap = false;
            EvalDataGrid.Columns.Add(bc);
 
            //***URL***//
            bc = new BoundColumn();
            bc.HeaderText = "URL";
            bc.DataField = "URL";
            bc.DataFormatString = "{0:d}";
            bc.ItemStyle.Wrap = false;
            EvalDataGrid.Columns.Add(bc);
 
            //***Korrektur***//
            bc = new BoundColumn();
            bc.HeaderText = "Korrektur";
            bc.DataField = "Korrektur";
            bc.DataFormatString = "{0:d}";
            bc.ItemStyle.Wrap = false;
            EvalDataGrid.Columns.Add(bc);
 
            //***Bemerkung***//
            bc = new BoundColumn();
            bc.HeaderText = "Bemerkung";
            bc.DataField = "Bemerkung";
            bc.DataFormatString = "{0:d}";
            bc.ItemStyle.Wrap = false;
            EvalDataGrid.Columns.Add(bc);
 
            //***Datum***//
            bc = new BoundColumn();
            bc.HeaderText = "Datum";
            bc.DataField = "Datum";
            bc.DataFormatString = "{0:d}";
            bc.ItemStyle.Wrap = false;
            EvalDataGrid.Columns.Add(bc);
 
            //***Editierbutton***//
            EditCommandColumn ecc = new EditCommandColumn();
            ecc.EditText = "Edit";
            ecc.CancelText = "Cancel";
            ecc.UpdateText = "Update";
            ecc.ItemStyle.Wrap = true;
            EvalDataGrid.Columns.Add(ecc);
 
            //***DeleteColumn***//
            ButtonColumn btc = new ButtonColumn();
            btc.Text = "Delete";
            btc.CommandName = "Delete";
            EvalDataGrid.Columns.Add(btc);
 
            //****End BoundColumns****//
 
            //Get the Authors DataView and filter it for the current ISBN
            DataView _orders = ds.Tables["Auswertungen"].DefaultView;
            _orders.RowFilter = "wk_Id='" + e.Item.Cells[0].Text + "'";
 
            //Bind the DataGrid.
            EvalDataGrid.DataSource = _orders;
            EvalDataGrid.DataBind();
 
            //Add the OrdersDataGrid to the BooksDataGrid.
            e.Item.Cells[3].Controls.Add(EvalDataGrid);
        }
    }
 
// For the single grid I had this edit triggering command
// Can't be used since datagrid triggering is the second one now (EvalDataGrid, see above)?
 
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E) {
        MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
        BindGrid(MySelect.Text);
    }

Open in new window

master-detail.jpg
Avatar of xberry
xberry
Flag of Germany image

ASKER

Almost finished it now myself !

Anyone who has interest in code let me know.
Avatar of xberry

ASKER

Solved it myself now, completely - so thanks to everyone who did not
respond, so that I could learn myself and be proud of my own
solution ; -))

Only a question back to Moderators, would it be possible to add a link here
that if anyone would ever be interested in how to solve the whole lot
(datagrid within datagrid, with option to add new rows to child datagrid, edit
or delete entries in child datagrid) I could be of any help ?

 
ASKER CERTIFIED SOLUTION
Avatar of xberry
xberry
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xberry

ASKER

If ever anyone has interest in more detailed code and solutions, please post a comment here.
If I find that more and more interest is in the code I'll certainly prepare it for fine publication here.
Avatar of rohan_koudinya
rohan_koudinya

Could you please post the solution here?
Avatar of xberry

ASKER

Hi Rohan,

problem is that I have not been in ASP.Net programming for three years now and the code I did at a firm
that I dont work with any longer. I have made a copy of my work though to local computer
but it may take some time to find the relevant code on my hard disk. I beg you for patience.
Meanwhile please try to work it out for yourselve from the instructions I gave under
'accepted' sulotion' up there in this thread.

Xberry
Avatar of xberry

ASKER

Hi Rohan,

I found the code, however, sinced it consists of multiple files, being part of a complete little
program it would be best you give me an email adress, so that I can send to you the zip file.
Then you can see yourself which part is useful for you.