DataRelation in a DataGridView - c#

I have got a DataSet filled with two Tables:
dbSet = new DataSet();
//DataTable and DataRelation
DataTable dtStudent = new DataTable("Student");
//fill datatable 1
dtStudent.Columns.Add("Id", typeof(int));
dtStudent.Columns.Add("Name", typeof(string));
dtStudent.Columns.Add("TownId", typeof(int));
dtStudent.Rows.Add(new object[] { 1, "Arthur", 1 });
dtStudent.Rows.Add(new object[] { 2, "Stefan", 2 });
DataTable dtTown = new DataTable("Town");
dtTown.Columns.Add("Id", typeof(int));
dtTown.Columns.Add("Name", typeof(string));
dtTown.Rows.Add(new object[] { 1, "KW",});
dtTown.Rows.Add(new object[] { 2, "Perg", });
dbSet.Tables.Add(dtStudent);
dbSet.Tables.Add(dtTown);
And then I have got a DataRelation for these two tables.
So I want to print the Student with his ID, Name and the Name of his town.
That's why I create a DataRelation.
//DataRelation
DataColumn parentCol, childCol;
childCol = dbSet.Tables["Town"].Columns["Id"];
parentCol = dbSet.Tables["Student"].Columns["TownId"];
DataRelation dr;
dr = new DataRelation("DataRelation", parentCol, childCol);
dbSet.Relations.Add(dr);
However, when I add the DataSet to my DataGridView I always get the TownId instead of the TownName.
dgv.DataSource = dbSet;
dgv.DataMember = "Student";

You missed only one thing: you need to add the appropriate column to view the data from the master table
dbSet.Tables["Student"].Columns.Add("Town", dbSet.Tables["Town"].Columns["Name"].DataType, "Parent.Name");
Thus, the entire code will look like this:
dbSet = new DataSet();
//DataTable and DataRelation
DataTable dtStudent = new DataTable("Student");
//fill datatable 1
dtStudent.Columns.Add("Id", typeof(int));
dtStudent.Columns.Add("Name", typeof(string));
dtStudent.Columns.Add("TownId", typeof(int));
dtStudent.Rows.Add(new object[] { 1, "Arthur", 1 });
dtStudent.Rows.Add(new object[] { 2, "Stefan", 2 });
DataTable dtTown = new DataTable("Town");
dtTown.Columns.Add("Id", typeof(int));
dtTown.Columns.Add("Name", typeof(string));
dtTown.Rows.Add(new object[] { 1, "KW",});
dtTown.Rows.Add(new object[] { 2, "Perg", });
dbSet.Tables.Add(dtStudent);
dbSet.Tables.Add(dtTown);
//DataRelation
DataColumn parentCol, childCol;
childCol = dbSet.Tables["Town"].Columns["Id"];
parentCol = dbSet.Tables["Student"].Columns["TownId"];
DataRelation dr;
dr = new DataRelation("DataRelation", parentCol, childCol);
dbSet.Relations.Add(dr);
dbSet.Tables["Student"].Columns.Add("Town", dbSet.Tables["Town"].Columns["Name"].DataType, "Parent.Name");
//Datagridview
dgv.DataSource = dbSet;
dgv.DataMember = "Student";

Related

Merge two datatables?

