Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid : move up/down rows in code

Status
Not open for further replies.

vbkal

Programmer
Jan 30, 2002
13
BE
Hello,

I have a window application with a datagrid on it. (Data bound from a dataset). Each row has an order number (stoporder).
Now I want the user to be able to move the rows up and down with an up and down button(and automatically change the number).

I can do this ok, if I manually click the order number first. (Then my rows move up and down and the number alters.) But how can I do this in the code? (how can I programmaticaly force a sort on a column?)
- I've tried the DataView with DefaultView.Sort="stopOrder asc" , but nothing changes ?
-
If I sort on any other column, then the up/down goes wrong too.
What I would like to do is hide the order column and sort it manually. I would prevent the user from sorting on other columns.

Any suggestions?
Thx for the help,
kathleen

<code>
DataSet m_dataSetRouteStops;
DataGrid dtRouteStops;

// push the up button --> switch this and the previous row
private void btnRouteStopUp_Click(object sender, System.EventArgs e)
{
DataRow row;
int stopOrder;
DataRowCollection rowCollection;
int inc;

try
{
row = GetRouteStopsRow ();
dtRouteStops.UnSelect(dtRouteStops.CurrentRowIndex);
inc=dtRouteStops.CurrentRowIndex;

if (null != row)
{
stopOrder = Convert.ToInt32(row[FLD_STOPORDER]);
if (stopOrder > 1)
{
stopOrder -= 1;
row[IMS.FLD_STOPORDER] = 0;
rowCollection = m_dataSetRouteStops.Tables[TBL_ROUTESTOPS].Rows;
foreach (DataRow rowMove in rowCollection)
{
if (DataRowState.Deleted != rowMove.RowState)
{
if ((row != rowMove) && (Convert.ToInt32(rowMove[FLD_STOPORDER]) == stopOrder))
{
rowMove[FLD_STOPORDER] = stopOrder + 1;
row[FLD_STOPORDER] = stopOrder;
break;
}
}
}
}
}
}
dtRouteStops.Select(inc-1);
}
catch (System.IndexOutOfRangeException ex)
{}
catch (Exception exc)
{/*error message*/}
}

</code>


 
I must have overlooked something, because the
the DataView with DefaultView.Sort="stopOrder asc" works fine for sorting in code.

But now I am faced with a new problem. How do I set the manual sorting to false, allowing code to sort, but not the users of the form?

kathleen
 
Cant you make the datagrid columnheader disabled so that user cant click on it(and hence cant sort)?
 
The other option is
see if the datagrid has a property called AllowSorting
and set it false
 
The last option was what I tried, but I also use a TableStyle.

The AllowSorting property of the TableStyle apparently overwrites the AllowSorting property of the datagrid !

so I have to set both to false to not allow sorting.

Problem solved.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top