Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, November 17, 2014 12:08 PM
DataTable dt = new DataTable();
DataTable dx = new DataTable();
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
foreach (DataColumn drx in dx.Rows)
{
if ((dr["Day/Hour"] == drx["NameDay"]) && (dc.ColumnName == drx["NameHour"]))
{
dt.Rows[row][col] = drx["IdStatus"];
}
}
col++;
}
row++;
}
error : Cannot apply indexing with [] to an expression of type 'System.Data.DataColumn'
All replies (4)
Monday, November 17, 2014 2:47 PM âś…Answered
drx should probably be DataRow instead of a DataColumn (at least dx.Rows is a collection of DataRows and nothing else...):
DataTable dt = new DataTable();
DataTable dx = new DataTable();
int row = 0;
int col = 0;
foreach (DataRow dr in dt.Rows) {
foreach (DataColumn dc in dt.Columns) {
foreach (DataRow drx in dx.Rows) {
if ((dr["Day/Hour"] == drx["NameDay"]) && (dc.ColumnName == drx["NameHour"])) {
dt.Rows[row][col] = drx["IdStatus"];
}
}
col++;
}
row++;
}
}
Please remember to mark helpful posts as answer and/or helpful.
Monday, November 17, 2014 12:39 PM
Imho it should be foreach (DataRow drx in dx.Rows). But I don't get your logic behind it.
Monday, November 17, 2014 12:44 PM
The code below should run without errors. I won't guarantee you will get the results you are looking for. The logic in your code doesn't make complete sense. If you describe what you are looking to do I may be able to provide better code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
DataTable dx = new DataTable();
for (int dr = 0; dr < dt.Rows.Count - 1; dr++)
{
for (int drx = dr + 1; drx < dt.Rows.Count; drx++)
{
for (int col = 0; col < dt.Columns.Count; col++)
{
if ((dt.Rows[dr]["Day/Hour"] == dt.Rows[drx]["NameDay"]) && (dt.Columns[col].ColumnName == "NameHour"))
{
dt.Rows[dr][col] = dt.Rows[drx]["IdStatus"];
}
}
}
}
}
}
}
jdweng
Monday, November 17, 2014 2:39 PM
You do not declare the variables col and row