I want to merge two data table in which column "ID" as primary key in both table.
Note: Both table has two column as "ID, Name" & "ID, name" where Name & name is case sensitive.
Table: 1
Table: 2
Expected Merged Table Result:
Code:
public MainWindow()
{
InitializeComponent();
// Table 1
DataTable table1 = new DataTable();
table1.Columns.Add("ID", typeof(int));
table1.Columns.Add("Name", typeof(String));
table1.PrimaryKey = new DataColumn[] { table1.Columns["ID"] };
table1.Rows.Add(1, "A");
table1.Rows.Add(4, "D");
table1.Rows.Add(5, "E");
// Table 2
DataTable table2 = new DataTable();
table2.Columns.Add("ID", typeof(int));
table2.Columns.Add("name", typeof(String));
table2.PrimaryKey = new DataColumn[] { table2.Columns["ID"] };
table2.Rows.Add(1, "A");
table2.Rows.Add(2, "B");
table2.Rows.Add(3, "C");
table2.Rows.Add(5, "E");
table1.Merge(table2);
}
Please help me to achieve this expected result.
You could temporarily change the name of the second column and still use Merge:
const string OriginalName = "name";
const string TemporaryName = "temp";
table2.Columns[OriginalName].ColumnName = TemporaryName;
table1.Merge(table2);
table1.Columns[TemporaryName].ColumnName = OriginalName;
table2.Columns[TemporaryName].ColumnName = OriginalName;
Use this:
DataTable dtResult = new DataTable();
dtResult.Columns.Add("ID", typeof(int));
dtResult.Columns.Add("Name", typeof(string));
dtResult.Columns.Add("name", typeof(string));
var result = from dataRows1 in table1.AsEnumerable()
join dataRows2 in table2.AsEnumerable()
on dataRows1.Field<int>("ID") equals dataRows2.Field<int>("ID")
into rows
from row in rows.DefaultIfEmpty()
select dtResult.LoadDataRow(new object[]
{
dataRows1.Field<int>("ID"),
dataRows1.Field<string>("Name"),
dataRows2.Field<string>("name")
}, false);

How to link a ComboBox to a Column of DataGrid Control in WinForms? - C#

Here is a part of my code:
// building a dataset
DataSet ds = new DataSet("Stud_Prog");
DataTable student = new DataTable("Student");
DataColumn stud_ID = student.Columns.Add("Student_ID", typeof(int));
DataColumn stud_name = student.Columns.Add("Name", typeof(string));
DataColumn stud_surname = student.Columns.Add("Surname", typeof(string));
DataColumn stud_grade = student.Columns.Add("Grade", typeof(string));
stud_ID.AutoIncrement = true;
stud_ID.AutoIncrementSeed = 1;
stud_ID.Unique = true;
student.PrimaryKey = new DataColumn[] { stud_ID };
DataTable grade = new DataTable("Grade");
DataColumn grad_ID = grade.Columns.Add("Grade_ID", typeof(int));
DataColumn grad_gradeType = grade.Columns.Add("Grade Type", typeof(string));
grade.PrimaryKey = new DataColumn[] { grad_ID };
grade.Rows.Add(1, "A");
// ...
student.Rows.Add(null, "AAA", "AAA");
// ...
ds.Tables.Add(student);
ds.Tables.Add(grade);
ds.Relations.Add("StudentGradeRelation", ds.Tables[1].Columns[0], ds.Tables[0].Columns[3]);
DataGrid studentGrid = new DataGrid();
DataGrid gradeGrid = new DataGrid();
// adding to the form
// ...
studentGrid.DataSource = ds.Tables[0];
gradeGrid.DataSource = ds.Tables[1];
// ...
I need Grade column cells of Student table to look like ComboBoxes that would offer me to choose one of the grade types contained inside the Grade table.

GridView from 2 DataTables

