Combine two non-identical rows of a datatable - c#

I have two datatables dt1 and dt2 ,where as dt1 contains of a single column "ID" and dt2 contains of a single column "NAME". My requirement is to combine both the datatables and create a new datatable (ie)
whereas as mentioned above i need to get the datas of both the datatable (dt1 & dt2 ) and need to create a new datatable.

You can do something like this
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt1.Columns.Add("id", typeof(Int32));
dt2.Columns.Add("Name", typeof(String));
DataRow dr = dt1.NewRow();
dr["id"] = 1;
dt1.Rows.Add(dr);
dr = dt1.NewRow();
dr["id"] = 2;
dt1.Rows.Add(dr);
dr = dt2.NewRow();
dr["name"] = "XXX";
dt2.Rows.Add(dr);
dr = dt2.NewRow();
dr["name"] = "YYY";
dt2.Rows.Add(dr);
DataTable dt3 = new DataTable();
dt3.Columns.Add("id", typeof(Int32));
dt3.Columns.Add("Name", typeof(String));
for (int i = 0; i < dt1.Rows.Count; i++)
{
dr = dt3.NewRow();
dr["id"] = dt1.Rows[i]["id"];
dr["name"] = dt2.Rows[i]["name"];
dt3.Rows.Add(dr);
}

IF you want to combine to DataTables, you can do the following:
DataTable table1 = new DataTable("Items");
DataColumn column1 = new DataColumn("id", typeof(System.Int32));
table1.Columns.Add(column1);
DataTable table2 = new DataTable("details");
DataColumn column = new DataColumn("Name", typeof(System.String));
table1.Columns.Add(column);
table1.Merge(table2); //table1 will have 2 columns after executing this line

Related

how to add some selected items and their indexes from listbox to datatable

