2

hi im formulating my question now better.

in the foreach command i get for temp(save variable) values in an array. The values that i save in temp are from the datagridview cells.

in the next step, in the for command, i want to compare 2 strings, the string and the next string, if the fist string is bigger than the second, i want to change their positions. But there is the problem, they dont change positions they even get an empty value, and i cant understand why they get an empty value.

Im thinking that they get an empty value because of the foreach command, the index [i] just stays the same, but if i would put in i = i+1, the command would be out of bound.

Thank you

senc. NIko

foreach (DataGridViewRow row in dataGridView1.Rows)     
{
    if (row.Cells[i].Value == null)
    {
        //MessageBox.Show("This row is empty");
        break;
    }
    if (row.Cells[i].Value != null)
    {
        temp = row.Cells[i].Value.ToString();
        UnsortArray[i] = temp;
        i = i + 1;

    }
}

for (int a = 0; a < MaxZeilen; a++)
{
    if (i < MaxZeilen)
    {
        *if (String.Compare(UnsortArray[a], UnsortArray[a + 1]) > 0) 
        {
            UnsortArray[a] = temp;
            UnsortArray[a + 1] = temp2;
            temp = UnsortArray[a + 1];
            temp2 = UnsortArray[a];
        }*
    }
}

for (int i = 0; i < MaxZeilen; i++)
{
    UnsortArray[i] = SortArray[i];
    MessageBox.Show(UnsortArray[i]);
}
6
  • Do you want to sort all strings or just swap strings near each other? Your code doesn't sort all strings. {"22", "33", "11"} will become {"22", "11", "33"} Commented Sep 24, 2012 at 13:12
  • are you sure, that UnsortArray contains right strings after foreach code? Commented Sep 24, 2012 at 13:14
  • yes it was mentioned to sort the strings. but not with numbers, with names. like {"Pepsi" "Cola" "Fanta" "Orangejuice"} should become (alphabetic) {"Cola" "Fanta" "Orangejuice" "Pepsi"}. My mentor said it would work with compare, but im not sure.. Commented Sep 24, 2012 at 13:15
  • Please read some articles about sorting algorithms en.wikipedia.org/wiki/Sorting_algorithm Commented Sep 24, 2012 at 13:17
  • And in your foreach loop you read 1 value from 1 row and each time it is new column. Is it ok? Commented Sep 24, 2012 at 13:25

7 Answers 7

1

You're backwards, assigning the array to your temp variables before assigning the temp variables:

UnsortArray[a] = temp;
UnsortArray[a + 1] = temp2;
temp = UnsortArray[a + 1];
temp2 = UnsortArray[a];

Try:

temp = UnsortArray[a + 1];
temp2 = UnsortArray[a];
UnsortArray[a] = temp;
UnsortArray[a + 1] = temp2;

And you've done it again here UnsortArray[i] = SortArray[i];. I think you mean SortArray[i] = UnsortArray[i];

Sign up to request clarification or add additional context in comments.

2 Comments

unfortunetlly it doesnt works, changes nothing -.- BUT THANK YOU
ok i understand now whats going on, thank you for your help. but the problem still exists, as you can see i created a MessageBox for showing the values, and all boxes are empty. so sth is wrong, im searching further!
1
    temp = UnsortArray[a + 1];    

    UnsortArray[a + 1] = UnsortArray[a];

    UnsortArray[a] = temp;

instead of

          UnsortArray[a] = temp;

          UnsortArray[a + 1] = temp2;

          temp = UnsortArray[a + 1];

          temp2 = UnsortArray[a];

Comments

1

Why using an array and not a List ?

This page tells about sorting lists.

Comments

1

Here is some improvements to your code. You had an error at the top that could cause a infinite loop and just use the Array.Sort method. If you want it to sort differently, add an IComparable interface.

foreach (DataGridViewRow row in dataGridView1.Rows)     
{
     if (row.Cells[i].Value == null)
     {
          //MessageBox.Show("This row is empty");
          i++;
          break;
     }
     else (row.Cells[i].Value != null)
     {
          UnsortArray[i] = row.Cells[i].Value.ToString();
          i++;
     }
}  

Array.Sort(UnsortArray);
SortArray = UnsortArray;

