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++;
}
Related
Here and Two Variable Store Two tables Data Now I want to Add these tables in to DataTable through Foreach Loop ?
var meterData = MeterTable(startDateTime, endDateTime);
var levelData = LevelTable(startDateTime, endDateTime);
var dataTable = new DataTable();
dataTable.Columns.Add("Meter", typeof(string));
dataTable.Columns.Add("Volume", typeof(int));
dataTable.Columns.Add("OpeningBalance", typeof(int));
dataTable.Columns.Add("IN", typeof(int));
dataTable.Columns.Add("Transfer", typeof(int));
dataTable.Columns.Add("OUT", typeof(int));
dataTable.Columns.Add("ClosingBalance", typeof(int));
// how Onward i have not any idea ?
If you want all columns from each table you can do this:
var newTable = new DataTable();
//Copy the meter table into your new table
newTable = meterData.Copy();
//Use .Merge to merge in the level table
newTable.Merge(levelData);
If you want to add new rows, you create a datarow and add it to the datatable:
// Create new DataRow objects and add to DataTable.
for(int i = 0; i < someNumberOfRows; i++)
{
row = newTable.NewRow();
row["column1"] = i;
row["column2"] = "item " + i.ToString();
newTable.Rows.Add(row);
}
If you need to use foreach, I'm assuming you want to iterate the rows in your 2 tables:
foreach(DataRow r in meterData.Rows)
{
row = newTable.NewRow();
row["someColumn"] = r["someColumn"];
row["someOtherColumn"] = r["someOtherColumn"];
newTable.Rows.Add(row);
}
you can add new row through foreach loop like this
var dataTable = new DataTable();
dataTable.Columns.Add("Meter", typeof(string));
dataTable.Columns.Add("Volume", typeof(int));
foreach (var items in Source) //source is the where you want to loop
{
DataRow row = dataTable.NewRow();
row["Meter"] = "meter";
//you can access value of source items through item.NameofTargetField like items.meter
row["Volume"] = 11;
dataTable.Rows.Add(row);
}
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 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 a datatable in windows form and I want to add a new row to the table. I always receive the error
Table doesn't have a primary key.
The question is that I don't want a primary key at all. It may have duplicated "ID" in the table.
My code:
using (DataTable dt = new DataTable())
{
dt.Columns.Add("EntryDE", typeof(string));
dt.Columns.Add("Descr", typeof(string));
dt.Columns.Add("Id", typeof(int));
foreach (DataGridViewRow row in dgv.Rows)
{
DataGridViewCheckBoxCell check = row.Cells[0] as DataGridViewCheckBoxCell;
if (check.Value != null)
{
if ((bool)check.Value)
{
//this row has a checkBox set to true (tick is added)
//add this row to dataTable ...
DataRow myRow = (row.DataBoundItem as DataRowView).Row;
DataRow dr = dt.NewRow();
dr["EntryDE"] = myRow["ID"].ToString();
dr["Descr"] = myRow["EntryName"];
dr["Id"] = Id;
dt.Rows.Add(dr);
}
}
}
Thanks for advice.
The error isn't coming from the DataTable because this works:
using (DataTable dt = new DataTable())
{
dt.Columns.Add("EntryDE", typeof(string));
dt.Columns.Add("Descr", typeof(string));
dt.Columns.Add("Id", typeof(int));
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["EntryDE"] = "abc";
dr["Descr"] = "xyz";
dr["Id"] = 1;
dt.Rows.Add(dr);
}
}
You've got some other problem.
You probably do want a primary key, as without it you'll be unable to update or delete rows later on.
If the ID field is going to contain duplicates, then you should add another column, call it primary_key or whatever you like, and use that column as PK.
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 };