当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Winform DataBinding

问题描述:

I need to enable/disable a row columns of a datagridview this can be easily done by looping all the rows after binding it.

But i want to do this while the data is binding... is there a way to do that? also how do i enable/disable a row cell?

dgvLayout.AutoGenerateColumns = false;

dgvLayout.DataSource = list;

in cell click but it does not work

if ((dgvLayout.Rows[e.RowIndex].Cells["colControlText"].Value.ToString()) == "-Invalid-")

{

if (e.ColumnIndex == 2 || e.ColumnIndex == 5)

{

return;

}

else if (e.ColumnIndex == 1)

{

return;

}

}

网友答案:

you can use this solution for enabling and disabling cell

To "disable" a cell, it must be read-only and grayed out somehow. This function enables/disables a DataGridViewCell:

    /// <summary>
    /// Toggles the "enabled" status of a cell in a DataGridView. There is no native
    /// support for disabling a cell, hence the need for this method. The disabled state
    /// means that the cell is read-only and grayed out.
    /// </summary>
    /// <param name="dc">Cell to enable/disable</param>
    /// <param name="enabled">Whether the cell is enabled or disabled</param>
    private void enableCell(DataGridViewCell dc, bool enabled) {
        //toggle read-only state
        dc.ReadOnly = !enabled;
        if (enabled)
        {
            //restore cell style to the default value
            dc.Style.BackColor = dc.OwningColumn.DefaultCellStyle.BackColor;
            dc.Style.ForeColor = dc.OwningColumn.DefaultCellStyle.ForeColor;
        }
        else { 
            //gray out the cell
            dc.Style.BackColor = Color.LightGray;
            dc.Style.ForeColor = Color.DarkGray;
        }
    }
网友答案:

you can write you code on RowsAdded event of datagrid

分享给朋友:
您可能感兴趣的文章:
随机阅读: