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

Row''s Move Inside the DataboundGrid

Clay, How to move Row''s inside the DataBoundGrid. Thanks Satish

7 Replies

AD Administrator Syncfusion Team July 9, 2004 06:22 AM UTC

A GridDataBoundGrid just displays what is presented to it by its DataSource. So, your question really is how do you move items in your DataSource? Of course, this depends on the DataSource. If your DataSource is something you can easily work with, like an ArrayList, you can move rows by switching elements in the ArrayList. If your datasource is a DataTable, then this is a problem as moving rows around in a DataTable is not supported by the framework. One thing you can do is to use a DataView as the DataSource, add an index column to the DataTable and use this column is a sorted dataview to control the order rows are displayed. Here is a sample.


SA Satish July 9, 2004 07:18 AM UTC

Thanks clay. But this code doesn''t work if you delete row inbetween. How can we handle? Thanks Satish >A GridDataBoundGrid just displays what is presented to it by its DataSource. So, your question really is how do you move items in your DataSource? > >Of course, this depends on the DataSource. If your DataSource is something you can easily work with, like an ArrayList, you can move rows by switching elements in the ArrayList. > >If your datasource is a DataTable, then this is a problem as moving rows around in a DataTable is not supported by the framework. One thing you can do is to use a DataView as the DataSource, add an index column to the DataTable and use this column is a sorted dataview to control the order rows are displayed. Here is a sample.


AD Administrator Syncfusion Team July 9, 2004 07:27 AM UTC

In the sample, if I click a row header and press the delete key to delete a row, clicking the up and down buttons still work for me. I am using 2.0.5.1 version of our libraries. What are you using?


SA Satish July 9, 2004 07:50 AM UTC

I am so sorry. Delete along with Insert Functionality. I have luck in inserting a row in between the grid rows but when I start up & down i am not able to move. using - 2.0.5.1 version Thanks Satish >In the sample, if I click a row header and press the delete key to delete a row, clicking the up and down buttons still work for me. I am using 2.0.5.1 version of our libraries. What are you using?


AD Administrator Syncfusion Team July 9, 2004 08:42 AM UTC

Here is a button handler that works for me in the sample to insert a row at the current position.
private void button3_Click(object sender, System.EventArgs e)
{
	this.gridDataBoundGrid1.BeginUpdate();
	CurrencyManager cm = (CurrencyManager)this.BindingContext[this.gridDataBoundGrid1.DataSource, this.gridDataBoundGrid1.DataMember];
	DataView dv = (DataView)cm.List;
	int fromPos = cm.Position;
	int toPos = cm.Count;
	string saveSort = dv.Sort;
	dv.Sort = "";
	cm.AddNew();
	DataRowView drv1 = (DataRowView) cm.List[toPos];
	drv1.Row["sortKey"] = toPos;
	dv.Sort = saveSort;
	for(int i = toPos; i > fromPos; i--)
	{
		Swap(i, i-1);
		cm.EndCurrentEdit();
	}
	cm.Position = fromPos;
	this.gridDataBoundGrid1.EndUpdate();
}


SA Satish July 13, 2004 07:33 AM UTC

How can we move a row to a specified position. Example - 10 Rows. I would like to enter 5row in the textbox the selected row should be moved to the 5th row. The functionality should work even for multiple row selection. Secondly, if the user selects just a column, i should able to hightlight whole row. How can we do this? Thanks Satish >Here is a button handler that works for me in the sample to insert a row at the current position. > >
>private void button3_Click(object sender, System.EventArgs e)
>{
>	this.gridDataBoundGrid1.BeginUpdate();
>	CurrencyManager cm = (CurrencyManager)this.BindingContext[this.gridDataBoundGrid1.DataSource, this.gridDataBoundGrid1.DataMember];
>	DataView dv = (DataView)cm.List;
>	int fromPos = cm.Position;
>	int toPos = cm.Count;
>	string saveSort = dv.Sort;
>	dv.Sort = "";
>	cm.AddNew();
>	DataRowView drv1 = (DataRowView) cm.List[toPos];
>	drv1.Row["sortKey"] = toPos;
>	dv.Sort = saveSort;
>	for(int i = toPos; i > fromPos; i--)
>	{
>		Swap(i, i-1);
>		cm.EndCurrentEdit();
>	}
>	cm.Position = fromPos;
>	this.gridDataBoundGrid1.EndUpdate();
>}
>


AD Administrator Syncfusion Team July 13, 2004 08:28 AM UTC

I think you will have to move rows one at teh time using the same techniques as above. Below is a try at this. I am not sure what selecting columns has to do with selecting rows. If you are using ListBoxSelectionMode, and want to also be able to select a column, then you will have to do this yourself. See this form thread. http://www.syncfusion.com/forums/message.asp?MessageID=16321
private void button4_Click(object sender, System.EventArgs e)
{
	int moveToRow = int.Parse(this.textBox1.Text);
	GridRangeInfo rangeToMove = this.gridDataBoundGrid1.Selections.Ranges.ActiveRange;
	if(!rangeToMove.IsEmpty)
	{
		this.gridDataBoundGrid1.BeginUpdate();
		int distance = moveToRow - rangeToMove.Top;
		if(distance > 0)
		{
			for(int i = rangeToMove.Bottom; i >= rangeToMove.Top; --i)
			{
				for(int j = 0; j < distance; ++j)
					Swap(i+j, i+j-1);
			}
		}
		else if(distance < 0)
		{
			for(int i = rangeToMove.Top; i <= rangeToMove.Bottom; ++i)
			{
				for(int j = rangeToMove.Bottom-1; j >= moveToRow; --j)
					Swap(j-1, j);
			}
		}
		this.gridDataBoundGrid1.Selections.Clear();
		this.gridDataBoundGrid1.Focus();
		this.gridDataBoundGrid1.Binder.CurrentPosition = moveToRow - 1;
		this.gridDataBoundGrid1.EndUpdate();
		this.gridDataBoundGrid1.Refresh();
	}
}

Loader.
Live Chat Icon For mobile
Up arrow icon