-1

if i try to add int value into array with string length Whole data is "0" why? and how can solve it?

  for (int j = 0; j < dTable.Columns.Count; j++)
                for (int i = 0; i < dTable.Rows.Count; i++)
                {

                    mycounter[i] = dTable.Rows[i][j].ToString().Length;
                }

it is not related to mycounter because:

  string test = "";
           foreach(DataColumn dc in dTable.Columns)
                for (int i = 0; i < dTable.Rows.Count; i++)
                {
                   // MessageBox.Show(dTable.Rows[i][dc].ToString());
                    test = dTable.Rows[i][dc].ToString().Length.ToString();
                }
5
  • 1
    It may be obvious to someone with more C# experience...but my first thought was: What is the datatype of dTable and mycounter? The more information you provide with your questions, the better people can help you.
    – KevenK
    Jun 18, 2010 at 14:58
  • mycounter must be some descendant of IEnumerable<int>, like a List<int>. Jun 18, 2010 at 15:04
  • I'm not sure what your update even means. You set test to the last column in the last row and.... ?
    – Marc
    Jun 18, 2010 at 15:15
  • What kind of data stored in DataTable? Jun 18, 2010 at 15:16
  • So in your update, you mean to say: When I inspect test, the value is always "0". Right?
    – JeffH
    Jun 18, 2010 at 15:25

3 Answers 3

2

In mycounter[] you will have only values from last column of table. May be last column contains empty strings?

1

In addition to what @Orsol said, the fact that you're looping through both dimensions seems to indicate that you want a total length for each row, and therefore,

mycounter[i] = dTable.Rows[i][j].ToString().Length; 

should be changed to

mycounter[i] += dTable.Rows[i][j].ToString().Length; 

It is also possible that the ToString() method is returning empty strings. Perhaps you're looking for a Text property?

0

Unless you are redeclaring mycounter between the for loops, you are overwriting mycounter[i] for each column 0 to Columns.Count-1 until the last column.

Did you want to sum the lengths? Or did you maybe want a 2-dimensional array for mycounter?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.