GridView from 2 DataTables - c#

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?

Related

Why doesn't it show me the values in my DataGrid that I assign in the DataTable?

I initially wrote the headers manually in my DataGrid and now I want to fill the DataGrid from a DataTable. I wanted to do it like this:
void fillingDataGrid()
{
DataTable dt = new DataTable();
DataColumn id = new DataColumn("id", typeof(int));
DataColumn name = new DataColumn("name", typeof(string));
DataColumn ort = new DataColumn("ort", typeof(string));
DataColumn alter = new DataColumn("alter", typeof(string));
DataColumn land = new DataColumn("land", typeof(string));
dt.Columns.Add(id);
dt.Columns.Add(name);
dt.Columns.Add(ort);
dt.Columns.Add(alter);
dt.Columns.Add(land);
DataRow firstrow = dt.NewRow();
firstrow[0] = 1;
firstrow[1] = "Peter";
firstrow[2] = "Berlin";
firstrow[3] = "18";
firstrow[4] = "Germany";
DataRow secondrow = dt.NewRow();
firstrow[0] = 2;
firstrow[1] = "Karl";
firstrow[2] = "Prag";
firstrow[3] = "12";
firstrow[4] = "Tschechien";
dt.Rows.Add(firstrow);
dt.Rows.Add(secondrow);
gridd.ItemsSource = dt.DefaultView;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.fillingDataGrid();
}
The problem, however, is that if I do it this way, it is not output correctly because it looks like this:
Why doesn't it show me all the data, what do I have to change on my DataTable?
Change this line :
gridd.ItemsSource = dt.DefaultView;
To :
gridd.DataContext = dt.DefaultView;

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.

DataRelation in a DataGridView

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";

Why an auto incremented column in datatable is coming empty?

I have made an identity column id with an auto-incremented equals true in datatable but it's empty.
Why ?
Datatable dt= new datatable();
DataColumn dc = new DataColumn("id", typeof(int));
dc.AutoIncrement = true;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep = 1;
dt.Columns.Add(dc);
dc = new DataColumn("NITNo", typeof(string));
dc.DefaultValue = txtNitNo.Text.ToString();
dt.Columns.Add(dc);
dc = new DataColumn("WorkNo", typeof(string));
dc.DefaultValue = txtWorkNo.Text.ToString();
dt.Columns.Add(dc);
//dt.Rows.rem
//Bind Data to GridView
gvBOQ.Caption = Path.GetFileName(FilePath);
gvBOQ.DataSource = dt;
gvBOQ.DataBind();
If you add data to the DataTable, you have to make sure that the value in the Identity Column is null
//create a datatable
DataTable table = new DataTable();
//auto increment column
DataColumn column = new DataColumn("id", typeof(int));
column.AutoIncrement = true;
column.AutoIncrementSeed = 1;
column.AutoIncrementStep = 1;
table.Columns.Add(column);
//add a normal column
column = new DataColumn("value", typeof(string));
table.Columns.Add(column);
//add some data to the table
table.Rows.Add(null, "Netherlands");
table.Rows.Add(null, "Japan");
table.Rows.Add(99, "Australia");
table.Rows.Add(null, "America");
The resulting table will look like this
id value
1 Netherlands
2 Japan
99 Australia
100 America
You can loop through the datatable and increment the value of the particular column
e.g
int i = 1;
foreach (DataRow dr in table.Rows)
{
dr["id"] = i;
i++;
}

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 };

Categories

Resources