简体   繁体   English

更新:结果未传输到原始数据集。 使用C#对数据集中的行进行排序

[英]Update :Result is not transfered to the orginal dataset. Sorting Rows in Dataset using C#

Update: even though I have got the required result but when the the second function access the data table the value is still the same It a sequential program with two functions in different classes. 更新:即使我得到了所需的结果,但是当第二个函数访问数据表时,值仍然相同。这是一个顺序程序,其中两个函数位于不同的类中。 First sort and second replace function. 第一排序和第二替换功能。 So it should sort the value and other function should be able to retrieve the sorted table but when it retrieve the datatable it gives the unsorted table. 因此,它应该对值进行排序,其他函数应该能够检索已排序的表,但是当它检索数据表时,它会给出未排序的表。

I have used acceptchanges() but it also give the same result. 我已经使用了acceptchanges(),但它也给出了相同的结果。

The program is trying to sort the table according to the required field and the result is stored in Sorted table variable. 程序正在尝试根据必填字段对表进行排序,结果存储在Sorted table变量中。 I am trying to copy this to the original ie sourceTables but it is not working and is adding another row instead of updating [As shown in below dig]. 我试图将其复制到原始的,即sourceTables,但它不起作用,并且添加了另一行而不是更新[如以下dig所示]。 I have tried to copy whole table but it does not work and by adding rows it is not giving the required result. 我试图复制整个表,但是它不起作用,并且通过添加行它没有给出所需的结果。 I have used different methods but I am not getting the required result. 我使用了不同的方法,但没有得到所需的结果。

List<DataTable> sourceTables =   context.GetDataByTable(sourceTable.StringValue);
List<DataTable> targetTables = context.GetDataByTable(targetTable.StringValue, sourceTables.Count);

        string orderDesc= orderField.StringValue + " DESC";


        for (int i = 0; i < sourceTables.Count; i++)
        {
            DataView dv = sourceTables[i].DefaultView;

            if (orderDirection.StringValue == OrderDirectionAsc)
            {
                // for Sorting in Ascending Order 
                dv.Sort = orderField.StringValue;
            }

            else
            {
                // for Sorting in Descending Order
                dv.Sort = orderDesc;
            }


            DataTable sortedTable = dv.ToTable();

            DataTable dttableNew = sortedTable.Clone();
            //sourceTables[i] = sortedTable.Copy();
            //targetTables[i] = dv.ToTable();
            //targetTables[i] = sortedTable.Copy();
            // foreach (DataRow dr in sortedTable.Rows)
            ////  targetTables[i].Rows.Add(dr.ItemArray);
            //}
            for (int j = 0; j < sourceTables[i].Rows.Count; j++)
            {
                if (sourceTable.GetValue().ToString() == targetTable.GetValue().ToString())
                {
                    foreach (DataRow dr in sortedTable.Rows)
                    {
                        targetTables[i].Rows.Add(dr.ItemArray);

                    }                  
                else
                {
                    foreach (DataRow dr in sortedTable.Rows)
                    {
                        targetTables[i].Rows.Add(dr.ItemArray);

                    }

                    // targetTables[i] = sortedTable.Copy();  does not work

                    //foreach (DataRow drtableOld in sortedTable.Rows)
                    //{
                    // targetTables[i].ImportRow(drtableOld);
                    //}

Instead of replacing the first values it is adding more rows 而不是替换第一个值,而是添加了更多行

在此处输入图片说明

any help would be appreciated 任何帮助,将不胜感激

If any one have problem with duplicate data or the changes are only local and is not effecting the original data table. 如果有人对重复数据有疑问,或者更改仅是本地的,并且不会影响原始数据表。 Remember to always use .ImportRow(dr) function to add rows to the table and if you use Tables[i].Rows.Add(dr.ItemArray); 请记住,始终使用.ImportRow(dr)函数向表中添加行,如果使用Tables [i] .Rows.Add(dr.ItemArray); the changes will affect only the local table and not the original one. 所做的更改将只影响本地表,而不影响原始表。 Use .clear to remove the old rows from the orginal table. 使用.clear从原始表中删除旧行。 The action done directly on the original function will only effect the rows. 直接在原始函数上执行的操作只会影响行。 If it is done on the clone copy changes will nor affect the original table. 如果对克隆完成,则更改不会影响原始表。
Here is the complete code 这是完整的代码

           DataTable sortTable = dv.ToTable();

            if (sTable.GetValue().ToString() == tTable.GetValue().ToString())
            {

                sTables[i].Clear();

                foreach (DataRow dr in sortTable.Rows)

                {
                    sTables[i].ImportRow(dr);
                }

                sTables[i].AcceptChanges();

            }   

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

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