0

In my datagridview, I have got some columns are read only, so i have given in some color. When I load the the DGV in advance view with all columns including read only. When the user cliks on Normal View I want to just display the normal eitable cols and hide the read only columns and the color.

I'm on telerik and using radgrid.

I tried the below approach but it's not working. Is there any better way of doing this?? Thank you.

private void normalToolStripMenuItem_Click(object sender, EventArgs e)//Normal View
{
    DTURradGridView.Columns[2].IsVisible = false;
    DTURradGridView.Columns[3].IsVisible = false;
    DTURradGridView.Columns[4].IsVisible = false;
    DTURradGridView.Columns[5].IsVisible = false;
    DTURradGridView.Columns[7].IsVisible = false;
    DTURradGridView.Columns[11].IsVisible = false;

    DTURradGridView.Columns[2].ReadOnly = false;
    DTURradGridView.Columns[3].ReadOnly = false;
    DTURradGridView.Columns[4].ReadOnly = false;
    DTURradGridView.Columns[5].ReadOnly = false;
    DTURradGridView.Columns[7].ReadOnly = false;
    DTURradGridView.Columns[11].ReadOnly = false;

    DTURradGridView.CellFormatting -= DTURradGridView_CellFormatting;
}

private void advancedToolStripMenuItem_Click(object sender, EventArgs e) //Advanced View
{
    DTURradGridView.Columns[2].IsVisible = true;
    DTURradGridView.Columns[3].IsVisible = true;
    DTURradGridView.Columns[4].IsVisible = true;
    DTURradGridView.Columns[5].IsVisible = true;
    DTURradGridView.Columns[7].IsVisible = true;
    DTURradGridView.Columns[11].IsVisible = true;

    DTURradGridView.Columns[2].ReadOnly = true;
    DTURradGridView.Columns[3].ReadOnly = true;
    DTURradGridView.Columns[4].ReadOnly = true;
    DTURradGridView.Columns[5].ReadOnly = true;
    DTURradGridView.Columns[7].ReadOnly = true;
    DTURradGridView.Columns[11].ReadOnly = true;

    DTURradGridView.CellFormatting += DTURradGridView_CellFormatting;   
}

private void DTURradGridView_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement.ColumnIndex != 2 && e.CellElement.ColumnIndex != 3 && e.CellElement.ColumnIndex != 4
    && e.CellElement.ColumnIndex != 5 && e.CellElement.ColumnIndex != 7 && e.CellElement.ColumnIndex != 11) return;

    e.CellElement.DrawFill = true;
    e.CellElement.NumberOfColors = 1;
    e.CellElement.BackColor = Color.LightSlateGray;
    e.CellElement.GradientStyle = GradientStyles.Linear;    
}

When I click on the normal view, I see the colors of the col index 2,3,4,5. So the -= is not working.

4

1 回答 1

0

更好的方法是在表单启动时设置只读列及其颜色,最好是按名称。请参阅如何更改 datagridview 中列的颜色?

然后遍历列并隐藏或显示那些只读的,具体取决于您所处的模式。像这样硬编码不是一个好习惯:

DTURradGridView.Columns[2].ReadOnly = true;
DTURradGridView.Columns[3].ReadOnly = true;
DTURradGridView.Columns[4].ReadOnly = true;

谁知道2、3、4是什么?没有人。问题是您很容易迷失在自己的代码中。

于 2012-10-26T18:10:33.473 回答