简体   繁体   English

C#Gridview,重新排列

[英]C# Gridview, rearrange

I really didnt know what to put to the title so sorry about that. 我真的不知道该怎么做标题所以很抱歉。 I have 2 colums where i list files from 2 folders. 我有2个列,我列出了2个文件夹中的文件。 Now what i would like to do is to give a user ability to rearrange the files by clicking on one file in one column and then on the other file in other column. 现在我想做的是让用户能够通过单击一列中的一个文件然后单击其他列中的另一个文件来重新排列文件。 The application merges these two files together. 应用程序将这两个文件合并在一起。

Just onother option would be just use datagrid and its drag and drop functionality (one column is static and one rearrangable with drag and drop or something) but doing it this way isnt really what i would like.. 另外一个选择就是使用datagrid及其拖放功能(一列是静态的,一个可以拖放或者其他东西重新排列)但是这样做真的不是我想要的。

So, all options are welcome.. 所以,欢迎所有选择..

EDIT: Using WinForms, it doesnt have to be gridview, just i couldnt think of anything else.. 编辑:使用WinForms,它不必是gridview,只是我想不出其他任何东西..

I would just create two DataGridView 's (or ListView if you prefer), the first listing the files in the folder A and the second listing the files in the folder B . 我只想创建两个DataGridView (或者你喜欢的ListView ),第一个列出文件夹A中的文件,第二个列出文件夹B中的文件。

Then allow to select just one row at a time in both grids ( MultiSelect = false , SelectionMode = FullRowSelect ) and add a button called "Merge Selected", that simply merges the file selected in the first grid with the one selected in the second grid. 然后允许在两个网格中一次只选择一行( MultiSelect = falseSelectionMode = FullRowSelect )并添加一个名为“Merge Selected”的按钮,它只是将第一个网格中选择的文件与第二个网格中选择的文件合并。

  1. Use 2 list boxes linked to each folder so that the use can scroll up/down to select the file they need 使用链接到每个文件夹的2个列表框,以便使用可以向上/向下滚动以选择所需的文件
  2. Have button "Merge Files" which gets enabled when an item from both listboxes are selected. 具有“合并文件”按钮,当选择两个列表框中的项目时,该按钮将启用。
  3. When user clicks on "Merge Files" have a confirmation box to make sure its not clicked my mistake. 当用户点击“合并文件”时有一个确认框,以确保它没有点击我的错误。

I'll assume you're talking about DataGridView , as DataGrid has been deprecated. 我假设您正在讨论DataGridView ,因为DataGrid已被弃用。

In the designer, make sure SelectionMode on the DataGridView is set to CellSelect . 在设计器中,确保DataGridView上的SelectionMode设置为CellSelect Then in properties --> events, double-click the SelectionChanged event to create a new method which handles that event. 然后在properties - > events中,双击SelectionChanged事件以创建处理该事件的新方法。

Add this code to the method: 将此代码添加到方法中:

private DataGridViewCell _lastCellSelected = null;
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
    if(dataGridView.SelectedCells.Count == 0)
    {
        _lastCellSelected = null;
        return;
    }

    DataGridViewCell selectedCell = dataGridView.SelectedCells[0];
    if(_lastCellSelected == null || selectedCell.ColumnIndex == _lastCellSelected.ColumnIndex)
    {
        //User clicked first cell
        _lastCellSelected = selectedCell;
    }
    else
    {
        //User has clicked two cells from different columns
        string filename1 = _lastCellSelected.Value;
        string filename2 = selectedCell.Value;

        //TODO: "Merge" files here

        _lastCellSelected = null;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM