Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,

I want to get the values from a for loop separated with comma in to a single message box.
Below is my for loop.

C#
for (int i = 0; i < dtgrdUnconfirm.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
                    ch1 = (DataGridViewCheckBoxCell)dtgrdUnconfirm.Rows[i].Cells[0];
                    if (ch1.Value != null &&
                           Convert.ToBoolean(ch1.Value) == true)
                    {
                        data = (dtgrdUnconfirm.Rows[i].Cells[6].Value.ToString());
                    }
                }



In this for loop, string data will get 4 values and I want those 4 values to be shown in a single message box separated with comma. For example, output should be 1,2,3,4



Thanks in advance
Posted
Comments
Suresh Suthar 24-Sep-13 3:05am    
You can concatenate string using data += string.Format("{0},", dtgrdUnconfirm.Rows[i].Cells[6].Value.ToString()); and finally trim the last comma.
You should use StringBuilder.

1 solution

Hi,
I just modified your code
C#
for (int i = 0; i < dtgrdUnconfirm.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
                    ch1 = (DataGridViewCheckBoxCell)dtgrdUnconfirm.Rows[i].Cells[0];
                    if (ch1.Value != null &&
                           Convert.ToBoolean(ch1.Value) == true)
                    {
                        data += (dtgrdUnconfirm.Rows[i].Cells[6].Value.ToString())+",";
                    }
                }
             MessageBox.Show(data);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900