使用自定义对象的动态列表,不能动态更改dataGrid的单元格属性 [英] Using a dynamic list of Custom Object and can't dynamically change dataGrid's cells properties

查看:174
本文介绍了使用自定义对象的动态列表,不能动态更改dataGrid的单元格属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来张贴在堆栈。我搜索了相当长的一段时间我相似的问题。我试图从非只读到只读基于对象的布尔值动态地在一个WinForms DataGridView的改变复选框。

I am new to posting on Stack. I have searched for quite some time to a problem similar to mine. I am trying to change the checkboxes in a WinForms DataGridView from not read-only to read-only based on the object's boolean value dynamically.

据显示,该变化已经发生调试模式,但一旦它完全贯穿,要读取,都应该复选框细胞唯一仍允许检查并取消功能。我已经离开一节中的注释,以表明我试图做到这一点。

It is showing in debug mode that the change has happened but once it fully runs through, the checkbox cells that are supposed to be read only are still allowing check and uncheck functionality. I have left the commented out section in to show that I have attempted to do this.

m_SingletonForm.dataGridView1.DataSource = list;
m_SingletonForm.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
m_SingletonForm.dataGridView1.Columns["StoreGroup"].ReadOnly = true;
m_SingletonForm.dataGridView1.Columns["Message"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
m_SingletonForm.dataGridView1[0, 0].ReadOnly = true;


foreach (DataGridViewRow row in m_SingletonForm.dataGridView1.Rows)
{
    //var isChecked = Convert.ToBoolean(row.Cells["SendFile"].Value);

    //if (!isChecked)
    //{
        //m_SingletonForm.dataGridView1.Rows[0].Cells["SendFile"].Style.BackColor = Color.Red;
        //m_SingletonForm.dataGridView1.Rows[0].Cells["SendFile"].ReadOnly = true;

        //m_SingletonForm.dataGridView1.Rows[row.Index].Cells["SendFile"].Style.BackColor = Color.Red;
        //m_SingletonForm.dataGridView1.Rows[row.Index].Cells["SendFile"].ReadOnly = true;
        //m_SingletonForm.dataGridView1["SendFile", row.Index].ReadOnly = true;
        //m_SingletonForm.dataGridView1["SendFile", row.Index].Style.BackColor = Color.Red;          
    // }
}

m_SingletonForm.label1.Text = message;
m_SingletonForm.Text = title;
MessageBox.Show(m_SingletonForm.dataGridView1[0, 0].ReadOnly.ToString());
m_SingletonForm.ShowDialog();



任何帮助将不胜感激。

Any help would be greatly appreciated.

推荐答案

这行 m_SingletonForm.ShowDialog(); 看来你有 DataGridView的<在此之前的代码/ code>已经显示 * 。这对于这样的改变,以要施加的栅极的物品过早。你也看到了同样的问题,如果你的代码是构造表单中。

From the line m_SingletonForm.ShowDialog(); it appears that you have this code before the DataGridView has been displayed *. This is too early for such changes to the grid items to be applied. You would also see the same issue if your code was inside the constructor for your form.

有关问题的最简单的解决方法是把你设置的单元为只读代码一个 DataBindingComplete 事件处理程序中。事情是这样的:

The simplest fix for the issue is to put you code for setting the cells to readonly within a DataBindingComplete event handler. Something like this:

// Attach the event
m_SingletonForm.dataGridView1.DataBindingComplete += new
    DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);


// And the code for the handler
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow row in m_SingletonForm.dataGridView1.Rows)
    {
        var isChecked = Convert.ToBoolean(row.Cells["SendFile"].Value);

        if (!isChecked)
        {
            m_SingletonForm.dataGridView1.Rows[0].Cells["SendFile"].Style.BackColor = Color.Red;
            m_SingletonForm.dataGridView1.Rows[0].Cells["SendFile"].ReadOnly = true;

            m_SingletonForm.dataGridView1.Rows[row.Index].Cells["SendFile"].Style.BackColor = Color.Red;
            m_SingletonForm.dataGridView1.Rows[row.Index].Cells["SendFile"].ReadOnly = true;
            m_SingletonForm.dataGridView1["SendFile", row.Index].ReadOnly = true;
            m_SingletonForm.dataGridView1["SendFile", row.Index].Style.BackColor = Color.Red;
        }
    }            
}






* 我从来没有100%的努力,为什么是这样 - 我相信,有在的DataGridView两组细胞它与事实 - 编辑/ UI细胞和他们坐在数据。


* I have never 100% worked out why it is this way - I believe it is related to the fact that there are two sets of cells in the DataGridView - the editing/ui cells and the data they sit upon.

这篇关于使用自定义对象的动态列表,不能动态更改dataGrid的单元格属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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