2012-01-03 62 views
22

Có cách nào dễ dàng để vô hiệu/xám ra một DataGridView không? Ví dụ: khi thực hiệnTắt hoặc chuyển sang màu xám một DataGridView

dgv.Enabled = false 

Sự xuất hiện của dgv không thay đổi. Tôi đã thấy những người phụ thêm như sau:

dgv.forecolor = gray 
dgv.columnheader.forecolor = gray 

Tuy nhiên, điều này có vẻ vụng về. Có cách nào tốt hơn?

Trả lời

14

Câu trả lời đơn giản cho câu hỏi của bạn: không, không có cách nào tốt hơn.

MSDN chủ yếu là im lặng về chủ đề này nhưng diễn đàn thì không rõ. Đặt màu nền thành màu xám theo cách thủ công là cách hầu hết mọi người có giao diện "bị tắt" trên DGV.

+6

Vui lòng tôn trọng màu hệ thống và sử dụng 'SystemColors.Control' không phải là màu xám cứng! – MarkJ

+4

mọi người chấp nhận màu xám nhạt khi bị tắt. đó là phổ quát. – Timmerz

0

thiết lập ReadOnly = false có thay đổi giao diện không? Tôi nghĩ rằng có thể có màu xám ra các phần 'clickable' của datagraid như tiêu đề cột .. nhưng bạn vẫn có thể nhìn thấy dữ liệu trong đó.

+0

Điểm tốt - tuy nhiên, không, ngoại hình vẫn giữ nguyên. Điều này chỉ ngăn cản các chỉnh sửa tế bào afaik – Jeb

0

Tôi giả sử bạn muốn xem dữ liệu để hiển thị thông tin cho người dùng và từ chối người dùng khả năng sửa đổi theo bất kỳ cách nào.

private void IntializeDataGridView() 
    { 
    dataGridViewTest.ReadOnly = true; 
    // you can code permissions or colors as well 
    dataGridViewTest.AllowUserToAddRows = false; 
    dataGridViewTest.AllowUserToDeleteRows = false; 
    dataGridViewTest.AllowUserToOrderColumns = false; 
    dataGridViewTest.BackgroundColor = Color.LightGray; 
    //so on and so forth 
    } 

Hy vọng điều này sẽ hữu ích. :]

+0

Cảm ơn, tôi quan tâm hơn đến khía cạnh trực quan của điều khiển khi nó bị vô hiệu hóa cho một tín hiệu thị giác; hành vi tương tự khi vô hiệu hóa nút, v.v. – Jeb

+0

Bạn đang nói về việc thay đổi màu của các cột và các nút của ô? Bạn có thể cụ thể hơn một chút không? Tôi không nghĩ rằng tôi khá hiểu biết. – javasocute

+0

Thực ra, tôi nghĩ tôi biết. Nhưng thật không may, tôi không nghĩ rằng trong C# có một cách dễ dàng hơn. Những gì bạn sẽ phải làm là gọi một chức năng javascript mà sẽ màu xám nó ra cho bạn. – javasocute

3

Chỉ cần đặt màu xám cho tiêu đề sẽ không thay đổi màu. Bạn cũng cần phải chuyển EnableHeadersVisualStyles thành false.

dgv.ForeColor = Color.Gray; 
dgv.ColumnHeadersDefaultCellStyle.ForeColor = Color.Gray; 
dgv.EnableHeadersVisualStyles = false; 
+1

Nếu bạn vẫn muốn xem văn bản tiêu đề, nhưng có nó "nhìn" vô hiệu hóa, sử dụng 'dgv.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.ControlDark;'. –

15
Private Sub DataGridView1_EnabledChanged(sender As Object, e As EventArgs) Handles DataGridView1.EnabledChanged 
    If Not DataGridView1.Enabled Then 
     DataGridView1.DefaultCellStyle.BackColor = SystemColors.Control 
     DataGridView1.DefaultCellStyle.ForeColor = SystemColors.GrayText 
     DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control 
     DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.GrayText 
     DataGridView1.CurrentCell = Nothing 
     DataGridView1.ReadOnly = True 
     DataGridView1.EnableHeadersVisualStyles = False 
    Else 
     DataGridView1.DefaultCellStyle.BackColor = SystemColors.Window 
     DataGridView1.DefaultCellStyle.ForeColor = SystemColors.ControlText 
     DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Window 
     DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.ControlText 
     DataGridView1.ReadOnly = False 
     DataGridView1.EnableHeadersVisualStyles = True 
    End If 
End Sub 
+1

Tôi cũng phải thêm DefaultCellStyle.SelectionBackColor vào danh sách các thứ được đặt thành Màu điều khiển hoặc ô đầu tiên trong lưới sẽ hiển thị với nền trắng. –

1

Tôi hiểu đây là một giải quyết một mà muốn ngăn chặn sự mất mát của 1 giờ cho một ai khác.

//C# version for buttons also. Inspired by sveilleux2. 
private void DataGridView1_EnabledChanged(object sender, EventArgs e){ 
if (!DataGridView1.Enabled){ 
    DataGridView1.DefaultCellStyle.BackColor = SystemColors.Control; 
    DataGridView1.DefaultCellStyle.ForeColor = SystemColors.GrayText; 
    DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control; 
    DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.GrayText; 
    //Disable two colums of buttons 
    for (int i = 0; i < DataGridView1.RowCount; i++){ 
     DataGridViewButtonCell buttonCell = (DataGridViewButtonCell)DataGridView1.Rows[i].Cells[1]; 
     buttonCell.FlatStyle = FlatStyle.Popup; 
     buttonCell.Style.ForeColor = SystemColors.GrayText; 
     buttonCell.Style.BackColor = SystemColors.Control; 
     DataGridViewButtonCell buttonCell_2 = (DataGridViewButtonCell)DataGridView1.Rows[i].Cells[6]; 
     buttonCell_2.FlatStyle = FlatStyle.Popup; 
     buttonCell_2.Style.ForeColor = SystemColors.GrayText; 
     buttonCell_2.Style.BackColor = SystemColors.Control; 
    } 

    DataGridView1.Columns[1].DefaultCellStyle.ForeColor = SystemColors.GrayText; 
    DataGridView1.Columns[1].DefaultCellStyle.BackColor = SystemColors.Control; 
    DataGridView1.ReadOnly = true; 
    DataGridView1.EnableHeadersVisualStyles = false; 
    DataGridView1.CurrentCell = null; 
}else{ 
    DataGridView1.DefaultCellStyle.BackColor = SystemColors.Window; 
    DataGridView1.DefaultCellStyle.ForeColor = SystemColors.ControlText; 
    DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Window; 
    DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.ControlText; 
    DataGridView1.ReadOnly = false; 
    DataGridView1.EnableHeadersVisualStyles = false; 

    //Enable two colums of buttons 
    for (int i = 0; i < DataGridView1.RowCount; i++){ 
     DataGridViewButtonCell buttonCell = (DataGridViewButtonCell)DataGridView1.Rows[i].Cells[1]; 
     buttonCell.FlatStyle = FlatStyle.Standard; 
     DataGridViewButtonCell buttonCell_2 = (DataGridViewButtonCell)DataGridView1.Rows[i].Cells[6]; 
     buttonCell_2.FlatStyle = FlatStyle.Standard; 
    } 
} 

} Ví dụ

1

sveilleux2, chỉ trong C# (đó là thẻ) và cao cấp (cho phép bạn đặt nó vào tên bất kỳ và trên bất kỳ số lượng DataGridViews)

private void DataGridView_EnabledChanged(object sender, EventArgs e) 
    { 
     DataGridView dgv = sender as DataGridView; 
     if (!dgv.Enabled) { 
      dgv.DefaultCellStyle.BackColor = SystemColors.Control; 
      dgv.DefaultCellStyle.ForeColor = SystemColors.GrayText; 
      dgv.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control; 
      dgv.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.GrayText; 
      dgv.CurrentCell = null; 
      dgv.ReadOnly = true; 
      dgv.EnableHeadersVisualStyles = false; 
     } 
     else { 
      dgv.DefaultCellStyle.BackColor = SystemColors.Window; 
      dgv.DefaultCellStyle.ForeColor = SystemColors.ControlText; 
      dgv.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Window; 
      dgv.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.ControlText; 
      dgv.ReadOnly = false; 
      dgv.EnableHeadersVisualStyles = true; 
     } 
    } 
0

tôi sẽ thêm điều này ở đây mặc dù câu hỏi là một chút cũ - tôi đã làm nó khác với những người khác bằng cách ghi đè phương pháp Paint trên điều khiển để vẽ một hộp trong suốt. Tôi đã sử dụng một lớp được kế thừa từ cơ sở DataGridView và sau đó cung cấp một số thuộc tính bổ sung và ghi đè cho phương thức OnPaint. Bạn cũng có thể làm điều này trong sự kiện Paint, nhưng đối với tôi, tôi đã có phiên bản kiểm soát riêng của mình.

Điều này có lợi ích là không thay đổi bất kỳ màu/định dạng hàng/ô nào bạn đã thiết lập và chỉ muốn làm mờ điều khiển khi bị tắt.

Chỉ cần đặt DisableColor (thành Đen chẳng hạn) để làm cho nó mờ đi (bạn cũng có thể thay đổi kênh alpha bằng thuộc tính DisableColorAlpha). Nếu không nó hoạt động như mọi khi.

/// <summary> 
/// Color used when the grid is disabled 
/// </summary> 
[Category("Appearance"), DefaultValue(typeof(Color), "Transparent"), Description("Color to use when the control is disabled (should be transparent)")] 
public Color DisableColor { get; set; } 

/// <summary> 
/// Color used when the grid is disabled 
/// </summary> 
[Category("Appearance"), DefaultValue(50), Description("Alpha channel value for disabled color (0-255)")] 
public int DisableColorAlpha { get; set; } 

protected override void OnPaint(PaintEventArgs e) 
{ 
    base.OnPaint(e); 

    if (this.Enabled == false && DisableColor != Color.Transparent) 
    { 
     // paint a transparent box -- simulate disable 
     using (Brush b = new SolidBrush(Color.FromArgb(DisableColorAlpha, DisableColor))) 
     { 
      e.Graphics.FillRectangle(b, e.ClipRectangle); 
     } 
    } 
} 
Các vấn đề liên quan