1 Ira 4
2 Vasya 1
3 Masha 5
4 Viktor 3
5 Alena 2
Соединение двух DataTable и вывод в DataGridView
Код:
-----------------------
ID | NAME | PLACE |
-----------------------
Guid | string | int |
-----------------------
Guid | string | int |
-----------------------
Guid | string | int |
-----------------------
ID | NAME | PLACE |
-----------------------
Guid | string | int |
-----------------------
Guid | string | int |
-----------------------
Guid | string | int |
-----------------------
Код:
-------------------------
DATE | ID | VALUE |
-------------------------
DateTime | Guid | int |
-------------------------
DateTime | Guid | int |
-------------------------
DateTime | Guid | int |
-------------------------
DATE | ID | VALUE |
-------------------------
DateTime | Guid | int |
-------------------------
DateTime | Guid | int |
-------------------------
DateTime | Guid | int |
-------------------------
Код:
----------------------------------
| NAME | NAME | NAME |
----------------------------------
DateTime | Value | Value | Value |
----------------------------------
DateTime | Value | Value | Value |
----------------------------------
DateTime | Value | Value | Value |
----------------------------------
| NAME | NAME | NAME |
----------------------------------
DateTime | Value | Value | Value |
----------------------------------
DateTime | Value | Value | Value |
----------------------------------
DateTime | Value | Value | Value |
----------------------------------
Как лучше и более правильно это реализовать программно?
Проект на 2 фреймворке, потому Linq тоже не получится использовать.
Спасибо.
Код:
private void Form1_Load(object sender, EventArgs e)
{
Data1 = File.ReadAllLines("Data1.txt", Encoding.Default);
if (Data1 != null)
{
SetColumn(data1, Data1, "ID", "Name", "Place");
}
Data2 = File.ReadAllLines("Data2.txt", Encoding.Default);
if (Data2 != null)
{
SetColumn(data2, Data2, "ID", "Date", "Value");
}
dgView();
}
private DataTable data1 = new DataTable();
private DataTable data2 = new DataTable();
private void dgView()
{
DataGridView dataGridView1 = new DataGridView();
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AllowUserToOrderColumns = true;
dataGridView1.AllowUserToResizeColumns = true;
dataGridView1.AllowUserToResizeRows = true;
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.Controls.Add(dataGridView1);
DataGridViewColumn newColumn = null;
DataGridViewRow newRow = new DataGridViewRow();
dataGridView1.RowTemplate = newRow;
for (int idx = 0; idx < data1.Columns.Count; idx++)
{
if (data1.Columns[idx].ColumnName != "ID") //&& data1.Columns[idx].ColumnName != "Place")
{
newColumn = new DataGridViewColumn();
newColumn.Visible = true;
newColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
newColumn.ReadOnly = true;
newColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
newColumn.HeaderText = data1.Columns[idx].ColumnName;
dataGridView1.Columns.Add(newColumn);
}
}
for (int idx = 0; idx < data2.Columns.Count; idx++)
{
if (data2.Columns[idx].ColumnName != "ID")
{
newColumn.Visible = true;
newColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
newColumn.ReadOnly = true;
newColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
newColumn = new DataGridViewColumn();
newColumn.HeaderText = data2.Columns[idx].ColumnName;
dataGridView1.Columns.Add(newColumn);
}
}
SetDataTodgView(dataGridView1);
DataGridViewColumn needColumn = null;
for (int idx = 0; idx < dataGridView1.Columns.Count; idx++)
{
if (dataGridView1.Columns[idx].HeaderText == "Place")
{
needColumn = dataGridView1.Columns[idx];
break;
}
}
dataGridView1.Sort(needColumn, ListSortDirection.Ascending);
dataGridView1.Columns.Remove(needColumn);
}
private void SetDataTodgView(DataGridView dgv)
{
int ColInd = -1;
DataGridViewTextBoxCell Name = null;
DataGridViewTextBoxCell Place = null;
DataGridViewTextBoxCell Date = null;
DataGridViewTextBoxCell mValue = null;
for (int gdx = 0; gdx < data2.Columns.Count; gdx++)
{
if (data2.Columns[gdx].ColumnName == "ID")
{
ColInd = gdx;
break;
}
}
for (int idx = 0; idx < data1.Rows.Count; idx++)
{
int IdParent = -1;
for (int jdx = 0; jdx < data1.Columns.Count; jdx++)
{
switch (data1.Columns[jdx].ColumnName)
{
case "Name":
Name = new DataGridViewTextBoxCell();
Name.Value = data1.Rows[idx].Field<string>(jdx);
break;
case "Place":
Place = new DataGridViewTextBoxCell();
Place.Value = data1.Rows[idx].Field<string>(jdx);
break;
default:
IdParent = Convert.ToInt32(data1.Rows[idx].Field<string>(jdx));
break;
}
}
for (int kdx = 0; kdx < data2.Rows.Count; kdx++)
{
if (IdParent == Convert.ToInt32(data2.Rows[kdx].Field<string>(ColInd)))
{
for (int ldx = 0; ldx < data2.Columns.Count; ldx++)
{
switch (data2.Columns[ldx].ColumnName)
{
case "Date":
Date = new DataGridViewTextBoxCell();
Date.Value = data2.Rows[kdx].Field<string>(ldx);
break;
case "Value":
mValue = new DataGridViewTextBoxCell();
mValue.Value = data2.Rows[kdx].Field<string>(ldx);
break;
}
}
break;
}
}
DataGridViewRow newRow = new DataGridViewRow();
newRow.Cells.AddRange(Name, Place, Date, mValue);
dgv.Rows.Add(newRow);
}
}
private void SetColumn(DataTable myTable, string[] Arr, string Col1, string Col2, string Col3)
{
for (int idx = 0; idx < 3; idx++)
{
DataColumn newColumn = new DataColumn();
switch (idx)
{
case 0:
newColumn.ColumnName = Col1;
newColumn.DataType = Type.GetType("System.String");
newColumn.ReadOnly = false;
break;
case 1:
newColumn.ColumnName = Col2;
newColumn.DataType = Type.GetType("System.String");
newColumn.ReadOnly = false;
break;
case 2:
newColumn.ColumnName = Col3;
newColumn.DataType = Type.GetType("System.String");
newColumn.ReadOnly = false;
break;
}
myTable.Columns.Add(newColumn);
}
for (int idx = 0; idx < Arr.Length; idx++)
{
SetInfo(myTable, Arr[idx], Col1, Col2, Col3);
}
}
private void SetInfo(DataTable myTable, string info, string Col1, string Col2, string Col3)
{
string Line = string.Empty;
int flag = 0;
DataRow newRow = myTable.NewRow();
for (int idx = 0; idx < info.Length; idx++)
{
if (info[idx] != ' ')
{
if (idx != info.Length - 1)
{
Line += Convert.ToString(info[idx]);
}
else
{
flag++;
Line += Convert.ToString(info[idx]);
if (flag == 3)
{
newRow[Col3] = Line;
}
}
}
else
{
flag++;
if (flag == 1)
{
newRow[Col1] = Line;
}
if (flag == 2)
{
newRow[Col2] = Line;
}
Line = string.Empty;
}
}
myTable.Rows.Add(newRow);
}
{
Data1 = File.ReadAllLines("Data1.txt", Encoding.Default);
if (Data1 != null)
{
SetColumn(data1, Data1, "ID", "Name", "Place");
}
Data2 = File.ReadAllLines("Data2.txt", Encoding.Default);
if (Data2 != null)
{
SetColumn(data2, Data2, "ID", "Date", "Value");
}
dgView();
}
private DataTable data1 = new DataTable();
private DataTable data2 = new DataTable();
private void dgView()
{
DataGridView dataGridView1 = new DataGridView();
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AllowUserToOrderColumns = true;
dataGridView1.AllowUserToResizeColumns = true;
dataGridView1.AllowUserToResizeRows = true;
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.Controls.Add(dataGridView1);
DataGridViewColumn newColumn = null;
DataGridViewRow newRow = new DataGridViewRow();
dataGridView1.RowTemplate = newRow;
for (int idx = 0; idx < data1.Columns.Count; idx++)
{
if (data1.Columns[idx].ColumnName != "ID") //&& data1.Columns[idx].ColumnName != "Place")
{
newColumn = new DataGridViewColumn();
newColumn.Visible = true;
newColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
newColumn.ReadOnly = true;
newColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
newColumn.HeaderText = data1.Columns[idx].ColumnName;
dataGridView1.Columns.Add(newColumn);
}
}
for (int idx = 0; idx < data2.Columns.Count; idx++)
{
if (data2.Columns[idx].ColumnName != "ID")
{
newColumn.Visible = true;
newColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
newColumn.ReadOnly = true;
newColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
newColumn = new DataGridViewColumn();
newColumn.HeaderText = data2.Columns[idx].ColumnName;
dataGridView1.Columns.Add(newColumn);
}
}
SetDataTodgView(dataGridView1);
DataGridViewColumn needColumn = null;
for (int idx = 0; idx < dataGridView1.Columns.Count; idx++)
{
if (dataGridView1.Columns[idx].HeaderText == "Place")
{
needColumn = dataGridView1.Columns[idx];
break;
}
}
dataGridView1.Sort(needColumn, ListSortDirection.Ascending);
dataGridView1.Columns.Remove(needColumn);
}
private void SetDataTodgView(DataGridView dgv)
{
int ColInd = -1;
DataGridViewTextBoxCell Name = null;
DataGridViewTextBoxCell Place = null;
DataGridViewTextBoxCell Date = null;
DataGridViewTextBoxCell mValue = null;
for (int gdx = 0; gdx < data2.Columns.Count; gdx++)
{
if (data2.Columns[gdx].ColumnName == "ID")
{
ColInd = gdx;
break;
}
}
for (int idx = 0; idx < data1.Rows.Count; idx++)
{
int IdParent = -1;
for (int jdx = 0; jdx < data1.Columns.Count; jdx++)
{
switch (data1.Columns[jdx].ColumnName)
{
case "Name":
Name = new DataGridViewTextBoxCell();
Name.Value = data1.Rows[idx].Field<string>(jdx);
break;
case "Place":
Place = new DataGridViewTextBoxCell();
Place.Value = data1.Rows[idx].Field<string>(jdx);
break;
default:
IdParent = Convert.ToInt32(data1.Rows[idx].Field<string>(jdx));
break;
}
}
for (int kdx = 0; kdx < data2.Rows.Count; kdx++)
{
if (IdParent == Convert.ToInt32(data2.Rows[kdx].Field<string>(ColInd)))
{
for (int ldx = 0; ldx < data2.Columns.Count; ldx++)
{
switch (data2.Columns[ldx].ColumnName)
{
case "Date":
Date = new DataGridViewTextBoxCell();
Date.Value = data2.Rows[kdx].Field<string>(ldx);
break;
case "Value":
mValue = new DataGridViewTextBoxCell();
mValue.Value = data2.Rows[kdx].Field<string>(ldx);
break;
}
}
break;
}
}
DataGridViewRow newRow = new DataGridViewRow();
newRow.Cells.AddRange(Name, Place, Date, mValue);
dgv.Rows.Add(newRow);
}
}
private void SetColumn(DataTable myTable, string[] Arr, string Col1, string Col2, string Col3)
{
for (int idx = 0; idx < 3; idx++)
{
DataColumn newColumn = new DataColumn();
switch (idx)
{
case 0:
newColumn.ColumnName = Col1;
newColumn.DataType = Type.GetType("System.String");
newColumn.ReadOnly = false;
break;
case 1:
newColumn.ColumnName = Col2;
newColumn.DataType = Type.GetType("System.String");
newColumn.ReadOnly = false;
break;
case 2:
newColumn.ColumnName = Col3;
newColumn.DataType = Type.GetType("System.String");
newColumn.ReadOnly = false;
break;
}
myTable.Columns.Add(newColumn);
}
for (int idx = 0; idx < Arr.Length; idx++)
{
SetInfo(myTable, Arr[idx], Col1, Col2, Col3);
}
}
private void SetInfo(DataTable myTable, string info, string Col1, string Col2, string Col3)
{
string Line = string.Empty;
int flag = 0;
DataRow newRow = myTable.NewRow();
for (int idx = 0; idx < info.Length; idx++)
{
if (info[idx] != ' ')
{
if (idx != info.Length - 1)
{
Line += Convert.ToString(info[idx]);
}
else
{
flag++;
Line += Convert.ToString(info[idx]);
if (flag == 3)
{
newRow[Col3] = Line;
}
}
}
else
{
flag++;
if (flag == 1)
{
newRow[Col1] = Line;
}
if (flag == 2)
{
newRow[Col2] = Line;
}
Line = string.Empty;
}
}
myTable.Rows.Add(newRow);
}
Код:
Код:
5 10.01.2001 500
3 02.10.2003 333
4 13.05.2007 1500
1 05.06.2011 150
2 23.03.1999 4000
3 02.10.2003 333
4 13.05.2007 1500
1 05.06.2011 150
2 23.03.1999 4000
Код:
Vasya 23.03.1999 4000
Alena 10.01.2001 500
Viktor 13.05.2007 1500
Ira 05.06.2011 150
Masha 02.10.2003 333
Alena 10.01.2001 500
Viktor 13.05.2007 1500
Ira 05.06.2011 150
Masha 02.10.2003 333