Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi There,

Need help with looping on data grid row,

       col   col   col   col  col  col

row 1 - 1      2

row 2 -              3     4

row 3 -                         5    6


My code:

C#
private void iqcsavBtn_Click(object sender, EventArgs e)
        {
            
            string iqcData;
            string iqcData1 = string.Empty;
            iqcData1 = "";
          
            for (int i = 0; i < frmTabledgv.Rows.Count; i++)//loop to collect frmTabledgv cell value
             {
                 for (int j = 0; j < frmTabledgv.Columns.Count; j++)
                    {
                        //if (frmTabledgv.Rows[i].Cells[j].Value != null)
                        if (frmTabledgv[j, i].Value != null)

                        {
                           
                            iqcData = frmTabledgv.Rows[i].Cells[j].Value.ToString() + ",";
                            iqcData1 = iqcData1.ToString() + iqcData.ToString();

                            

                        }
                        
                    }
                    iqcData1 = iqcData1.TrimEnd(',');
                    StringBuilder s1 = new StringBuilder();
                    SqlConnection conn = new SqlConnection(Program.p_connectionString);
                    conn.Open();
                    s1.Length = 0;
                    s1.Append("INSERT INTO UT_SMT_BCP_IQC(MainBCP,IQCDATA ) ");
                    s1.Append(" VALUES('{0}','{1}')");
                    string sql_insert = string.Format(s1.ToString(), Program.bcpnmbrtxtbx1, iqcData1);
                    IMIS_WyfUtility.SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql_insert);
                    conn.Close();


Current Output :

1,2
1,23,4
1,23,45,6
1,23,45,6


i want to achieved this Output :
1,2
3,4
5,6
Posted
Updated 22-Jul-14 19:44pm
v2
Comments
Maciej Los 23-Jul-14 1:50am    
Why to read anything from GridView? If the data comes from SQL database, you can use ranking functions to achieve the same.

1 solution

Please try as following

C#
iqcData1=string.Empty
for (int i = 0; i < frmTabledgv.Rows.Count; i++)//loop to collect frmTabledgv cell value
{
for (int j = 0; j < frmTabledgv.Columns.Count; j++)
{

if (frmTabledgv[j, i].Value != null)

{


iqcData1 = iqcData1.ToString() + frmTabledgv.Rows[i].Cells[j].Value.ToString() + ",";



}
iqcData1 = iqcData1.ToString() + "\n";
}
 
Share this answer
 
Comments
Maciej Los 23-Jul-14 1:49am    
Please, see the question again... Now, it's well formated.
Manoj Kumar Choubey 23-Jul-14 3:06am    
ok
Manoj Kumar Choubey 23-Jul-14 3:16am    
iqcData==string.Empty;

iqcData1=string.Empty
for (int i = 0; i < frmTabledgv.Rows.Count; i++)//loop to collect frmTabledgv cell value
{
for (int j = 0; j < frmTabledgv.Columns.Count; j++)
{

if (frmTabledgv[j, i].Value != null)

{


iqcData1 = iqcData1.ToString() + frmTabledgv.Rows[i].Cells[j].Value.ToString() + ",";

iqcData=iqcData+ " ";

}
iqcData1 = iqcData1.ToString() + "\n" + iqcData;

}

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