c# - I want to change order of colums of listview -


how can make c#? last colum must first , first colum must last.

thanks.

i have listview  name   age  school  phone      job ----   ---  ------  -----      --- helen  18    ktu     5224511   student sarah  25    hitan   2548441   engineer david  19    havai   2563654   student   want make  name   job       phone    school   age ----   ---       -----    -----    --- helen  student   5224511   ktu     18 sarah  engineer  2548441   hitan   25 david  student   2563654   havai   19 

how can make c#?

thanks.

it seems have set (re-arrange) columns in desired display order:

  // job (2nd column display)   mylistview.columns[4].displayindex = 1;    // phone (3d column display)   mylistview.columns[3].displayindex = 2;    // school (4th)   mylistview.columns[2].displayindex = 3;    // age (5th)   mylistview.columns[1].displayindex = 4;  

you can use little bit more elaborated code well:

   public static void arrangecolumns(listview listview, params string [] order) {       listview.beginupdate();        try {          (int = 0; < order.length; ++i) {           boolean found = false;            (int j = 0; j < listview.columns.count; ++j) {             if (string.equals(listview.columns[j].text, order[i], stringcomparison.ordinal)) {               listview.columns[j].displayindex = i;                found = true;                break;             }           }            if (!found)              throw new argumentexception("column " + order[i] + " not found.", "order");          }       }        {         listview.endupdate();       }     }    ...    arrangecolumns(mylistview, "name", "job", "phone", "school", "age"); 

finally, if want re-arrange subitems (move data, not view change) that:

public static void swapsubitems(listview listview, int fromindex, int toindex) {   if (fromindex == toindex)     return;    if (fromindex > toindex) {     int h = fromindex;     fromindex = toindex;     toindex = h;   }    listview.beginupdate();    try {     foreach (listviewitem line in listview.items) {       var subitem = line.subitems[fromindex];       line.subitems.removeat(fromindex);       line.subitems.insert(toindex, subitem);        if (listview.columns.count > toindex) {         var column = listview.columns[fromindex];         listview.columns.removeat(fromindex);         listview.columns.insert(toindex, column);       }     }   }   {     listview.endupdate();   } }  ...  mylistview.beginupdate();  try {   // job    swapsubitems(mylistview, 4, 1);   // phone (now, when job moved, phone 4th column)   swapsubitems(mylistview, 4, 2);   // school (now, when job , phone moved, school 4th column)   swapsubitems(mylistview, 4, 3); } {   mylistview.endupdate(); } 

Comments