I am trying to get the value of a specific cell in a dataset. I have a dataset that has at least two tables(could be more). One of the tables has two columns - Name$ and Value$.
I have to search through the dataset by the name column where the name is "FirstName" and save the value corresponding to that specific name.
Here is what I have so far:
string val = null;
foreach (DataTable dt in ds.Tables)
{
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
object item = dr[dc];
if (item.ToString().Equals("Name$"))
{
// store the value for that name
}
}
}
}
Any ideas how this could happen, keeping in mind that there could be many tables in the dataset.
EDIT: Here is the full solution:
foreach (DataTable dt in ds.Tables)
{
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
if (dc.ColumnName.ToString().Equals("Name$"))
{
if (row["Name$"].ToString().Equals("FirstName"))
{
firstName = (string)row[row.Table.Columns["Name$"].Ordinal + 1];
}
if (row["Name$"].ToString().Equals("LastName$"))
{
lastName = (string)row[row.Table.Columns["Name$"].Ordinal + 1];
}
}
}
}
}
Use ColumnName property
string val = null;
foreach (DataTable dt in ds.Tables)
{
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
if(dc.ColumnName=="Name")
{
//save
}
}
}
}
foreach (DataRow dr in ds.Tables[0].Rows) //Tables[1]....
{
if(dr["ColumName"].ToString()==....)
// store the value for that name
}
for (int i = 0; i < ds.Tables.Count; i++)
{
foreach (DataRow dr in ds.Tables[i].Rows)
{
//dr....
}
}
Related
I have following string. I am trying to insert these details into dataTable but last columns in below string ("incident" column) not inserting into data table.The code is working based on first 4 column. I am getting these values from frontend using JavaScript and I have assigned to string.
String given below
[{"CompanayID":"k123","Role":"Admin","Country":"UK","Asset":"HD"},
"CompanayID":"k234","Role":"User","Country":"US","Asset":"HD12","incident":"abc 1"}]
I have done following code but it doesn't take last column
DataTable dt = new DataTable("UserDetails");
JavaScriptSerializer serializer = new JavaScriptSerializer();
object[] objCustomers = (object[])serializer.DeserializeObject(customers);
var columnNames = ((Dictionary<string, object>)objCustomers[0]).Select(x => x.Key).ToList();
for (int i = 0; i < columnNames.Count; i++)
{
dt.Columns.Add(columnNames[i]);
}
for (int i = 0; i < objCustomers.Length; i++)
{
Dictionary<string, object> keyValue = (Dictionary<string, object>)objCustomers[i];
DataRow dr = dt.NewRow();
foreach (KeyValuePair<string, object> item in keyValue)
{
for (int k = 0; k < columnNames.Count(); k++)
{
if (item.Key == columnNames[k].ToString())
{
dr[columnNames[k]] = item.Value;
break;
}
}
}
dt.Rows.Add(dr);
}
You are making the columns depending on the first object and it does not contain the last one 'incident'.
You Have more than a way to do it
Adding the columns depending on the second object
string customers = "[{\"CompanayID\":\"k123\",\"Role\":\"Admin\",\"Country\":\"UK\",\"Asset\":\"HD\"}, {\"CompanayID\":\"k234\",\"Role\":\"User\",\"Country\":\"US\",\"Asset\":\"HD12\",\"incident\":\"abc 1\"}]";
DataTable dt = new DataTable("UserDetails");
var rows = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(customers);
foreach (var c in rows[1])
{
dt.Columns.Add(c.Key);
}
foreach (var row in rows)
{
DataRow dr = dt.NewRow();
foreach (var innerColumn in row)
{
dr[innerColumn.Key] = innerColumn.Value;
}
dt.Rows.Add(dr);
}
}
Add missing column to the first object as "" or null
string customers = "[{\"CompanayID\":\"k123\",\"Role\":\"Admin\",\"Country\":\"UK\",\"Asset\":\"HD\",\"incident\":\"\"}, {\"CompanayID\":\"k234\",\"Role\":\"User\",\"Country\":\"US\",\"Asset\":\"HD12\",\"incident\":\"abc 1\"}]";
DataTable dt = new DataTable("UserDetails");
var rows = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(customers);
foreach (var c in rows[0])
{
dt.Columns.Add(c.Key);
}
foreach (var row in rows)
{
DataRow dr = dt.NewRow();
foreach (var innerColumn in row)
{
dr[innerColumn.Key] = innerColumn.Value;
}
dt.Rows.Add(dr);
}
Update (Dynamic Way)
string customers = "[{\"CompanayID\":\"k123\",\"Role\":\"Admin\",\"Country\":\"UK\",\"Asset\":\"HD\",\"incident\":\"\"}, {\"CompanayID\":\"k234\",\"Role\":\"User\",\"Country\":\"US\",\"Asset\":\"HD12\",\"incident\":\"abc 1\"}]";
DataTable dt = new DataTable("UserDetails");
var rows = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(customers);
foreach (var row in rows)
{
DataRow dr = dt.NewRow();
foreach (var innerColumn in row)
{
if (!dt.Columns.Contains(innerColumn.Key))
{
dt.Columns.Add(innerColumn.Key);
}
dr[innerColumn.Key] = innerColumn.Value;
}
dt.Rows.Add(dr);
}
check https://dotnetfiddle.net/RsOu87
i am trying to add a new column to a data table that is the rank. For that I want to loop through each row after sorting and then assign a rank to each row starting at 1.
Below is my code. However The rank is getting assigned to the row before the sort it looks like.
So my rank calculation is not doing what its supposed to do. Is it possible to add a rank this way?
protected void bindGridView(GridView gv)
{
using(DataTable dt = getData())
{
DataColumn dc_Total = new DataColumn("Total");
DataColumn dc_Rank = new DataColumn("Rank");
dt.Columns.Add(dc_Total);
dt.Columns.Add(dc_Rank);
dc_Rank.SetOrdinal(1);
dc_Total.SetOrdinal(2);
foreach (DataRow dr in dt.Rows)
{
dr["Total"] = Convert.ToDouble(dr["A_B_PERC"].ToString()) + Convert.ToDouble(dr["C_PERC"].ToString());
}
dt.DefaultView.Sort = "Total";
int rank = 1;
foreach(DataRow dr in dt.Rows)
{
dr["Rank"] = rank;
rank++;
}
gv.DataSource = dt;
gv.DataBind();
}
}
Below is the result I'm getting:
What I really want is:
I was able to fix my problem with the code below:
protected void bindGridView(GridView gv)
{
using(DataTable dt = getData())
{
DataColumn dc_Total = new DataColumn("Total");
DataColumn dc_Rank = new DataColumn("Rank");
dt.Columns.Add(dc_Total);
dt.Columns.Add(dc_Rank);
dc_Rank.SetOrdinal(1);
dc_Total.SetOrdinal(2);
foreach (DataRow dr in dt.Rows)
{
dr["Total"] = Convert.ToDouble(dr["A_B_PERC"].ToString()) + Convert.ToDouble(dr["C_PERC"].ToString());
}
dt.DefaultView.Sort = "Total";
using(DataView dv = dt.DefaultView)
{
int rank = 1;
foreach (DataRowView drv in dv)
{
drv["Rank"] = rank;
rank++;
}
gv.DataSource = dv;
gv.DataBind();
}
}
}
Hope this helps if anyone else is having similar issue.
In C#, I'm trying to loop through my dataset to show data from each row from a specific column. I want the get each date under the column name "TaskStart" and display it on a report, but its just shows the date from the first row for all rows can anybody help?
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dr in table.Rows)
{
DateTime TaskStart = DateTime.Parse(
ds.Tables[0].Rows[0]["TaskStart"].ToString());
TaskStart.ToString("dd-MMMM-yyyy");
rpt.SetParameterValue("TaskStartDate", TaskStart);
}
}
I believe you intended it more this way:
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dr in table.Rows)
{
DateTime TaskStart = DateTime.Parse(dr["TaskStart"].ToString());
TaskStart.ToString("dd-MMMM-yyyy");
rpt.SetParameterValue("TaskStartDate", TaskStart);
}
}
You always accessed your first row in your dataset.
DateTime TaskStart = DateTime.Parse(dr["TaskStart"].ToString());
foreach (DataRow dr in ds.Tables[0].Rows)
{
//your code here
}
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dr in table.Rows)
{
var ParentId=dr["ParentId"].ToString();
}
}
foreach (DataRow table in ds.Tables[0].Rows)
{
int ForType = Convert.ToInt32(table["Fortype"].ToString());
ds.Tables[0].DefaultView.RowFilter = "ForType ='" + ForType +"'";
rbtnQuestion.DataSource = ds.Tables[0].DefaultView;
rbtnQuestion.DataBind();
}
I have a DataSet named DataSet1. It contains an unknown number of tables and an unknown number of columns and rows in those tables. I would like to loop through each table and look at all of the data in each row for each column. I'm not sure how to code this. Any help would be appreciated!
foreach (DataTable table in dataSet.Tables)
{
foreach (DataRow row in table.Rows)
{
foreach (object item in row.ItemArray)
{
// read item
}
}
}
Or, if you need the column info:
foreach (DataTable table in dataSet.Tables)
{
foreach (DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
{
object item = row[column];
// read column and item
}
}
}
Just loop...
foreach(var table in DataSet1.Tables) {
foreach(var col in table.Columns) {
...
}
foreach(var row in table.Rows) {
object[] values = row.ItemArray;
...
}
}
How do I loop through each column in a datarow using foreach?
DataTable dtTable = new DataTable();
MySQLProcessor.DTTable(mysqlCommand, out dtTable);
foreach (DataRow dtRow in dtTable.Rows)
{
//foreach(DataColumn dc in dtRow)
}
This should work:
DataTable dtTable;
MySQLProcessor.DTTable(mysqlCommand, out dtTable);
// On all tables' rows
foreach (DataRow dtRow in dtTable.Rows)
{
// On all tables' columns
foreach(DataColumn dc in dtTable.Columns)
{
var field1 = dtRow[dc].ToString();
}
}
I believe this is what you want:
DataTable dtTable = new DataTable();
foreach (DataRow dtRow in dtTable.Rows)
{
foreach (DataColumn dc in dtRow.ItemArray)
{
}
}
You can do it like this:
DataTable dt = new DataTable("MyTable");
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
if (row[column] != null) // This will check the null values also (if you want to check).
{
// Do whatever you want.
}
}
}
You can check this out. Use foreach loop over a DataColumn provided with your DataTable.
foreach(DataColumn column in dtTable.Columns)
{
// do here whatever you want to...
}
Something like this:
DataTable dt = new DataTable();
// For each row, print the values of each column.
foreach(DataRow row in dt .Rows)
{
foreach(DataColumn column in dt .Columns)
{
Console.WriteLine(row[column]);
}
}
http://msdn.microsoft.com/en-us/library/system.data.datatable.rows.aspx
In LINQ you could do something like:
foreach (var data in from DataRow row in dataTable.Rows
from DataColumn col in dataTable.Columns
where
row[col] != null
select row[col])
{
// do something with data
}
int countRow = dt.Rows.Count;
int countCol = dt.Columns.Count;
for (int iCol = 0; iCol < countCol; iCol++)
{
DataColumn col = dt.Columns[iCol];
for (int iRow = 0; iRow < countRow; iRow++)
{
object cell = dt.Rows[iRow].ItemArray[iCol];
}
}