문제

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....
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top