我如何手动更改datagridview列的值并更新该列 [英] How can i manually change datagridview column values and update that column

查看:194
本文介绍了我如何手动更改datagridview列的值并更新该列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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";
     }
 }


如何手动检索DataGridView列值并更改这些值,然后重新填充该列?我当然可以检索列值,但是我的问题是在重新填充或更新该列时出现错误:


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

推荐答案

好吧,除了建议以下与您的代码完全相同的代码外,只是出于偏爱,我只能以某种方式认为您的DataGridViewDataSource是只读的.

我的代码版本.
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.
 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.

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;
        }
    }
}


我只花了10分钟时间就您最近的问题输入答案,并解释说虽然我没有答案,但我还是遇到了一些可能的解决方法.然后,我在您的代码中发现了问题. :laugh:

3件事.

1. e.FormattingApplied = true;语句仅应在确定格式有效或至少会发生时才使用.您的代码不会检查右列是否正在处理.

2.最后一行(您在其中分配值)引起错误,因为它试图将波斯语日期放入数据库中.那是行不通的,因此是错误.它应该将值分配给e.Value.

3.无需将代码放在< code> for循环</code>中.无论何时显示数据,格式化都会应用到DataGridview中的每一行.

假设代码与之前的DataGridview相同,则它应如下所示:

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:

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. :)


这篇关于我如何手动更改datagridview列的值并更新该列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