【问题标题】:How to reformat all cells in DataBindingComplete method?如何重新格式化 DataBindingComplete 方法中的所有单元格?
【发布时间】:2017-08-21 20:16:13
【问题描述】:

我需要在方法中按名称迭代所有行/单元格并按条件替换值:DataBindingComplete

我试图这样:

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    string pass = dataGridView1.Rows[e.RowIndex].Cells["dataGridViewTextBoxColumn45"].Value.ToString();
               dataGridView1.Rows[e.RowIndex].Cells["dataGridViewTextBoxColumn45"].Value = (pass == "1") ? "Повторно" : "Первый раз";
}

但是没有e.RowIndex 属性。该怎么做?

不要建议我使用方法_CellFormatting。它对我不起作用,因为我有自定义列,可以通过这种方法呈现。

我也试过这种方式:

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) {
foreach (DataGridViewRow row in dataGridView1.Rows)
{
     intRow++;
     string pass = dataGridView1.Rows[intRow].Cells["dataGridViewTextBoxColumn45"].Value.ToString();

               dataGridView1.Rows[intRow].Cells["dataGridViewTextBoxColumn45"].Value = (pass == "1") ? "Повторно" : "Первый раз";
}
}

它返回一个错误:

System.StackOverflowException 发生 HResult=0x800703E9

【问题讨论】:

    标签: c# winforms c#-4.0 datagridview


    【解决方案1】:

    在 foreach 循环中不需要索引:

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.Cells[0] is DataGridViewTextBoxCell)
        {
             string pass = ((DataGridViewTextBoxCell)row.Cells[0]).Value;
             ((DataGridViewTextBoxCell)row.Cells[0]).Value = (pass == "1") ? "Повторно" : "Первый раз";
        }
    }
    

    【讨论】:

    • 它给了我一个错误:'Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewCheckBoxCell'.'
    • 我需要按名称检查并替换值。
    • 你没有改变答案
    • 我用DataGridViewTextBoxCell替换了DataGridViewCheckBoxCell,还是不行?
    • 如何检查额外的类型,如果文本然后replce?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多