Duplicating a row in a data grid

Duplicating a row in a data grid

Old forum URL: forums.lhotka.net/forums/t/6639.aspx


pagolas posted on Thursday, March 19, 2009

really new to csla need help

JohnB replied on Thursday, March 19, 2009

Without knowing any details I'll assume that your grid is bound to a BusinessListBase.

Essentially capture the selected row object and clone it. Take the clone, change the id and add it to your BLB and rebind your grid.

There are many details left out here but hopefully this points you in the right direction.

John

pagolas replied on Tuesday, March 24, 2009

thank you very much

but i am fairly new to csla so i just have a few questions

1. Once the row is selected i will create a button that is called duplicate and once this button is pressed it shuld be able to create another row. Does csla have an interface that i can inherit from in order to clone lets say the object cloner interface i.e IClonable interface.

2. I have some of the code that i can post up sop that u understand better how it is bound to the BusinessListBase?

 

I really appreciate your help

esteban404 replied on Wednesday, March 25, 2009

It may be possible to cast the selected row into your specific object type, then use that to construct the duplicate row w/or without modification. I do it all the time in the case of RO list objects bound to a grid or drop down box where some users need to be able to add to that read only list. It's sweetly fast and easy. It works on a button click or context menu item click. Here's a sample from a DevExpress look up editor that allows this:
private void repLuRefDes_ProcessNewValue(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e)
{
// adds the new item and sets a reference in a different column based on it, direct relation exists
// between them
object row = repLuRefDes.GetDataSourceRowByKeyValue(e.DisplayValue);
RefDesInfo info = (RefDesInfo)row;
if (row == null) // because it's new, dead give away
{
RefDesInfo i = new RefDesInfo();
i.Part = "Unknown";
i.RefDes = e.DisplayValue.ToString();
i.OpType = MyHeader.ReportingOp;
i.Product = MyHeader.Info.Product;
_refDesList.MyAdd(i); // new constructor to allow adding to the list, does not modify db
gvDefects.SetRowCellValue(gvDefects.FocusedRowHandle, "Component", i.Part);
e.Handled = true;
}
else // row is present, grab value
gvDefects.SetRowCellValue(gvDefects.FocusedRowHandle, "Component", info.Part);
}

To get around the fact that I'm not modifying the datasource, this event also fires when the databinding sets the read-in data as a NotInList event. A life saver for the have cake and eat it too crowd I deal with.

_E

pagolas replied on Wednesday, March 25, 2009

thank you very much for your input it is greatly appreciated but this is wayyyyyyy to complicated for me man...i am a beginner and i dont even know what DevExpress Editor is . I was thinking of simply creating a duplicate button and then having the code to clone the object inside of that

pagolas replied on Wednesday, March 25, 2009

 

private void button1_Click(object sender, EventArgs e)

{

myList.DuplicateItem(myListDataGridView.CurrentRow.DataBoundItem);

}

myList is my collection of objects. It shows up in the datagrid when you do the search.

below is the code that shows the data binding nature of my myList

 

private void SortAndBindResults(myList list)

{

// clear previous results

myListBindingSource.DataSource = null;

if (list.Count > 0)

{

Csla.SortedBindingList<myListInfo> sortedList = new Csla.SortedBindingList<myListInfo>(list);

//sortedList.ApplySort("IsWorkCommitted", ListSortDirection.Descending);

sortedList.ApplySort("myListId", ListSortDirection.Ascending);

myListListBindingSource.DataSource = sortedList;

myListDataGridView.Focus();

}

else

{

MessageBox.Show("No matching lists found",

"No matching lists", MessageBoxButtons.OK);

}

}

esteban404 replied on Wednesday, March 25, 2009

What is the question?

esteban404 replied on Wednesday, March 25, 2009

OK, I see your question. I got the post out of series.

You're using a DataGridView and so does the editor I use, so the technique is similar. Your object just implements it instead of the UI where I do it. So your DuplicateItem method takes the selected row as an argument and creates the new item from it and resorts the list, right? After you've done that, you may need to select that new item in the list so the user doesn't need to, but that's up to your design. I don't know.

By definition, the list item may need to be different/unique in the list, not an exact copy, so you may nee to do something different, but at this point, I don't have a clue about the details of the problem from code posting.

Post some question and the community here will try to help.

_E

Copyright (c) Marimer LLC