We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Cut/Insert cut cell

Hi, I have seen samples.But can we just move the cut row or rows to the some position so that it will be inserted.Behavior similar to the Excel. Do you have sample?

9 Replies

AD Administrator Syncfusion Team December 9, 2004 08:05 AM UTC

You can move rows in a grid by setting the grid.AllowDragSelectedRows property. Take a look at the grid\samples\quickstart\cellstyles sample. If you select a row by clicking a row header (or select several rows by drag-selecting the row headers), then you can then mouse down on one of the selected header cells and drag the selected rows to another position in the grid.


AD Administrator Syncfusion Team December 9, 2004 08:18 AM UTC

No i dont want to drag them. I have attached the sample. i want to cut one or more than one row and insert. I have attached my Sample program.can you tell where it is going wrong. It doesnt cut the selected rows. Also tooltip attached are not working private DataTable dt; private int mouseDownRow = -1; private int mouseDownCol = -1; private void menuItem1_Click(object sender, System.EventArgs e) { //delete if(this.mouseDownRow > -1 && this.mouseDownCol > -1) { this.gridDataBoundGrid1.Model.CutPaste.Cut(); int position = this.gridDataBoundGrid1.Binder.RowIndexToPosition(mouseDownRow); GridRangeInfoList ranges = this.gridDataBoundGrid1.Selections.GetSelectedRows(true, false); //ranges.mo foreach(GridRangeInfo range in ranges) { for(int i = range.Bottom; i >= range.Top; i--) { this.dt.Rows.RemoveAt(i); dt.AcceptChanges(); gridDataBoundGrid1.Refresh(); } } } } int numRowsCut=0; private void menuItem2_Click(object sender, System.EventArgs e) { //insert if(this.mouseDownRow > -1 && this.mouseDownCol > -1) { int position = this.gridDataBoundGrid1.Binder.RowIndexToPosition(mouseDownRow); DataRow dr = dt.NewRow(); this.dt.Rows.InsertAt(dr, position); dt.AcceptChanges(); gridDataBoundGrid1.Refresh(); this.gridDataBoundGrid1.Model.CutPaste.Paste(); } } private void Model_ClipboardCut(object sender, GridCutPasteEventArgs e) { char[]a = {''''}; DataObject data = (DataObject) Clipboard.GetDataObject(); if(data.GetDataPresent(DataFormats.Text)) { string s = (string)data.GetData(DataFormats.Text); string[] rows = s.Split(a); numRowsCut = rows.GetLength(0); } } private void Form1_Load(object sender, System.EventArgs e) { dt= new DataTable("MyTable"); int nCols = 4; int nRows = 10; for(int i = 0; i < nCols; i++) dt.Columns.Add(new DataColumn(string.Format("Col{0}", i))); for(int i = 0; i < nRows; ++i) { DataRow dr = dt.NewRow(); for(int j = 0; j < nCols; j++) dr[j] = string.Format("row{0} col{1}", i, j); dt.Rows.Add(dr); } this.gridDataBoundGrid1.DataSource = dt; gridDataBoundGrid1[0,1].CellTipText = "Header Column1"; gridDataBoundGrid1[0,1].BackColor = Color.Aquamarine; gridDataBoundGrid1[0,2].CellTipText = "Header Column2"; gridDataBoundGrid1[0,2].BackColor = Color.Aquamarine; gridDataBoundGrid1[0,3].CellTipText = "Header Column3"; gridDataBoundGrid1[0,4].CellTipText = "Header Column4"; gridDataBoundGrid1[1,1].CellTipText = "Column1"; gridDataBoundGrid1[1,1].BackColor = Color.Aquamarine; gridDataBoundGrid1[1,2].CellTipText = "Column2"; gridDataBoundGrid1[1,3].CellTipText = "Column3"; gridDataBoundGrid1[1,4].CellTipText = " Column4"; gridDataBoundGrid1.ThemesEnabled = true; this.gridDataBoundGrid1.ListBoxSelectionMode = SelectionMode.MultiExtended; this.gridDataBoundGrid1.Model.Options.SelectCellsMouseButtonsMask = MouseButtons.Left; //avoid complexity of trying to have context menu on actively editing cell //this.gridDataBoundGrid1.ActivateCurrentCellBehavior = GridCellActivateAction.None; this.gridDataBoundGrid1.ContextMenu = this.contextMenu1; } //save mousedown row and col private void gridDataBoundGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { Point pt = new Point(e.X, e.Y); this.gridDataBoundGrid1.PointToRowCol(pt, out this.mouseDownRow, out this.mouseDownCol, -1); } >You can move rows in a grid by setting the grid.AllowDragSelectedRows property. > >Take a look at the grid\samples\quickstart\cellstyles sample. If you select a row by clicking a row header (or select several rows by drag-selecting the row headers), then you can then mouse down on one of the selected header cells and drag the selected rows to another position in the grid.


AD Administrator Syncfusion Team December 9, 2004 08:46 AM UTC

You cannot set cell style properties like CellTipText on individual cells. You eithe rhave to do it column-wide using the GridBoundColumn,StyleInfo proeprty for that cell, or use an event like Model.QueryCellInfo or PrepareViewStyleInfo to set these properties. See the GridDataBoundGrid section of our KB for a couple of KB''s on setting cetain cell specific properties like BackColor and ReadOnly. There is no simple way to rearrange rows in a GridDataBoundGrid (other than sorting). The reason that the grid only displays the exact contents of the DataSource in the order that the datasource present its. So, to reaarange things means to rearrange the datasource. And I do not know how to rearrange DataRows in a DataTable so they will always appear in the proper order in a control (our GridDataBoundGrid or a Windows Forms DataGrid or ???) bound to the DefaultView on the DataTable. Now you can move (cut/insert) in a GridControl that holds its own data as it can insert rows into any position. There are ways to try to simulate the behavior you want, but it takes some effort. One way is to add a SortOrderKey column that will be used to display the datasource source always sorted by this column. So, to move row (cut them form one place and put them in another), you can just re-assign the sortorderkey values. Here is forum thread that discusses this idea and has a sample. http://64.78.18.34/Support/Forums/message.aspx?MessageID=4419


AD Administrator Syncfusion Team December 10, 2004 12:26 AM UTC

Now im removing and inserting the rows. but there is one problem When i am removing more than one rows and inserting at some place ,those get inserted but when clicked at the end one blank row gets inserted at the end and newly inserted rows contents are removed. I have attached my sample.Can you see any problem in the way i am doing. Also if line this.gridDataBoundGrid1.Model.CutPaste.Paste(); is commented lines get inserted properly.But when and how to paste the contents Is there problem in above line Regards >You cannot set cell style properties like CellTipText on individual cells. You eithe rhave to do it column-wide using the GridBoundColumn,StyleInfo proeprty for that cell, or use an event like Model.QueryCellInfo or PrepareViewStyleInfo to set these properties. See the GridDataBoundGrid section of our KB for a couple of KB''s on setting cetain cell specific properties like BackColor and ReadOnly. > >There is no simple way to rearrange rows in a GridDataBoundGrid (other than sorting). The reason that the grid only displays the exact contents of the DataSource in the order that the datasource present its. So, to reaarange things means to rearrange the datasource. And I do not know how to rearrange DataRows in a DataTable so they will always appear in the proper order in a control (our GridDataBoundGrid or a Windows Forms DataGrid or ???) bound to the DefaultView on the DataTable. > >Now you can move (cut/insert) in a GridControl that holds its own data as it can insert rows into any position. > >There are ways to try to simulate the behavior you want, but it takes some effort. One way is to add a SortOrderKey column that will be used to display the datasource source always sorted by this column. So, to move row (cut them form one place and put them in another), you can just re-assign the sortorderkey values. Here is forum thread that discusses this idea and has a sample. >http://64.78.18.34/Support/Forums/message.aspx?MessageID=4419 > > CutInsert_9195.zip


AD Administrator Syncfusion Team December 10, 2004 07:11 AM UTC

The only way I know to get the inserts to stick is to always call dt.AcceptChanges as you leave the row.
private void gridDataBoundGrid1_RowLeave(object sender, GridRowEventArgs e)
{
	dt.AcceptChanges();
}


AD Administrator Syncfusion Team December 13, 2004 12:22 AM UTC

Thankz Clay it worked. Like cut and insert of rows can we do Cut and insert of columns? how? >The only way I know to get the inserts to stick is to always call dt.AcceptChanges as you leave the row. > >
>private void gridDataBoundGrid1_RowLeave(object sender, GridRowEventArgs e)
>{
>	dt.AcceptChanges();
>}
>


AD Administrator Syncfusion Team December 13, 2004 06:37 AM UTC

The GridDataBoundGrid supports dragging columns just setting AllowDragSelectedCols and making sure the AllowSelections includes the Columns flag. Your user just clicks a column header (or drags several headers) to select the column. and then the user can mouse back down on the header and drag the selected columns to a new location. This is not cut and paste, but it will move teh columns around without you doing additional work. If you want to cut and paste, you have 2 options. In your menu handler, if columns are selected, you could either remove teh columns from the datasource using datatable.Columns.Remove/RemoveAt methods, or you could use grid.GridBoundColumns.Remove/RemoveAt methods to remove the GridBoundColumn from the grid. (If you do not explicitly add GridBoundColumns, then you would work with the grid.Binder.InternalColumns instead of the grid.GridBoundColumns). Then to insert you would work with the same objects removed to put back into the appropriate collection in the position you need. After doing this, you would likely need to call some grid methods to maake sure the changes are initialzed for the displayed grid. You might need to call grid.Binder.InitializeColumns and/or grid.Refresh.


AD Administrator Syncfusion Team December 13, 2004 08:01 AM UTC

I want to do using cut and paste. But there is not any method which would insert the column at some position. For removing we have REmoveAt() >The GridDataBoundGrid supports dragging columns just setting AllowDragSelectedCols and making sure the AllowSelections includes the Columns flag. Your user just clicks a column header (or drags several headers) to select the column. and then the user can mouse back down on the header and drag the selected columns to a new location. > >This is not cut and paste, but it will move teh columns around without you doing additional work. > >If you want to cut and paste, you have 2 options. In your menu handler, if columns are selected, you could either remove teh columns from the datasource using datatable.Columns.Remove/RemoveAt methods, or you could use grid.GridBoundColumns.Remove/RemoveAt methods to remove the GridBoundColumn from the grid. (If you do not explicitly add GridBoundColumns, then you would work with the grid.Binder.InternalColumns instead of the grid.GridBoundColumns). > >Then to insert you would work with the same objects removed to put back into the appropriate collection in the position you need. After doing this, you would likely need to call some grid methods to maake sure the changes are initialzed for the displayed grid. You might need to call grid.Binder.InitializeColumns and/or grid.Refresh.


AD Administrator Syncfusion Team December 13, 2004 10:03 AM UTC

You will have to create a new GridBoundColumns collection, and then add the exisitng columns (or maybe clones of the existing columns) in the new order with your inserted column included in teh proper positions. To give you the idea, here is a forum thread that has code to serialize GridBoundColumn order to disk, an dthen bring the order back in. You could do something similar to the ''bring back in code'' to create a new GridBoundColumns collection that has teh columns in teh order you want them. http://64.78.18.34/Support/Forums/message.aspx?MessageID=3918

Loader.
Live Chat Icon For mobile
Up arrow icon