Move up and down the row in the datagridview

Source: Internet
Author: User

1) if dgoutimage. datasource = set (recfileargs)

(Move up)

Dgoutimage. datasource = new bindinglist <recfileargs> (recfileargs1 );

// If multiple rows are selected

If (dgoutimage. selectedrows. Count> 1)

{

Return;

}

Dgoutimage. Focus ();

// The currently selected row number

Int rowindex = dgoutimage. currentrow. index;

If (rowindex! = 0)

{

Recfileargs _ recccurr = dvrplaylistright [rowindex];

Recfileargs _ reccper = dvrplaylistright [rowindex-1];

Dvrplaylistright [rowindex] = _ reccper;

Dvrplaylistright [rowindex-1] = _ recccurr;

Dgoutimage. datasource = dvrplaylistright;

Dgoutimage. Refresh ();

Dgoutimage. Rows [rowindex-1]. Selected = true;

Dgoutimage. currentcell = dgoutimage. Rows [rowindex-1]. cells [0];

}

If (rowindex-1 = 0)

{

Up button. Enabled = false;

}

Else

{

Up button. Enabled = true;

}

2) If dgoutimage. datasource = datatable

(Move down)

Object [] _ rowdata = datatable. Rows [I]. itemarray;

Datatable. Rows [I]. itemarray = datatable. Rows [I + 1]. itemarray

Datatable. Rows [I + 1]. itemarray = _ rowdata;

========================================================== ========

Set sortorder int in the database table.

SQL code
     insert Tb (title, sortorder) Select @ title, (select isnull (max (sortorder), 0) + 1 from TB) as sortorder create procedure tb_move (@ ID int, @ upordown nchar (4) as declare @ currentorder int, @ exorder int select @ currentorder = sortorder from TB where id = @ ID if @ upordown = 'up' select top 1 @ exorder = sortorder from TB where sortorder <@ currentorder order by sortorder DESC if @ upordown = 'low' select top 1 @ exorder = sortorder from TB where sortorder> @ currentorder order by sortorder ASC if @ exorder is null return else begin update TB set sortorder = 0 where sortorder = @ currentorder update TB set sortorder = @ currentorder where sortorder = @ exorder update TB set sortorder = @ exorder where sortorder = 0 end go 
C # code
   Public void upordown (int id, string upordown) {try {sqlparameter [] sqlparams = new sqlparameter [] {New sqlparameter ("@ ID", ID ), new sqlparameter ("@ upordown", upordown)}; dbhelper. executenonquery ("tb_move", sqlparams);} catch {}} protected void gridview_romcommand (Object sender, gridviewroweventargs e) {swith (E. commandname) {Case "up": // note that you must add the commandname = "up" attribute to the button to move up, And commandargument = '<% # eval ("ID ") %> 'upordown (Int. parse (E. commandargument. tostring (), "up"); bindgridview (); break; Case "down": // note that you must add the commandname = "down" attribute to the button to be moved up at the front end, and commandargument = '<% # eval ("ID") %>' upordown (Int. parse (E. commandargument. tostring (), "down"); bindgridview (); break ;}} private void bindgridview () {dataset DS = dbhelper. excutenoquery ("select * from TB order by sortorder ASC"); // obtain by sequence ASC. and bound ,}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.