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 };
Related
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.
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";
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++;
}
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?
I have denormalized data in a DataTable.
The data contains employee names, and the pay they got over a series of pay cycles. i.e.:
My DataTable contains:
Employee 1 Jan-1-2012 $100
Employee 2 Jan-1-2012 $300
Employee 1 Feb-1-2012 $400
Employee 2 Feb-1-2012 $200
Employee 1 Mar-1-2012 $150
Employee 2 Mar-1-2012 $325
How can load this data into a DataSet where the parent DataTable contains the employees name, and the child DataTable contains details of the paycheck?
DataSet is nothing but a collection of DataTables. So to "load" the dataTable into dataSet simple Add it:
DataTable employees = new DataTable();
DataTable payCheckes = new DataTable();
DataSet ds = new DataSet();
ds.Tables.Add(employees);
ds.Tables.Add(payCheckes);
Do you want to "combine" datatables somehow?
Get paycheckes of each employee?
the code without manual insert:
DataSet ds = new DataSet();
DataTable dtemploye = new DataTable();
DataTable dtpayment = new DataTable();
ds.Tables.AddRange(new DataTable[] { dtemploye, dtpayment });
DataColumn dcIdemploye = dtemploye.Columns["ID_EMPLOYEE"];
DataColumn dcIdemployeprice = dtpayment.Columns["ID_EMPLOYEE"];
DataRelation drrelation = new DataRelation("relemploy_payment", dcIdemploye, dcIdemployeprice);
ds.Relations.Add(drrelation);
DataSet ds = new DataSet();
DataTable dtemploye = new DataTable();
DataColumn dcnameemploye = new DataColumn();
DataColumn dcIdemploye = new DataColumn();
dtemploye.Columns.AddRange(new DataColumn[]{dcnameemploye,dcIdemploye});
DataTable dtpayment = new DataTable();
DataColumn dtprice = new DataColumn();
DataColumn dtDate = new DataColumn();
DataColumn dcIdemployeprice = new DataColumn();
dtpayment.Columns.AddRange(new DataColumn[]{dcIdemployeprice,dtprice,dtDate});
DataRow drrowemploy = dtemploye.NewRow();
drrowemploy[0] = "1";
drrowemploy[1] = "Employee 1";
dtemploye.Rows.Add(drrowemploy);
DataRow drrowpayment = dtpayment.NewRow();
drrowpayment[0] = "1";
drrowpayment[0] = "01/01/2012";
drrowpayment[1] = " 300";
ds.Tables.AddRange(new DataTable[]{dtemploye, dtpayment});
DataRelation drrelation = new DataRelation("relemploy_payment", dcIdemploye, dcIdemployeprice);
ds.Relations.Add(drrelation);