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 };
Related
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";
public void populatetransaction()
{
TRANASACTIONHISTRORYDataContext th = new TRANASACTIONHISTRORYDataContext();
DataTable dt = new DataTable();
var query = from dr
in th.TRANSACTIONSP((System.Nullable<long>)Convert.ToInt32(hidreference.Value))
select dr;
dt = (DataTable)query;
}
here is my code in that th.TRANSACTIONSP((System.Nullable<long>)Convert.ToInt32(hidreference.Value))
is the procedure calling how can i do that
public void populatetransaction()
{
TRANASACTIONHISTRORYDataContext th = new TRANASACTIONHISTRORYDataContext();
DataTable dt = new DataTable();
var query= th.TRANSACTIONSP((System.Nullable<long>)Convert.ToInt32(hidreference.Value));
DataColumn dc1 = dt.Columns.Add("BTC", typeof(string));
dc1.AllowDBNull = true;
DataColumn dc2 = dt.Columns.Add("TRANSACTIONDATE", typeof(DateTime));
dc2.AllowDBNull = true;
DataColumn dc3 = dt.Columns.Add("TRANSACTIONSTATUS", typeof(string));
DataRow dw ;
dc3.AllowDBNull = true;
foreach (var c in query)
{
dw=dt.NewRow();
dw["BTC"] = c.BTC;
dw["TRANSACTIONDATE"] = c.TRANSACTIONDATE;
dw["TRANSACTIONSTATUS"] = c.TRANSACTIONSTATUS;
dt.Rows.Add(dw);
}
}
This was what I want
This question already has answers here:
How do you databind to a System.Windows.Forms.Treeview control?
(3 answers)
Closed 8 years ago.
I have created a DataTable with three columns and i am adding rows to that DataTable using Data Rows. Then,I am adding the DataTable to a DataSet. Now, I just want to bind the DataSet to a TreeView.
DataTable dt = new DataTable();
dt.TableName = "Tree";
dt.Columns.Add("Name");
dt.Columns.Add("Project");
dt.Columns.Add("Experience");
List<Profile> list = new List<Profile>
{
new Profile{ Name="Boopathi", Project="NPD", Experience=1},
new Profile{ Name="Stephan", Project="Luxstone", Experience=1},
new Profile{ Name="Sri", Project="DellAsap", Experience=1}
};
var query = from s in list.AsEnumerable()
where s.Experience == 1
select s;
DataSet ds=new DataSet();
DataRow dr = dt.NewRow();
foreach (var t in query)
{
dr["Name"] = t.Name;
dr["Project"] = t.Project;
dr["Experience"] = t.Experience;
}
ds.Tables.Add(dt);
return ds;
Try this
if (ds.Tables.Count > 0)
{
TreeNode root= new TreeNode("Root Node");
foreach (DataRow row in ds.Tables[0].Rows)
{
TreeNode NewNode = new TreeNode(row["Name"].ToString());
root.ChildNodes.Add(NewNode);
}
}
DataTable dt=new DataTable();
DataTable dt1 = new DataTable();
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables.Add(dt1);
ds.Relations.Add("children", dt.Columns["GSICCodeID"], dt1.Columns["GSICCodeID"]);
if (ds.Tables[0].Rows.Count > 0)
{
tvSicCode.Nodes.Clear();
Int32 i = 0;
foreach (DataRow masterRow in ds.Tables[0].Rows)
{
TreeNode masterNode = new TreeNode((string)masterRow["Description"], Convert.ToString(masterRow["GSicCodeID"]));
tvSicCode.Nodes.Add(masterNode);
foreach (DataRow childRow in masterRow.GetChildRows("Children"))
{
TreeNode childNode = new TreeNode((string)childRow["SICCodeDesc"], Convert.ToString(childRow["SICCodeID"]));
if (Convert.ToString(ds.Tables[1].Rows[i]["CarrierSICCode"]) != "")
childNode.Checked = true;
masterNode.ChildNodes.Add(childNode);
i++;
}
}
tvSicCode.CollapseAll();
}
here is a simple article also
using telerik :
using System.Data.SqlClient;using Telerik.Web.UI;
namespace RadTreeView_DataBindDataTable
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindToDataTable(RadTreeView1);
}
}
private void BindToDataTable(RadTreeView treeView)
{
SqlConnection connection = new SqlConnection(Properties.Settings.Default.NwindConnectionString);
SqlDataAdapter adapter = new SqlDataAdapter("select CategoryID, CategoryName, Description from Categories", connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
treeView.DataTextField = "CategoryName";
treeView.DataFieldID = "CategoryID";
treeView.DataValueField = "Description";
treeView.DataSource = dataTable;
treeView.DataBind();
}
}
}
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);
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 };