locked
Reorder Rows in DataGridView

    שאלה

  • I was searching today for examples and tutorials for reordering rows in a datagridview.

     

    I found the following link:

    http://www.danielsoper.com/programming/DataGridViewDragDropRowsColumns.aspx

     

    The code at this link appears to accomplish my task through a drag and drop interface.  It is a C# example and I am just a novice VB guy.  But I know the advantage of the .NET languages is that you can consume classes and modules written in the other languages.

     

    Can someone help me get this working in my project?  I really have no idea where to start.  So far, I created a C# class file in VS 2005, copied the example code to the file, and saved it.  I then imported this existing class item into my VB project.  But I don't know how to use it.

     

    Thanks,

    Tom

    יום שישי 18 אפריל 2008 19:36

תשובות

  • This class is compiled as a subclass of the datagridview in c#.  You need to add the control to your toolbox and then drag it to your form.

     

    Choose "Add Item" by right clicking on the toolbox and then browsing to the location of the compiled dll for the control.

    שבת 19 אפריל 2008 13:19

כל התגובות

  • This class is compiled as a subclass of the datagridview in c#.  You need to add the control to your toolbox and then drag it to your form.

     

    Choose "Add Item" by right clicking on the toolbox and then browsing to the location of the compiled dll for the control.

    שבת 19 אפריל 2008 13:19
  • Hi Tom,

    You can use Csc.exe to compile the above C# code file to a DLL and add this control to your Toolbox or add the reference to it to implement this.

    Best regards,
    Riquel
    יום שלישי 22 אפריל 2008 12:47
    מנחה דיון
  • These are the steps I have taken so far.  I have not yet been able to actually implement this control.

     

    I copied the code from the web site I mentioned before.  I started VS 2005 with a blank C# project.  I then select "Add New Item..." from the Project pull down.  In the next dialog, I select the "Class" item.  I pasted the code into this new class.  Once pasted, I select Build by right clicking in the Solution Explorer.

     

    I then shut down this project within VS 2005 and opened my VB.NET solution where I want to implement the control. 

     

    I right clicked on the Toolbox to add a new item as described in your response.  I then browse to where the previous build of the DLL file is stored and select it.  I get an error window that states:

     

    There are no components in 'path to selected DLL' that can be placed on the toolbox.

     

    Can you provide me with detailed instructions on how I can use the control once the DLL is built?  Did I build the DLL correctly in the first place?


    Thanks,

    Tom

    יום שלישי 29 אפריל 2008 21:12
  • I'd hold off if I were you...

     

    I rewrote that control in VB and it doesn't quite work properly.  It also isn't the cleanest code (there are some unnecessary steps and slowdowns).

     

    I may redo this tomorrow for the fun of it... but I do have a question for you:  do you actually need to let the user drag the rows around to sort them?  The columns can be drug around by simply setting AllowUserToOrderColumns to True.  Normally you set the order of rows by sorting on a specific column value.  This is done in a totally different way than the code you are trying to use.  If you just need to specify the sort on a specific column, let us know because that would be easy to do.

     

    יום שלישי 29 אפריל 2008 22:12
    מנחה דיון
  •  

    My application is for a Computer Aided Drafting program we use here at my Engineering firm.  We prepare engineering plans with many sheets in them, sometimes hundreds of sheets.

     

    Each sheet will be a row in the data grid view.  I want to allow the user to change the order of the sheets in the data grid view which will change the order in which the sheets are sent to the printer for plotting.  My database has a "Set Order" field that will keep track of the individual sheet's position in the overall plan set.  I allow the user to sort by the column headings to find sheets easier in the overall list.  So whatever method I implement has to calculate the new "Set Order" and write it back to the database.

     

    I could implement the typical up/down arrow type of reordering that you see on a lot of web sites.  The user would click the up arrow and the row would swap places with the row immediately above that.  However, with the possibility of several hundred sheets, it would not be ideal to move the sheets one row at a time.  That is why I am looking for a drag and drop method, particularly one that would support selecting multiple continuous rows (i.e. a block of sheets) and dropping them elsewhere in the overall set.

     

    Lastly, the control is data bound.  But if there are other suggestions, I would be open to that as well.

     

    If you need more information, please ask.  I will try to provide as much as I can.

     

    Thanks,

    Tom

    יום רביעי 30 אפריל 2008 13:57
  • I don't think that code would work with a databound datagrid anyway.  Once a grid is databound the column sort rules dictate the row order - I don't think you can override that, but I may be wrong.

     

    You might be best served using a listbox or treeview to make selections, press a button to "Mark selection", click the new location in the list, the press a button to "Move selection".  Something along those lines...  I'm pretty sure that unless you determine the new "Set Order" first and then update each row, you won't be able reorder the rows at will with databinding.  You'll have to get your rows then manually fill a list with the values.

     

    יום חמישי 01 מאי 2008 22:15
    מנחה דיון