Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a file I import into a grid, I want to copy the data from A column if it meets the Cartier to another column. example: if column D has "Yeshh" copy column A into column C. I have part of it working, the last 2 line I have trouble if it meets a different cartier return what is in that column.
the below coded show this in the cell DataGridViewTextBoxCell { ColumnIndex=13, RowIndex=0 } instead of the results.



C#
<pre>private void btn_Adjust_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow rows in dataGridView1.Rows)
            {
                for (int i = 0; i < dataGridView1.RowCount - 1; i++)
                {
                    if (dataGridView1.Rows[i].Cells[24].Value.ToString() == "Yesmix")
                    {
                        dataGridView1.Rows[i].Cells[17].Value = "48";
                    }

                    else
                    {
                        if (dataGridView1.Rows[i].Cells[24].Value.ToString() == "Yeshh")     // this not working
                        {
                           dataGridView1.Rows[i].Cells[17].Value = dataGridView1.Rows[i].Cells[13];      // this not working
                        }
                    }
                }
            }
        }


What I have tried:

DataGridViewTextBoxCell { ColumnIndex=13, RowIndex=0 }
Posted
Updated 9-Nov-18 7:03am
Comments
Richard MacCutchan 9-Nov-18 4:39am    
What does "not working" mean? Have you used your debugger to see exactly what happens at that point?
Member 12349103 9-Nov-18 11:14am    
This is returned in the cell instead of the data from the cell. DataGridViewTextBoxCell { ColumnIndex=13, RowIndex=0 }
Richard MacCutchan 9-Nov-18 11:27am    
You nee to use the Value property, and also (possibly) call ToString() on that. Just as you have done in the lines above.

1 solution

I have found a solution.





<pre>private void btn_Adjust_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow rows in dataGridView1.Rows)
            {
                for (int i = 0; i < dataGridView1.RowCount ; i++)
                {
                    if (dataGridView1.Rows[i].Cells[24].Value.ToString() == "Yesmix")
                    {
                        dataGridView1.Rows[i].Cells[17].Value = "48";
                        dataGridView1.Rows[i].Cells[16].Value = "45";
                    }

                    else
                    {
                           if (dataGridView1.Rows[i].Cells[24].Value.ToString() == "Yeshh")
                        {
                            var value1 = dataGridView1.Rows[i].Cells[13].Value.ToString();
                            dataGridView1.Rows[i].Cells[17].Value = value1;

                            var value2 = dataGridView1.Rows[i].Cells[12].Value.ToString();
                            dataGridView1.Rows[i].Cells[16].Value = value2;

                            var value3 = dataGridView1.Rows[i].Cells[14].Value.ToString();
                            dataGridView1.Rows[i].Cells[18].Value = value3;


                        }
 
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