And this is with datatables....note: the ToArray at the end is required as without it, the enumerable won't get iterated over and as such, the rows won't be added:
class Program
{
static void Main(string[] args)
{
string name = "A,B,C,D";
string value = "100,200,300,400";
var names = name.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var values = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
var table = new DataTable();
table.Columns.Add("Name", typeof (String));
table.Columns.Add("Value", typeof (int));
names.Zip(values, (n, v) => table.Rows.Add(n, v)).ToArray();
foreach(DataRow row in table.Rows)
{
Console.WriteLine("{0} -- {1}", row["Name"], row["Value"]);
}
}
}
"If I can see further than anyone else, it is only because I am standing on the shoulders of giants."blog: www.heartysoft.com twitter: @ashic
Marked as answer by HeartattacK on Jul 29, 2012 06:24 PM
tripati_tutu
Member
30 Points
53 Posts
How to add comma separated string into datatable in c#?
Jul 17, 2012 09:18 AM|LINK
Let's consider I have two comma separated strings as written below.
So I want to split these two strings and want to insert into DataTable which is as shown below.
Name Value
A 100
B 200
C 300
D 400
So please help me out on this.
Thanks in advance
shabirhakim1
Star
13496 Points
2145 Posts
Re: How to add comma separated string into datatable in c#?
Jul 17, 2012 09:27 AM|LINK
DataTable dt = new DataTable();
here is code for adding name string..you can fallow same for value
string str = string.Empty;
for (int i = 0; i < dt.Rows.Count; i++)
{
str = str + dt.Rows[i]["Name"].ToString();
str += (i < dt.Rows.Count-1) ? "," : string.Empty;
}
DarrellNorto...
All-Star
86645 Points
9629 Posts
Moderator
MVP
Re: How to add comma separated string into datatable in c#?
Jul 17, 2012 09:29 AM|LINK
Try this:
string name = "A,B,C,D"; string value = "100,200,300,400"; List<string> nameList = name.Split(new string[] {","}, StringSplitOptions.RemoveEmptyEntries).ToList(); List<string> valueList = value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList(); DataTable workTable = new DataTable("NameOfDataTable"); DataColumn column, column2; column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "Name"; workTable.Columns.Add(column); column2 = new DataColumn(); column2.DataType = System.Type.GetType("System.String"); column2.ColumnName = "Value"; workTable.Columns.Add(column2); for (int i = 0; i < nameList.Count; i++) { DataRow row = workTable.NewRow(); row["Name"] = nameList[i]; row["Value"] = valueList[i]; workTable.Rows.Add(row); }Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: How to add comma separated string into datatable in c#?
Jul 17, 2012 09:30 AM|LINK
string name = "A,B,C,D";
string value = "100,200,300,400";
var names = name.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var values = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var rows = names.Zip(values, (n, v) => new Row(n, v));
I've used a Row class, but that's essentially where you'll create the data row. And then you can insert them into a datatable.
blog: www.heartysoft.com
twitter: @ashic
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: How to add comma separated string into datatable in c#?
Jul 17, 2012 09:40 AM|LINK
And this is with datatables....note: the ToArray at the end is required as without it, the enumerable won't get iterated over and as such, the rows won't be added:
class Program { static void Main(string[] args) { string name = "A,B,C,D"; string value = "100,200,300,400"; var names = name.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); var values = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse); var table = new DataTable(); table.Columns.Add("Name", typeof (String)); table.Columns.Add("Value", typeof (int)); names.Zip(values, (n, v) => table.Rows.Add(n, v)).ToArray(); foreach(DataRow row in table.Rows) { Console.WriteLine("{0} -- {1}", row["Name"], row["Value"]); } } }blog: www.heartysoft.com
twitter: @ashic