0

I am working on a program in c# . There are 3 columns. Let's call them A,B and C. When any row in column B is unchecked but column A is checked, it should pop out a display warning message indicating which location id (rows) that has column A checked but column B unchecked.

I managed to create the display message to do this but instead of getting the location in the message box, it shows the values in : " DataGridView Column [0] Row[2] ".

I figured I have to convert my List of locationID variable to convert to string but the results are the same.

Here is the code where ListLocationID is declared:

 List<string> listLocationID = new List<string>();
                List<int> listLocationIDIndex = new List<int>();


    for (int i = 0; i < this.dataGridLocationDetails.Rows.Count; i++)
                {
         if (((Convert.ToBoolean(dataGridLocationDetails.Rows[i].Cells["colViewPermission"].Value) == true)
                         && (Convert.ToBoolean(dataGridLocationDetails.Rows[i].Cells["colIssuePermission"].Value) == false)
                     ))
                    {
                            listLocationID.Add(dataGridLocationDetails.Rows[i].Cells["colLocationID"].ToString());

               listLocationIDIndex.Add(i);

     }


                        }

                bool isEmpty = true;
                if (listLocationID.Count > 0)
                    isEmpty = false;

        if(!isEmpty) 
        {
            string message = string.Format("The following Location ID has view permission but not issue permission:{0}  Do you wish to continue?", DisplayString(listLocationID));
        DialogResult dialogResult = MessageBox.Show(message, "WARNING", MessageBoxButtons.YesNo);

          if (dialogResult == DialogResult.Yes)                       
         {                            
        for (int i = 0; i < listLocationIDIndex.Count; i++)                
        {

        }
         }                        
        else if (dialogResult == DialogResult.No)                       
         {                            
        return;                        
        }
        } 

Here is part of the code to display the string:

 private string DisplayString(List<string> listLocationID)
        {
            String strSentence = string.Empty;
            for (int i = 0; i < listLocationID.Count; i++)
            {
                strSentence = strSentence + (listLocationID[i].ToString());

            }

            return strSentence;
        }

Note: listLocationID[i] is supposed to display the location name in message box. Instead it displayed the index column names.

Here is snapshot of the selection for checkbox: [![enter image description here][1]][1]

Here is snapshot of the output i am getting: [![enter image description here][2]][2]

Appreciate any kind of help.

11
  • Remove .ToString(). Additional you should use a StringBuilder to build your return string. Commented Mar 9, 2018 at 9:53
  • listLocationID[i] is already a string - so wherever you get that from (which you don't show) is not giving you the string you want Commented Mar 9, 2018 at 9:53
  • don't think you are passing the right input Commented Mar 9, 2018 at 9:54
  • The issue comes from where the parameter listLocationID is populated from the calling method. Post the code from there Commented Mar 9, 2018 at 9:55
  • Ok see my updated code Commented Mar 9, 2018 at 9:56

2 Answers 2

3

Use

listLocationID.Add(dataGridLocationDetails.Rows[i].Cells["colLocationID"].Value.ToString());

instead of

listLocationID.Add(dataGridLocationDetails.Rows[i].Cells["colLocationID"].ToString());

To allow a neat format as mentioned in comment:

use StringBuilder instead of string

StringBuilder strSentence = new StringBuilder();
for (int i = 0; i < listLocationID.Count; i++)
{
     strSentence.Append(listLocationID[i]);
     strSentence.AppendLine();
}

return strSentence.ToString();
Sign up to request clarification or add additional context in comments.

2 Comments

One more thing. In the display message box, the list goes like : Location1Location2Location3 etc . I want it to display neatly like in a newline each location, how would i achieve that?
Use StringBuilder instead of string. Updated aswer
0

Loop looks good. you dont have to convert to string explicitly as it is string array.

public class Program
{
    public static void Main(string[] args)
    {
    List<string> listLocationID = new List<string>();
    listLocationID.Add("one");
    listLocationID.Add("two");
    listLocationID.Add("three");

     String strSentence = string.Empty;
     for (int i = 0; i < listLocationID.Count; i++)
        {
         strSentence = strSentence + (listLocationID[i]);
        }
        Console.Write(strSentence);
    }
}

you may try foreach as well

foreach (String locationID in listLocationID)
 {
  strSentence = strSentence + locationID ;
 }

Comments

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.