Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
C#
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
  {
     string temp = dataGridView1.Rows[i].Cells[6].Value.ToString();
     if ( temp== "0")
     {
         dataGridView1.Rows[i].Cells[6].Value = "Cashed";
     }
     else if (temp == "1")
     {
         dataGridView1.Rows[i].Cells[6].Value = "Cancelled";
     }
     else if (temp == "2")
     {
         dataGridView1.Rows[i].Cells[6].Value = "Not Cashed";
     }
     else
     {
        dataGridView1.Rows[i].Cells[6].Value = "Returned";
     }
 }

How can I manually retrieve a DataGridView column values and change these values then refill that column? Of course I could to retrieve column values, but my problem is in refill or update that column and I received errors:
The column 'Status' is read only.<br />
but ichecked it,it was not readonly
Posted
Updated 18-Dec-10 4:12am
v10
Comments
Sandeep Mewara 17-Dec-10 16:37pm    
Looks like you missed posting errors.
Henry Minute 17-Dec-10 17:04pm    
Please show the 'partial code' you mention and also fully explain the errors that you get.
[no name] 17-Dec-10 23:47pm    
Toli Cuturicu: Stop the idiotic and asinine editing. It was completely unnecessary to color code the word DataGridView

Well, apart from suggesting the following code, which does exactly the same as yours, just a matter of preference, I can only think that somehow the DataSource for your DataGridView is readonly.

My version of your code.
C#
 for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
 {
    string temp = dataGridView1.Rows[i].Cells[6].Value.ToString();
    switch (temp)
    {
        case "0":
           dataGridView1.Rows[i].Cells[6].Value = "Cashed"
           break;
        case "1":
            dataGridView1.Rows[i].Cells[6].Value = "Cancelled"
            break;
        case "2":
            dataGridView1.Rows[i].Cells[6].Value = "Not Cashed"
            break;
        default:
            dataGridView1.Rows[i].Cells[6].Value = "Returned"
            break;
    }
}


Alternatively, rather than iterate over all the rows you might consider handling this in the OnCellFormatting[^] event handler which was designed to do this.

C#
private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 6)
    {
        e.FormattingApplied = true; // <===VERY, VERY important tell it you've taken care of it.
        string temp = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        switch (temp)
        {
            case "0":
                e.Value = "Cashed";
                break;
            case 1:
                e.Value = "Cancelled";
                break;
            case 2:
                e.Value = "Not Cashed";
                break;
            default:
                e.Value = "Returned";
                break;
        }
    }
}
 
Share this answer
 
I just spent 10 minutes typing out an answer to your latest question, explaining that while I didn't have an answer, several possible workrounds occurred to me. Then I spotted the problem in your code. :laugh:

3 things.

1. The e.FormattingApplied = true; statement should only be used when you are sure that your formatting has worked, or at least will happen. Your code does not check that the right column is being processed.

2. The last line, where you assign the value is causing the error because it is trying to put the Persian date into the database. That won't work, hence the errors. it should assign the value to e.Value.

3. There is no need to put the code in a <code>for loop</code> the formatting gets applied for every row in the DataGridview anyway when the time comes for the data to be displayed.

Assuming that the code is for the same DataGridview as before it should look something like this:

C#
private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 5)
    {
        e.FormattingApplied = true;
        DateTime dtTemp =Convert.ToDateTime( dataGridView1.Rows[i].Cells[5].Value.ToString());
        e.Value= DateUtility.Miladi2Shamsi(dtTemp);
    }
    else if (e.ColumnIndex == 6)
    {
        e.FormattingApplied = true; // <===VERY, VERY important tell it you've taken care of it.
        string temp = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        switch (temp)
        {
            case "0":
                e.Value = "Cashed";
                break;
            case 1:
                e.Value = "Cancelled";
                break;
            case 2:
                e.Value = "Not Cashed";
                break;
            default:
                e.Value = "Returned";
                break;
        }
    }
}


One last thing. I only noticed that you had another problem by accident. To attract the attention of someone who has answered one of your questions, you should do so by adding a comment to their answer (look at the bottom right of this answer and you will see the Add Comment link in blue) just click that and type your comment. That way, the person will get an Email and will know you need more help. :)
 
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