I`m trying to use something like this:
I have 20 list items and I have to choose some of them, then add them to datatable in first column elements, in second - their indexes.
DataTable dt = new DataTable();
dt.Columns.Add("Фактор-Критерій", typeof(string));
dt.Columns.Add("Ранг", typeof(string));
ChoosenCriteria.DataSource = dt;
List<string> selectedItems = new List<string>();
foreach (string o in listBox1.SelectedItems)
selectedItems.Add(o);
List<int> selectedItemIndexes = new List<int>();
foreach (int o in listBox1.SelectedIndices)
selectedItemIndexes.Add(listBox1.Items.IndexOf(o));
DataRow dr = dt.NewRow();
dr[0] = selectedItems.ToString();
dr[1] = selectedItemIndexes.ToString();
dt.Rows.Add(dr););
I can not do this, all i have at the end is on screen:enter image description here
Move the DataSource binding statement after you have done with the modification like
for(int i=0; i<selectedItems.Count;i++
{
DataRow dr = dt.NewRow();
dr[0] = selectedItems[i].ToString();
dr[1] = selectedItemIndexes[i].ToString();
dt.Rows.Add(dr);
}
ChoosenCriteria.DataSource = dt;

Gridview second page not binding in ASP.NET

I want to bind student register number as in DataTable1 in Gridview1 with positions as in DataTable2 .
But, Gridview1 first page only bind, second page onwards not binding. I give my .net code , DB table structure and Gridview1 display below. If anybody give me solution, I will thankful to you.
C#.Net Code
private void dgvHallSeating()
{
DataSet ds = new DataSet();
DataTable dt;
DataTable dtSeatPositionsAssign = new DataTable();
DataColumn col1;
DataColumn col2;
DataColumn col3;
DataColumn col4;
DataColumn col5;
dt = new DataTable();
col1 = new DataColumn("col1", Type.GetType("System.String"));
col2 = new DataColumn("col2", Type.GetType("System.String"));
col3 = new DataColumn("col3", Type.GetType("System.String"));
col4 = new DataColumn("col4", Type.GetType("System.String"));
col5 = new DataColumn("col5", Type.GetType("System.String"));
dtSeatPositionsAssign.Columns.Add(col1);
dtSeatPositionsAssign.Columns.Add(col2);
dtSeatPositionsAssign.Columns.Add(col3);
dtSeatPositionsAssign.Columns.Add(col4);
dtSeatPositionsAssign.Columns.Add(col5);
Int32 SeatPositionColIndex = 0;
Int32 SeatPositionRowIndex = 0;
DataTable DataTable2 = new DataTable();
DataTable2 = CMSBL.dtecSeatSlot1Positions();
DataTable DataTable1 = CMSBL.rptecSeatSlot1Reports();
int RegNoRowIndex = 0;
int HallSeatPosition = 0;
for (; RegNoRowIndex < 11; RegNoRowIndex++)
{
for (; HallSeatPosition < DataTable2.Rows.Count; )
{
SeatPositionColIndex = Convert.ToInt32(DataTable2.Rows[HallSeatPosition]["ecGridColumnIndex"]);
SeatPositionColIndex = SeatPositionColIndex - 1;
SeatPositionRowIndex = Convert.ToInt32(DataTable2.Rows[HallSeatPosition]["ecGridRowIndex"]);
SeatPositionRowIndex = SeatPositionRowIndex - 1;
dtSeatPositionsAssign.Rows.Add();
if (SeatPositionRowIndex >= dtSeatPositionsAssign.Rows.Count || GridView1.Rows.Count <= 5)
{
dtSeatPositionsAssign.Rows.Add();
}
dtSeatPositionsAssign.Rows[SeatPositionRowIndex][SeatPositionColIndex] = DataTable1.Rows[RegNoRowIndex][3].ToString();
break;
}
HallSeatPosition++;
}
GridView1.DataSource = dtSeatPositionsAssign;
GridView1.DataBind();
}
**
DataTable1
**
Datatable2
**
Gridview1 Display

Compare a Datatable and string array in c#

I simply want to compare the first col of the datatable called "name" to items in the array. They can be multiple rows in the datatable with the same name. After it has compared all items in the array it should delete the rows that were NOT from the datatable. The output result can be the datatable itself or list array (prefer this) however this should retain all the columns from datatable. Possibly want to use linq query.
code:
DataTable dt = new DataTable("TestTable");
dt.Columns.Add(new DataColumn("email",System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("type",System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("card",System.Type.GetType("System.String")));
ArrayList al = new ArrayList();
al.Add("one#mail.com");
al.Add("two#mail.com");
al.Add("nine#mail.com");
al.Add("ten#mail.com");
DataRow r1 = dt.NewRow(); r1["email"] = "one#mail.com"; //addtype+card// dt.Rows.Add(r1);
DataRow r2 = dt.NewRow(); r2["email"] = "one#mail.com"; //addtype+card// dt.Rows.Add(r2);
DataRow r3 = dt.NewRow(); r3["email"] = "two#mail.com"; //addtype+card// dt.Rows.Add(r3);
DataRow r4 = dt.NewRow(); r4["email"] = "four#mail.com"; //addtype+card// dt.Rows.Add(r4);
DataRow r5 = dt.NewRow(); r5["email"] = "five#mail.com"; //addtype+card// dt.Rows.Add(r5);
So from the above row r4 and r5 will be deleted from the datatable.
You can do couple of things with your code.
First use List<string> instead of ArrayList (see this for details)
Second add rows to your DataTable, currently you are creating new
rows but you are not adding them.
Later with this query you can create a new DataTable which would have rows based on your List of emails.
DataTable dtNew = dt.AsEnumerable()
.Where(r => al.Contains(r.Field<string>("email")))
.CopyToDataTable();
So your complete code could be:
DataTable dt = new DataTable("TestTable");
dt.Columns.Add(new DataColumn("email", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("type", System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("card", System.Type.GetType("System.String")));
List<string> al = new List<string>(); //Use List<string>
al.Add("one#mail.com");
al.Add("two#mail.com");
al.Add("nine#mail.com");
al.Add("ten#mail.com");
DataRow r1 = dt.NewRow(); r1["email"] = "one#mail.com"; //addtype+card// dt.Rows.Add(r1);
dt.Rows.Add(r1);
DataRow r2 = dt.NewRow(); r2["email"] = "one#mail.com"; //addtype+card// dt.Rows.Add(r2); //Add Row to DataTable
dt.Rows.Add(r2);
DataRow r3 = dt.NewRow(); r3["email"] = "two#mail.com"; //addtype+card// dt.Rows.Add(r3);
dt.Rows.Add(r3);
DataRow r4 = dt.NewRow(); r4["email"] = "four#mail.com"; //addtype+card// dt.Rows.Add(r4);
dt.Rows.Add(r4);
DataRow r5 = dt.NewRow(); r5["email"] = "five#mail.com"; //addtype+card// dt.Rows.Add(r5);
dt.Rows.Add(r5);
DataTable dtNew = dt.AsEnumerable()
.Where(r => al.Contains(r.Field<string>("email")))
.CopyToDataTable();
Try
var resultTable = dt.AsEnumberable().Where(r => al.Contains(r.Field<string>("email")).Select(r => r).CopyToDataTable();

GridView doesn't show my datasource

I have gridview "gvData" ,i need to fetch the data of my function to gridview :
(I am using c# application)
algorithm.prison obj=new prison();
gvData.DataSource= obj.TitForTat();
After run i have a empty gridview.
Here is my code:
public DataSet TitForTat()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("myt");
dt.Columns.Add(new DataColumn("iteration", typeof(int)));
dt.Columns.Add(new DataColumn("prison1", typeof(string)));
dt.Columns.Add(new DataColumn("prison2", typeof (string)));
prison[] prisons = new prison[2];
prisons[0] = new prison();
prisons[1] = new prison();
//---------------------------
DataRow dr = dt.NewRow();
prisons[0]._state = "c";
prisons[1]._state = valueOfState[rd.Next(0, 1)];
dr["iteration"] = 0;
dr["prison1"] = "c";
dr["prison2"] = prisons[1]._state;
dt.Rows.Add(dr);
//----------------------
for (int i = 1; i <= _iteration; i++)
{
prisons[0]._state = prisons[1]._state;
prisons[1]._state = valueOfState[rd.Next(0, 1)];
DataRow dr1 = dt.NewRow();
dr1["iteration"] =i;
dr1["prison1"] = prisons[0]._state;
dr1["prison2"] = prisons[1]._state;
dt.Rows.Add(dr1);
}
ds.Tables.Add(dt);
return ds;
}
If it's an ASP.NET-webforms GridView-control you need to call DataBind() after you have assigned the DataSource:
gvData.DataSource = obj.TitForTat();
gvData.DataBind();
If it's winforms you have to set the DataMember property if you use a DataSet instead of a DataTable:
gvData.DataSource = obj.TitForTat();
gvData.DataMember = "myt";
... or use a DataTable in the first place:
DataSet ds = obj.TitForTat();
if(ds.Tables.Count > 0)
gvData.DataSource = ds.Tables[0];

"Input array is longer than the number of columns in this table" in c# application

i have windows application in which i am constructing dataset with two datatables one is "Products" and other is "TaxView".I have included sample code below.in this code i am getting error Input array is longer than the number of columns in this table
public DataTable[] getProductViewDetails(ArrayList ssd,ArrayList tax)
{
DataTable Products = new DataTable();
DataColumn SalesDetailID = Products.Columns.Add("SalesDetailID", typeof(System.INT32));
DataColumn BrandName = Products.Columns.Add("BrandName", typeof(System.String));
DataColumn ProductName = Products.Columns.Add("ProductName", typeof(System.String));
DataColumn Quantity = Products.Columns.Add("Quantity", typeof(System.INT32));
DataColumn Rate = Products.Columns.Add("Rate", typeof(System.INT32));
DataColumn Per = Products.Columns.Add("Per", typeof(System.String));
DataColumn Discount = Products.Columns.Add("Discount", typeof(System.String));
DataTable TaxView = new DataTable();
DataColumn SalesTaxDetailID = Products.Columns.Add("SalesTaxDetailID", typeof(System.Int32));
DataColumn TaxAmt = Products.Columns.Add("TaxAmt", typeof(System.Int32));
DataColumn Total = Products.Columns.Add("Total", typeof(System.Int32));
DataColumn TaxDesc = Products.Columns.Add("TaxDesc", typeof(System.String));
DataColumn TaxValues = Products.Columns.Add("TaxValues", typeof(System.INT32));
foreach (SalesStructDetails s in ssd)
{
//int previoustaxdetail;
Products.Rows.Add(1,HP, Printer, 2, 2100, pcs, 125);
foreach (TaxDetail t in tax)
{
if (s.SalesDetailId == t.SalesDetailID)
{
TaxView.Rows.Add(1, 125, 75, VAT, 4);
}
}
}
DataTable[] dt = new DataTable[2] { Products, TaxView };
return dt;
}
i am getting error at the line TaxView.Rows.Add(1, 125, 75, VAT, 4);
Look carefully - you are adding the tax column definitions to the products table. Add them to the tax table instead. Example:
DataTable TaxView = new DataTable();
DataColumn SalesTaxDetailID = Products.Columns.Add(blah);
Should be;
DataTable TaxView = new DataTable();
DataColumn SalesTaxDetailID = TaxView.Columns.Add(blah);
Also: consider using regular classes rather than DataTable

Categories