简体   繁体   English

在C#,Gridview,Winforms中的数据网格中交换行

[英]interchanging rows in a datagrid in C#,Gridview,Winforms

I am doing a winforms application in C#.net I have a set of data in a grid view (populated based on textbox entry. If I enter ID in textbox, its corresponding data in grid view is shown as follows: 我在C#.net中做一个winforms应用程序,我在网格视图中有一组数据(根据文本框条目填充。如果我在文本框中输入ID,则其在网格视图中的对应数据如下所示:

        A  B    C     D 
       100 1   30500  null
       100 1   23000  null
       100 1   50000  null
       100 2   23000  null
       100 2   31300  null
       100 2   50000  null

In the above data, the value 50000 in column C has 2 sub items each corresponding to the value in column B(30500,23000-> value 1 in B and 23000,31300-> value 2 in column B). 在上面的数据中,C列中的值50000具有2个子项,每个子项分别对应于B列中的值(B中的30500,23000->值1和B列中的23000,31300->值2)。 I would like to see the table as follows.: 我希望看到以下表格:

          A  B    C     D 
       100 1   50000  null
       100 1   30500  null
       100 1   23000  null
       100 2   50000  null
       100 2   23000  null
       100 2   31300  null

I would like to have the row corresponding to the value 50000( highest) to appear first to show that the below 2 entries (beneath each 50000) are its sub items.
     I do not know how to do that since I am a beginner in c# Winforms.     
     Appreciate any help. Thanks in advance.

.

You have to sort the DataSource that is being used for your datagridView. 您必须对用于datagridView的数据源进行排序。 I am assuming that a datatable is the data source for you. 我假设数据表是您的数据源。 you have to apply sort twice one by column B as ascending then Column C as descending. 您必须对B列进行一次升序排序,然后对C列进行降序两次排序。

if dataTable1 is your datasource then somrthing similar like the below you need to do .. 如果dataTable1是您的数据源,则类似下面的内容,您需要做..

DataView view = new DataView(dataTable1);
view.Sort = "B ASC, C DESC";
DataTable newTable = view.ToTable();

and then use NewTable As your data source. 然后使用NewTable作为您的数据源。

Other way is to handle from the DB side by adding ORDER BY B ASC, C DESC at the end of your current query. 另一种方法是从数据库端通过在当前查询的末尾添加ORDER BY B ASC和C DESC来进行处理。

The answer by kishore VM will work if you have a datatable as your data source, here is another way to do it that works regardless of the data source. 如果您有数据表作为数据源,则kishore VM的答案将起作用,这是另一种有效的方法,无论数据源如何。

Register on the DataGrid SortCompare and handle the sort comparison yourself. 在DataGrid SortCompare上注册并自己处理排序比较。

    dgv.SortCompare += new DataGridViewSortCompareEventHandler(OnDataGridViewSortCompare);

    void OnDataGridViewSortCompare(object sender, DataGridViewSortCompareEventArgs e)
    {
        int retVal = String.Compare(e.CellValue1.ToString(), e.CellValue2.ToString());
        //They are ==, Compare by next column
        if (retVal == 0)
        {
            retVal = String.Compare(dgv[e.Column.Index + 1, e.RowIndex1].Value.ToString(),
            dgv[e.Column.Index + 1, e.RowIndex2].Value.ToString()) * -1; //Multiply by -1 to flip the ASC sort to DESC
        }
        e.SortResult = retVal;
        e.Handled = true;
    }

I am unsure if you are storing strings or int values so i used string compare. 我不确定是否要存储字符串或整数值,所以我使用了字符串比较。 If you want a true numeric compare you need to cast/parse the cell values to int and use your own int compare method since int does not have one built in like string or decimal. 如果要进行真正的数字比较,则需要将单元格值转换/解析为int并使用自己的int compare方法,因为int没有内置的字符串或十进制值。

    void OnDataGridViewSortCompare(object sender, DataGridViewSortCompareEventArgs e)
    {
        int retVal = CompareInt((int)e.CellValue1, (int)e.CellValue2);
        //They are ==, Compare by next column
        if (retVal == 0)
        {
            retVal = CompareInt((int)dgv[e.Column.Index + 1, e.RowIndex1].Value,
                (int)dgv[e.Column.Index + 1, e.RowIndex2].Value) * -1; //Multiply by -1 to flip the ASC sort to DESC
        }
        e.SortResult = retVal;
        e.Handled = true;
    }

    int CompareInt(int value1, int value2)
    {
        if (value1 == value2) return 0;
        else if (value1 < value2) return -1;
        else return 1;
    }

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

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