I have a DataTable in C#, and I would like to loop through each row of a particular column with the title specified.
Below is my code snippet:
foreach (DataRow listing in dtAll.Rows)
{
//get the value of <ID> column for each row
int itemID = ["ID"].value;
}
May I know how can it be done? Thank you.
foreach (DataRow listing in dtAll.Rows)
{
int itemID = listing.Field<int>("ID");
}
but note comments above as a search would probably have saved you posting
Related
Im new to DataSets and I am trying to get a specific column from a DataSet. I wrote this code
using (var dataset = new U2ZFDataSetTableAdapters.stationenTableAdapter())
{
var ds = new U2ZFDataSet();
dataset.Fill(ds.stationen);
var rows = ds.stationen.Select("pdvorhanden = 1");
foreach (DataRow row in rows)
{
foreach (DataColumn column in ds.stationen.Columns)
{
if(column.ColumnName == "Bezeichnung")
listOfStations.Add(row[column].ToString());
}
}
}
The code feels slow to me. Isnt there a better way to do this? How can I get a specific Colum from DataRow without iterating through every column of the row?
Access to the column via column name.
foreach (DataRow row in rows)
{
listOfStations.Add(row["Bezeichnung"].ToString());
}
https://msdn.microsoft.com/en-us/library/146h6tk5(v=vs.110).aspx
So I have a DataTable which uses a "SELECT * FROM People WHERE ID = ?" you can understand that this will only retrieve one row as the ID is unique
I have casted it to a list:
List<DataTable> userInfo = (List<DataTable>)HttpContext.Current.Session["userDT"];
Now I am trying to do a foreach loop that would loop through each row and store the column into a session, this is what I have so far:
However I get an error:
Cannot convert type 'System.Data.DataTable' to 'System.Data.DataRow'
Does anyone know what I am doing wrong?
userInfo is a list of tables. You have to iterate through it, and for each table you can access that table's rows:
foreach (DataTable dt in userInfo)
{
foreach (DataColumn column in dt.Columns)
{
// if you want column names here is how you would get them
string columnName = column.ColumnName;
foreach (DataRow row in dt.Rows)
{
// if you want the values in the cells, here's where you get them
object value = row[column];
}
}
}
I have a datatable has data like this format
........ IVR........
.........IVR........
.........IVR........
.........City1......
.........City1......
.........City1......
.........City2......
.........City2......
.........City2......
I want to take the last row of each value. in order words, the rows that are bold now
The challenge is that i wan these three rows in a datatable. I tried to search on internet but i didn't know what is the name of this feature. could you help me please
You can GroupBy() and then select last row with the help of the Last() method.
var result = from b in myDataTable.AsEnumerable()
group b by b.Field<string>("Your_Column_Name") into g
select g.Last();
DataTable filtered = myDataTable.Clone();
foreach(DataRow row in result)
{
filtered.ImportRow(row);
}
Clone clones the structure of the DataTable, including all DataTable schemas and constraints.
This can be implemented in a simple loop using a Dictionary to hold found rows:
var cRows = new Dictionary<string, DataRow>(StringComparer.InvariantCultureIgnoreCase);
foreach (DataRow oRow in oTable.Rows)
{
var sKey = oRow["KeyValue"].ToString();
if (!cRows.ContainsKey(sKey))
{
cRows.Add(sKey, oRow);
}
else
{
cRows[sKey] = oRow;
}
}
This approach will store the last row for each unique value in the column that you nominate.
To move the selected rows into a new DataTable:
var oNewTable = oTable.Clone();
foreach (var oRow in cRows.Values)
{
oNewTable.Rows.Add(oRow);
}
Clone just clones the structure of the current table, not the rows.
I have a DataTable that I'm exporting to pdf and there are numeric value like status id that I want to change to strings for example:
dataRow["statusId"] = 1; //dataRow["statusId"] type is int
and I want it to be:
dataRow["statusId"] = "Open file";
How can I do it without coping the entire data table ?
You actually can't do it. The best way is to actually clone the dataTable and then copy each row:
DataTable dtCloned = dt.Clone();
dtCloned.Columns[0].DataType = typeof(Int32); //in the column you actually need.
foreach (DataRow row in dt.Rows)
{
dtCloned.ImportRow(row);
}
Source: How To Change DataType of a DataColumn in a DataTable?
I'm trying to append more values the rows of a DataTable. The original data read from database doesn't have two additional columns I'd like to add. I've got this so far -
myTable.Columns.Add("type", typeof(int));
myTable.Columns.Add("rate", typeof(int));
foreach (DataRow rows in myTable.Rows)
{
if (rows["dst"] == "1875")
{
//How to append values to this current row?
}
}
Please advice.
Here is the answer
foreach (DataRow rows in myTable.Rows)
{
if (rows["dst"] == "1875")
{
//How to append values to this current row?
rows["type"] = 32;
rows["rate"] = 64;
}
}
Also as a best practice - change rows in your for loop to row. It should be singular as it represents a single object - not a collection.