I want to use my DataGridView
only to show things, and I want the user not to be able to select any row, field or anything from the DataGridView
.
How can I do this?
This question is related to
c#
.net
winforms
datagridview
This worked for me like a charm:
row.DataGridView.Enabled = false;
row.DefaultCellStyle.BackColor = Color.LightGray;
row.DefaultCellStyle.ForeColor = Color.DarkGray;
(where row = DataGridView.NewRow(appropriate overloads);)
You may set a transparent background color for the selected cells as following:
DataGridView.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.Transparent;
Use the DataGridView.ReadOnly
property
The code in the MSDN example illustrates the use of this property in a DataGridView
control intended primarily for display. In this example, the visual appearance of the control is customized in several ways and the control is configured for limited interactivity.
Observe these settings in the sample code:
// Set property values appropriate for read-only
// display and limited interactivity
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AllowUserToOrderColumns = true;
dataGridView1.ReadOnly = true;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.MultiSelect = false;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dataGridView1.AllowUserToResizeColumns = false;
dataGridView1.ColumnHeadersHeightSizeMode =
DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dataGridView1.AllowUserToResizeRows = false;
dataGridView1.RowHeadersWidthSizeMode =
DataGridViewRowHeadersWidthSizeMode.DisableResizing;
I fixed this by setting the Enabled
property to false
.
I liked user4101525's answer best in theory but it doesn't actually work. Selection is not an overlay so you see whatever is under the control
Ramgy Borja's answer doesn't deal with the fact that default style is not actually a color at all so applying it doesn't help. This handles the default style and works if applying your own colors (which may be what edhubbell refers to as nasty results)
dgv.RowsDefaultCellStyle.SelectionBackColor = dgv.RowsDefaultCellStyle.BackColor.IsEmpty ? System.Drawing.Color.White : dgv.RowsDefaultCellStyle.BackColor;
dgv.RowsDefaultCellStyle.SelectionForeColor = dgv.RowsDefaultCellStyle.ForeColor.IsEmpty ? System.Drawing.Color.Black : dgv.RowsDefaultCellStyle.ForeColor;
I found setting all AllowUser...
properties to false
, ReadOnly
to true
, RowHeadersVisible
to false
, ScollBars
to None
, then faking the prevention of selection worked best for me. Not setting Enabled
to false
still allows the user to copy the data from the grid.
The following code also cleans up the look when you want a simple display grid (assuming rows are the same height):
int width = 0;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
width += dataGridView1.Columns[i].Width;
}
dataGridView1.Width = width;
dataGridView1.Height = dataGridView1.Rows[0].Height*(dataGridView1.Rows.Count+1);
you have to create a custom DataGridView
`
namespace System.Windows.Forms
{
class MyDataGridView : DataGridView
{
public bool PreventUserClick = false;
public MyDataGridView()
{
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (PreventUserClick) return;
base.OnMouseDown(e);
}
}
}
` note that you have to first compile the program once with the added class, before you can use the new control.
then go to The .Designer.cs and change the old DataGridView to the new one without having to mess up you previous code.
private System.Windows.Forms.DataGridView dgv; // found close to the bottom
…
private void InitializeComponent() {
...
this.dgv = new System.Windows.Forms.DataGridView();
...
}
to (respective)
private System.Windows.Forms.MyDataGridView dgv;
this.dgv = new System.Windows.Forms.MyDataGridView();
Enabled
property to false
or
this.dataGridView1.DefaultCellStyle.SelectionBackColor = this.dataGridView1.DefaultCellStyle.BackColor;
this.dataGridView1.DefaultCellStyle.SelectionForeColor = this.dataGridView1.DefaultCellStyle.ForeColor;
I'd go with this:
private void myDataGridView_SelectionChanged(Object sender, EventArgs e)
{
dgvSomeDataGridView.ClearSelection();
}
I don't agree with the broad assertion that no DataGridView
should be unselectable. Some UIs are built for tools or touchsreens, and allowing a selection misleads the user to think that selecting will actually get them somewhere.
Setting ReadOnly = true
on the control has no impact on whether a cell or row can be selected. And there are visual and functional downsides to setting Enabled = false
.
Another option is to set the control selected colors to be exactly what the non-selected colors are, but if you happen to be manipulating the back color of the cell, then this method yields some nasty results as well.
If you don't need to use the information in the selected cell then clearing selection works but if you need to still use the information in the selected cell you can do this to make it appear there is no selection and the back color will still be visible.
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
dataGridView.RowsDefaultCellStyle.SelectionBackColor = row.DefaultCellStyle.BackColor;
}
}
Source: Stackoverflow.com