I am not able to scroll horizontally in datagridview which throws the error "FirstDisplayedScrollingColumnIndex property cannot be set to an invisible column." I am binding a datatable to the datagridview and have set few columns invisible. Not sure what causes the issue.The vertical scroll is working without any issue. I have not added the scroll bars programatically and the datgridview is set inside a panel with dock property set to fill. Any specific reason for the issue,please state it down
if (hashcharges.ContainsKey("1"))
{
if (dataGridViewSummary1.Columns.Contains("Charge1"))
{
dataGridViewSummary1.Columns["Charge1"].HeaderText = hashcharges["1"].ToString().ToLower();
}
}
else
{
dataGridViewSummary1.Columns["Charge1"].Visible = false;
}
This is how i set the column invisble after binding datatable with datagridview.The error is not thrown in the same form instead it is shown in program.cs file.
You can set DataGridView.FirstDisplayedScrollingColumnIndex property to the index of the column that is the first column displayed. In your case definitely not the first one, because it is hidden.
Related
This Grid column property i can set from the designer, but not from the C # code !!! Why? Header.BackColor is working fine. I can also set any other color for the GridView elements except this.
If you are doing this at runtime you need to set the GridColumn.AppearanceHeader.Options.UserForeColor property to true as well as set the actual color. When you do this via the designer, the property is automatically set to true.
gridView1.Columns["FieldName"].AppearanceHeader.ForeColor = Color.Red;
gridView1.Columns["FieldName"].AppearanceHeader.Options.UseForeColor = true;
Ref: How do I set the color of a grid column header panel?
The header's BackColor property is ignored in the Skin paint style.
Add the following line to your code to address this issue:
gridControl1.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Flat;
gridControl1.LookAndFeel.UseDefaultLookAndFeel = false; // <<<<<<<<
gridView1.Appearance.HeaderPanel.Options.UseForeColor = true;
gridView1.Appearance.HeaderPanel.ForeColor = System.Drawing.Color.Red;
Reference these for detailed information:
Gridview - How to change a column's header background color
Why Appearance settings of DevExpress controls are not taken into account?
Devexpress column header color (each column different color), winform c#
The DevExpress skinning mechanism overrides the Appearance object settings. You have a couple options if you need a different background color for the column header:
Disable skinning on the GridControl by setting the GridControl.LookAndFeel.UseDefaultLookAndFeel property to False and the GridControl.LookAndFeel.Style property to "Flat".
Handle the GridView's CustomDrawColumnHeader and fill the column header with a different color.
DataGridView is not showing the horizontal scroll bar. I tried all the following
set the Scroll bar:both
Frozen property of all cells to false
AutoSizeColumnMode:AllCells
Try to set the AutoSizeColumnMode to None
Edited as per OP's request
If problem persist then,
Delete that DataGridView and drag-drop new DataGridView and give the same name of the old DataGridView and check.
I have a Form, that contain a DataGridView control named gridParameters.
I'm adding control on-top of it, based on some values, to add controls that are not available (for example, DateTimePicker):
DateTimePicker dtp = new DateTimePicker();
gridParameters.Controls.Add(dtp);
dtp.Location = rowCellLocation;
dtp.Size = rowCellSize;
dtp.CustomFormat = "yyyy/MM/dd";
dtp.Format = DateTimePickerFormat.Custom;
dtp.Tag = rowId;
dtp.ValueChanged += dtp_SelectedValueChanged;
dtp.Visible = true;
That code is inside a loop that iterate throgh the grid rows, and is in an If statement (I add DateTimePicker for some rows and NumericUpDown for others based on a row cell value). The variables rowCellLocation, rowCellSize and rowId are set in a loop for each row.
My DataGridView selection mode is a full row selection.
I want to make the added control (DateTimePicker, NumericUpDown...) get focus when I switch to the row, for example, when I click on a row with the mouse.
To do so, I've tried the following:
In the loop that creates the controls, I set the control to the relevant row Tag property.
In the grid Row_Enter event (I also tried CurrentCellChanged) I added the following code:
if (gridParameters.CurrentRow.Tag != null)
{
((Control)gridParameters.CurrentRow.Tag).Focus();
}
I've also tried using Select() and also
this.ActiveControl = (Control)gridParameters.CurrentRow.Tag;
None of the above sets focus to the control.
I place a break point, and the code is getting to this line, the Tag property contain the control that need to be focused, and execute it, but I can't get the control to focus.
UPDATE: Setting focus to another control outside of the DataGridView, works just fine (for example, setting focus to an "OK" button works).
I use GridView in my C# application, and the cell seems too small.
I find the prop of GridView, but I find nothing about it. How can I display all text in Cell?
Under Columns Collection property there is AutoSizeMode under Layout. And use for example DisplayedCells and see for yourself.
For Word Wrap you could set it programmatically like:
// Columns[1] for 2nd column based on your example.
dataGridView.Columns[1].DefaultCellStyle.WrapMode = DataGridViewTriState.true;
But don't set the AutoSizeMode as above or else the columns will still expand.
You could set it also manually under Columns Collection property and under Appearance select Default Cell Style and then set WrapMode to true.
I am binding a DataTable to GridView. It does not adjust to height and width of the DataTable. How can I the strech the width of the grid that i shows all the columns and height shrink if the rows are few.
just go to the properties of your datagrid =>
and then at the section Layout=>
AutoSizeColumnsMode set it to Fill...
To autosize the columns to fit the data (width-wise) and then autosize the form to fit the gridview (width-wise), use the following code:
foreach (DataGridViewColumn column in dataGridView1.Columns)
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
this.Width = dataGridView1.Width + 100;
where dataGridView1 is the name of the Datagridview in this example, and this is refering to the instance of the form. The 100 is a constant for how many pixels wider you want the form than the datagridview. (note: you may want some width checking to make sure you form and data gridview aren't wider than the user's screen)
To autosize the datagridview to fit the rows (height-wise) and then autosize the form to fit the gridview (height-wise), use the following code:
int totalRowHeight = dataGridView1.ColumnHeadersHeight;
foreach (DataGridViewRow row in dataGridView1.Rows)
totalRowHeight += row.Height;
dataGridView1.Height = totalRowHeight;
this.Height = dataGridView1.Height + 100;
where dataGridView1 is the name of the Datagridview in this example, and this is refering to the instance of the form. The 100 is a constant for how many pixels higher you want the form than the datagridview. (note: you may want some height checking to make sure you form and data gridview aren't taller than the user's screen)
First, your form would have to be big enough for the DataGridView to expand and show all of the columns. In the screenshot, it doesn't look wide enough. You can set the size of the datagridview manually, but it is rare that you know the exact width of each column at design time because you don't know how much will be in each column at run time. For example, your Invoicedescription column could have strings that vary in length.
What I typically do in cases like yours is to put a splitter control on the form. The text box at the top and the Import Invoices button would go in the top panel of the splitter control, and the DataGridView would go in the bottom panel. Then, set the Dock property of the DataGridView to fill the bottom panel of the splitter control. This way, when the user resizes the form the DataGridView will grow/shrink with it.
You can further control the way the columns appear by setting the DataGridView.AutoResizeRows property.
Just giving this as an option, I'm not aware of any way to have the grid automatically resize to the data it is displaying. You may be able to calculate the height/width of the rows and then manually resize the grid in code, but I would make sure I really needed that requirement first.
This works assuming that all the rows have the same height, even when the first header row has different height (for example long headers are wrapped)
If List.RowCount = 0 Then
List.Height = 0
ElseIf List.RowCount = 1 Then
List.Height = List.ColumnHeadersHeight
Else
List.Height = List.ColumnHeadersHeight + List.Rows(1).Height * (List.RowCount + 1)
End If
Set the datagridviewautosizecolumn to "All"
Then, in the form that contains the datagridview, assuming you populate the data grid view in the load event handler or some other event handler as desired wherever the data is loaded (must be after data is loaded) type: (VB)
Me.Width = DataGridView1.Width + 50 ' Number of pixels wider than the datagridview you want the form to be.
datagridview1 is the name of your datagridview control.
Me refers to the form that contains the datagridview control.
You may need to either maximize the window or anchor it to the top and left to ensure fitment depending on how large of a dataset is being populated.
I know this is probably already answered but just in case I post here what helped me to autosize the whole datagrid view by AllCells:
After your Datasource load, paste the following
dataGridView1.AutoResizeColumns();
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
i used this in IronPyhton to fit the table
1: LayoutPanel.AutoSize = True
2: dataGridView.AutoSize = True
3: self.AutoSize = True