0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C# DataTable

Posted at

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ringo
{
public partial class Form_1009 : Form
{
DataSet_1009 ds = null;
DataSet_1009 fds = null;
DataSet_1009 Finalds = null;

    public Form_1009()
    {
        Init();

        InitCalc();

        Calc();

        InitializeComponent();
    }


    private void Init()
    {
        ds = new DataSet_1009();
        fds = new DataSet_1009();
        Finalds = new DataSet_1009();

        for (int i = 1; i <= 3; i++)
        {
            DataSet_1009.T_DtRow tRow = fds.T_Dt.NewT_DtRow();
            tRow.T_id = "2022/02/0" + (i + 2).ToString();
            tRow.T_data = "test_" + (i + 2).ToString();
            fds.T_Dt.Rows.Add(tRow);

            DataSet_1009.T_DtRow tttRow = fds.T_Dt.NewT_DtRow();
            tttRow.T_id = "2022/02/0" + (i + 2).ToString();
            tttRow.T_data = "test_" + (i + 2).ToString();
            fds.T_Dt.Rows.Add(tttRow);
        }

        DataSet_1009.T_DtRow ttRow = fds.T_Dt.NewT_DtRow();
        ttRow.T_id = "2022/02/08";
        ttRow.T_data = "test_8";
        fds.T_Dt.Rows.Add(ttRow);

        for (int i = 1; i <= 9; i++)
        {
            DataSet_1009.F_DtRow dayRow = ds.F_Dt.NewF_DtRow();
            dayRow.D_id = "2022/02/0" + i.ToString();
            dayRow.D_data = "test_" + i.ToString();
            ds.F_Dt.Rows.Add(dayRow);

            DataSet_1009.F_DtRow stayRow = ds.F_Dt.NewF_DtRow();
            stayRow.S_id = "2022/02/0" + i.ToString();
            stayRow.S_data = "test_" + i.ToString();
            ds.F_Dt.Rows.Add(stayRow);

        }
    }


    private void InitCalc()
    {
        string preDay = string.Empty;
        string day_id = string.Empty;
        string day_data = string.Empty;
        string stay_id = string.Empty;
        string stay_data = string.Empty;
        for (int i = 0; i < ds.F_Dt.Rows.Count; i++)
        {
            day_id = ds.F_Dt.Rows[i]["D_id"].ToString();
            if (!string.IsNullOrEmpty(day_id))
            {
                preDay = day_id;
            }
            day_data = ds.F_Dt.Rows[i]["D_data"].ToString();

            // 「ds.T_Dt」に「D/S」をまとめる。
            if (!string.IsNullOrEmpty(day_id))
            {
                DataSet_1009.T_DtRow ttRow = ds.T_Dt.NewT_DtRow();
                ttRow.D_id = day_id;
                ttRow.D_data = day_data;
                ds.T_Dt.Rows.Add(ttRow);
            }

            // Sも同じレコードか?
            if (i + 1 <= ds.F_Dt.Count - 1)
            {
                stay_id = ds.F_Dt.Rows[i + 1]["S_id"].ToString();
                stay_data = ds.F_Dt.Rows[i + 1]["S_data"].ToString();
                if (!string.IsNullOrEmpty(stay_id))
                {
                    if (preDay.Equals(stay_id))
                    {
                        // Edit
                        int rowNo = ds.T_Dt.Count - 1;
                        ds.T_Dt.Rows[rowNo]["S_id"] = stay_id;
                        ds.T_Dt.Rows[rowNo]["S_data"] = stay_data;
                    }
                    else
                    {
                        // Add(insertAt)
                        int rowNo = ds.T_Dt.Count - 1;
                        DataSet_1009.T_DtRow tRow = ds.T_Dt.NewT_DtRow();
                        ds.T_Dt.Rows.InsertAt(tRow, rowNo);
                    }
                }
            }

            //var T_01 = fds.T_Dt.Select("T_id = '" + day_id + "'", "T_data");
            //var T_02 = fds.T_Dt.Select("T_id = '" + stay_id + "'", "T_data");
        }
    }


    private void Calc()
    {
        //ds.T_Dt
        string date = string.Empty;
        var dsDr = ds.T_Dt.Rows;
        for (int i = 0; i < dsDr.Count; i++)
        {
            date = dsDr[i]["D_id"].ToString();

            var fds_dataRow = fds.T_Dt.Select("T_id = '" + date + "'", "T_data");
            for(int j = 0; j < fds_dataRow.Length; j++)
            {
                DataSet_1009.T_DtRow tRow = Finalds.T_Dt.NewT_DtRow();
                tRow.T_id = fds_dataRow[j]["T_id"].ToString();
                tRow.T_data = fds_dataRow[j]["T_data"].ToString();
                Finalds.T_Dt.Rows.Add(tRow);
            }

            // Add
            DataSet_1009.T_DtRow ttRow = Finalds.T_Dt.NewT_DtRow();
            ttRow.D_id = dsDr[i]["D_id"].ToString();
            ttRow.D_data = dsDr[i]["D_data"].ToString();
            ttRow.S_id = dsDr[i]["S_id"].ToString();
            ttRow.S_data = dsDr[i]["S_data"].ToString();
            Finalds.T_Dt.Rows.Add(ttRow);
        }

        //fds.T_Dt
        //    ds.T_Dt
        //Finalds


    }
}

}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?