I have a dataset contains 2 datatables with a relation between them,
in the first table i have an ID (primary key) and MainAreaName and in the second table I have the ID from the first table with 2 other columns
the question is: how to view the MainAreaName from the first table with the other 2 columns from the second table in a gridview
DataSet MyDB = new DataSet("MyDB");
DataTable MainArea = new DataTable("MainArea");
DataTable NameNumber = new DataTable("NameNumber");
DataColumn Col01 = new DataColumn("ID", typeof(int));
Col01.AutoIncrement = true;
Col01.AutoIncrementSeed = 1;
Col01.AutoIncrementStep = 1;
DataColumn Col02 = new DataColumn("MainAreaName", typeof(string));
MainArea.Columns.Add(Col01);
MainArea.Columns.Add(Col02);
MainArea.PrimaryKey =
new DataColumn[] { MainArea.Columns["ID"] };
DataRow MyRow = MainArea.NewRow();
MyRow["MainAreaName"] = "Area1";
MainArea.Rows.Add(MyRow);
MyRow = MainArea.NewRow();
MyRow["MainAreaName"] = "Area2";
MainArea.Rows.Add(MyRow);
DataColumn Col11 = new DataColumn("MainAreaID", typeof(int));
DataColumn Col12 = new DataColumn("Name", typeof(string));
DataColumn Col13 = new DataColumn("Number", typeof(int));
DataColumn Col14 = new DataColumn("Comment", typeof(string));
NameNumber.Columns.Add(Col11);
NameNumber.Columns.Add(Col12);
NameNumber.Columns.Add(Col13);
NameNumber.Columns.Add(Col14);
NameNumber.PrimaryKey =
new DataColumn[] {
NameNumber.Columns["MainAreaID"], NameNumber.Columns["Number"] };
NameNumber.Rows.Add(1, "Test1", 67);
NameNumber.Rows.Add(1, "Test2", 87);
NameNumber.Rows.Add(2, "Test3", 77);
NameNumber.Rows.Add(2, "Test4", 88);
MyDB.Tables.Add(MainArea);
MyDB.Tables.Add(NameNumber);
MyDB.Relations.Add(
MyDB.Tables["MainArea"].Columns["ID"],
MyDB.Tables["NameNumber"].Columns["MainAreaID"]);
I tried to use the following code
DataRelation DR = new DataRelation("MainAreaRelation", MyDB.Tables["MainArea"].Columns["ID"], MyDB.Tables["NameNumber"].Columns["MainAreaID"]);
dataGridView1.DataSource = MyDB.Relations["MainAreaRelation"].ParentTable;
//or
dataGridView1.DataSource = MyDB.Relations["MainAreaRelation"].ParentTable;
but didn't work,
anyone can help please?

how to insert data to dataset

i have this making table:
DataTable WorkTbl()
{
DataTable Work= new DataTable("Work"); //Table Name
DataColumn MAC = new DataColumn("MAC", typeof(string));
DataColumn ID_OLD = new DataColumn("ID_OLD", typeof(string));
Work.Columns.Add(MAC);
Work.Columns.Add(ID_OLD);
return Work;
}
how to insert data to this table and how to convert this table to Dataset ?
thanks in advance
From MSDN:
DataRow workRow = workTable.NewRow();
You then can manipulate the newly added row using an index or the column name, as shown in the following example.
workRow["CustLName"] = "Smith";
workRow[1] = "Smith";
DataSet customerOrders = new DataSet("CustomerOrders");
DataTable ordersTable = customerOrders.Tables.Add("Orders");
DataColumn pkOrderID =
ordersTable.Columns.Add("OrderID", typeof(Int32));
ordersTable.Columns.Add("OrderQuantity", typeof(Int32));
ordersTable.Columns.Add("CompanyName", typeof(string));
ordersTable.PrimaryKey = new DataColumn[] { pkOrderID };

Datatable add to dataset

i need to set a table to a dataset
DataSet ds = EventDal.GetEvents();
DataSet dsReturn = new DataSet();
DataTable dtReturn = dsReturn.Tables.Add();
dtReturn.Columns.Add("id");
dtReturn.Columns.Add("description");
dtReturn.Columns.Add("status");
foreach (DataRow row in ds.Tables[0].Rows)
{
if(Convert.ToInt32(row[1]) == status )
{
DataRow newrowdata = dtReturn.NewRow();
dsReturn.Tables["dtReturn"].ImportRow((row));///i'm getting object ref not set to an instance of object..
//DataRow drReturn = dtReturn.NewRow();
//dsReturn.Tables["dtReturn"].Rows.Add(row);
}
}
return dsReturn;
DataSet customerOrders = new DataSet("CustomerOrders");
DataTable ordersTable = customerOrders.Tables.Add("Orders");
DataColumn pkOrderID =
ordersTable.Columns.Add("OrderID", typeof(Int32));
ordersTable.Columns.Add("OrderQuantity", typeof(Int32));
ordersTable.Columns.Add("CompanyName", typeof(string));
ordersTable.PrimaryKey = new DataColumn[] { pkOrderID };

Categories

Resources