3 Comments

i dont know the sort method of array.sort. as mentioned i need to create an bubblesorting namesort programm.
Completely unrelated to this answer, but just wanted to message you: re your edit here - no, that is incorrect: you can have as many using statements as you want, with a single set of braces. The code compiles perfectly "as is"
Thanks Marc. I took your code and compiled it, and it does compile. Thanks for the info. I also found another post that explains that in more detail. I couldn't find any info about this on the Microsoft site. My initial mistake might have been putting a semicolon after the using statements, and that would not compile.
1
    List<String> itemList = new List<string>();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.Cells[0].Value = null)
        {
            itemList.Add(row.Cells[0].Value.ToString());
        }
    }

    itemList.Sort();
    string[] SortedArray = itemList.ToArray();

    for (int j = 0; j < SortedArray.Length; j++)
    {
        MessageBox.Show(SortedArray[j]);
    }

and if this code doesn't work, than add

foreach(string item in itemList)
{
    MessageBox.Show(item);
}

after first foreach loop and check the values

1 Comment

if (row.Cells[0].Value == null) ---> again null exception problem on itemlist.add code
1

Replace it with the following code block

int i= 0;
foreach (DataGridViewRow row in dataGridView1.Rows)     
{
    if (row != null)
    {
        temp = row.ToString();
        UnsortArray[i] = temp;
        i = i + 1;
    }
}


for (int a = 0; a < MaxZeilen; a++)
{
    if (String.Compare(UnsortArray[a], UnsortArray[a + 1]) > 0) 
    {
        temp = UnsortArray[a + 1];             
        UnsortArray[a + 1] = UnsortArray[a];
        UnsortArray[a] = temp;
    }
}
for (int i = 0; i < MaxZeilen; i++)
{
    MessageBox.Show(UnsortArray[i]);
}

10 Comments

i have tried this already, but the code doesnt change anything. THANK YOU FOR YOUR HELP
did you notice that i have removed the outer if condition ?
yes i did. your explaination was usefull for me, i would give up an up but i dont have enough reputations -.- No it didnt helped, as already said :&
are you setting the value of i above foreach loop ?
this is the case when you are comparing values of different columns in a datagrid. if you are try to compare values of same columns and different row then you need a different construct.
|
1
int MaxArrayCount = 0; 
foreach(DataGridViewRow row in dataGridView1.Rows)    
{ 
MaxArrayCount += row.Cells.Count;
 }
 string[] UnsortArray= new string[MaxArrayCount];



int cnt = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    for (int i = 0; i < row.Cells.Count; i++ )
                    {
                        if (row.Cells[i].Value != null)
                        {
                            temp = row.Cells[i].Value.ToString();

                            UnsortArray[cnt] = temp;

                            cnt++;
                        } 
                    }

                }



for (int b = 1; b < UnsortArray.Count; b++)
{    
  for (int a = 0; a < UnsortArray.Count - 1; a++)   
     {    

    if (String.Compare(UnsortArray[a], UnsortArray[a + 1]) > 0)
     {

      temp = UnsortArray[a + 1];

      UnsortArray[a + 1] = UnsortArray[a];

      UnsortArray[a] = temp;

    }    
  }    
}


for (int i = 0; i < MaxZeilen; i++)
{
MessageBox.Show(UnsortArray[i]);
}

5 Comments

No doesnt work, i dont know how often do i say, i cant ++cnt, if i count cnt++ i get an out of bound, why does nobody understand this? THANK YOU
you cant get out of bound for cnt++ , until you have UnsortedArray[] limited in length , use: int MaxArrayCount = 0; foreach (DataGridViewRow row in dataGridView1.Rows) { MaxArrayCount += row.Cells.Count; } string[] UnsortArray= new string[MaxArrayCount];
ok you were right!!!! You solved it, THANK YOU VERY MUCH!!!!! Im now trying to get the values into another datagrid, little bit difficult but im optimistic =) THUMBS UP FOR HIM!!!!! ty
SURE, please do it, i have tried it yesterday, but i have too less reputations
i guess now you have enough repo to mark this as an answer. lol i'm being annoying i know, but still :D could you? D:D:

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.