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();
Related
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;
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
I have DataGridView that i am adding items via DataTable
This items read from XML file and inside this XML file,
those items not sorted and i want to sort is before add to my DataGridView
private void UpdateDataGdirView(List<Vendor> list)
{
DataTable dt = new DataTable();
dt.Columns.Add("Column1", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column4", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column5", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Column6", System.Type.GetType("System.Boolean"));
DataRow dr;
foreach (Vendor vendor in list)
{
dr = dt.NewRow();
dr["Column1"] = vendor.IsVendorChecked;
dr["Column2"] = vendor.Number;
dr["Column3"] = vendor.Name;
dr["Column4"] = vendor.Size;
dr["Column5"] = vendor.Path;
dr["Column6"] = vendor.Path2;
dt.Rows.Add(dr);
}
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
this.Invoke((MethodInvoker)delegate { dataGridView1.DataSource = dt; });
}
you can use
dt.defaultview.sort="columnname asc/desc"
and then bind the datatable to its destination.
I think you should use DataView for this. Like :
DataTable orders = dataSet.Tables["SalesOrderHeader"];
EnumerableRowCollection<DataRow> query =
from order in orders.AsEnumerable()
orderby order.Field<decimal>("TotalDue")
select order;
DataView view = query.AsDataView();
bindingSource1.DataSource = view;
Follow this link : Query on DataView
OR
You can use like :
DataTable orders = dataSet.Tables["SalesOrderHeader"];
DataView dv = new DataView(orders);
dv.Sort = "TotalDue";
dataGridView1.DataSource = dv;
I have a gridview which will contain some 'n' number of rows.... Now i want to add all rows of the gridview to a datatable which will be used for bulkcopy operation...
I have found this http://www.codeproject.com/KB/aspnet/GridView_To_DataTable.aspx
But i want all columns of my gridview to be added to the datarow of the datatable
Grid http://img85.imageshack.us/img85/4044/gridp.jpg
I want to convert gridview to datatable on submit.... Any suggestion...
EDIT:
Answer below works and i have found an answer too...
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("EmpId", typeof(Int64)));
dt.Columns.Add(new DataColumn("FromDate", typeof(DateTime)));
dt.Columns.Add(new DataColumn("ToDate", typeof(DateTime)));
dt.Columns.Add(new DataColumn("DaysPresent", typeof(double)));
dt.Columns.Add(new DataColumn("OpeningAdvance", typeof(double)));
dt.Columns.Add(new DataColumn("AdvanceDeducted", typeof(double)));
dt.Columns.Add(new DataColumn("RemainingAdvance", typeof(double)));
dt.Columns.Add(new DataColumn("SalaryGiven", typeof(double)));
dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));
foreach (GridViewRow row in gridEmployee.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
DataRow dr = dt.NewRow();
dr["EmpId"] = Convert.ToInt64(((HiddenField)row.Cells[0].FindControl("HiddenId")).Value);
dr["FromDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(fromdate[1].ToString()) + '/' + fromdate[0].ToString() + '/' + fromdate[2].ToString());
dr["ToDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(todate[1].ToString()) + '/' + todate[0].ToString() + '/' + todate[2].ToString());
dr["DaysPresent"] = Convert.ToDouble(((TextBox)row.Cells[3].FindControl("TxtDaysPresent")).Text);
dr["OpeningAdvance"] = Convert.ToDouble(((TextBox)row.Cells[4].FindControl("txtOpeningAdv")).Text);
dr["AdvanceDeducted"] = Convert.ToDouble(((TextBox)row.Cells[5].FindControl("TxtAdvanceDeducted")).Text);
dr["RemainingAdvance"] = Convert.ToDouble(((TextBox)row.Cells[6].FindControl("TxtClosingAdvance")).Text);
dr["SalaryGiven"] = Convert.ToDouble(((TextBox)row.Cells[7].FindControl("TxtSalary")).Text);
dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
dt.Rows.Add(dr);
}
}
SqlBulkCopy sbc = new SqlBulkCopy(connectionString);
sbc.DestinationTableName = "SalaryDetails";
sbc.ColumnMappings.Add("EmpId", "EmpId");
sbc.ColumnMappings.Add("FromDate", "FromDate");
sbc.ColumnMappings.Add("ToDate", "ToDate");
sbc.ColumnMappings.Add("DaysPresent", "DaysPresent");
sbc.ColumnMappings.Add("OpeningAdvance", "OpeningAdvance");
sbc.ColumnMappings.Add("AdvanceDeducted", "AdvanceDeducted");
sbc.ColumnMappings.Add("RemainingAdvance", "RemainingAdvance");
sbc.ColumnMappings.Add("SalaryGiven", "SalaryGiven");
sbc.ColumnMappings.Add("CreatedDate", "CreatedDate");
sbc.WriteToServer(dt);
sbc.Close();
you can traverse datagrid row by row and make a comma separated file. then use Bulk insert or bcp for inserting data to db.
Another Solution
DataTable dt = new DataTable();
for (int j = 0; j < grdList.Rows.Count; j++)
{
DataRow dr;
GridViewRow row = grdList.Rows[j];
dr = dt.NewRow();
for (int i = 0; i < row.Cells.Count; i++)
{
dr[i] = row.Cells[i].Text;
}
dt.Rows.Add(dr);
}
SqlBulkCopy sbc = new SqlBulkCopy(targetConnStr);
sbc.DestinationTableName = "yourDestinationTable";
sbc.WriteToServer(dt);
sbc.Close();
I have a List<string> and I have a DataTable.
One of the columns in a DataRow is ID. The List holds instances of this ID.
The DataTable gets populated on a Timer.
I want to return items from the List that are not in the DataTable into another list.
You will want to do something like this
var tableIds = table.Rows.Cast<DataRow>().Select(row => row["ID"].ToString());
var listIds = new List<string> {"1", "2", "3"};
return listIds.Except(tableIds).ToList();
You can cast the rows in the data table to be an IEnumerable collection and then select the "ID" column value from each of them. You can then use the Enumerable.Except extension method to get all of the values from the List that are not in the collection you just made.
If you need to get the values that are in the table but not the list, just reverse listIds and tableIds.
If your table was something like that:
DataTable dt = new DataTable();
dt.Columns.Add("ID");
DataRow dr = dt.NewRow();
dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
dr["ID"] = "1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = "2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = "3";
dt.Rows.Add(dr);
and the list was something like this:
List<string> ls = new List<string>{"1","2","4"};
we could get the items found in the list and not in the datatable this way:
var v = from r in ls
where !dt.Rows.Contains(r)
select r;
v.ToList();
With reasonable efficiency via HashSet<T> (and noting that the fastest way to get data out of a DataRow is via the DataColumn indexer):
HashSet<int> ids = new HashSet<int>();
DataColumn col = table.Columns["ID"];
foreach (DataRow row in table.Rows)
{
ids.Add((int)row[col]);
}
var missing = list.Where(item => !ids.Contains(item.ID)).ToList();