I have DevExpress GridControl with one default GridView to show the records in my form. I have bound the values to GridControl from SQL Database table. Here, I want to select the single column's values as array or something similar using mouse click with any key combination. I have googled and get the solution to select multiple rows using CheckBox column.
But, my scenario is different. I would like to select single column's values as array.
EDIT:
I am also looking for an answer which needs to be highlight all cell values when clicked with any key combination in column header (similar like we have an option in Microsoft Excel).
Thanks in advance.
I don' think that there is a built-in method to do that. You can loop through rows and use ColumnView.GetRowCellValue to access the cells from a particular column.
Something like this should work:
//get the number of rows in a target view (gridView for example)
int rowsCount = gridView.DataRowCount;
//allocate an array for column values
var columnValues = new object[rowsCount];
//get column by field name
var column = gridView.Columns["ColumnName"];
//loop through rows and populate column values
for (int rowIndex = 0; rowIndex < rowsCount; rowIndex++)
{
columnValues[rowIndex] = gridView.GetRowCellValue(rowIndex, column);
}
UPDATE: You can refer to the official example in DevExpress knowledge base: How to select cells under by clicking the GridBand or the column header.
What you'll need to do:
handle MouseDown event on a gridview;
use CalcHitInfo to detect a click on a column header (check out this question: "Detect the column header region in the mouse down event of a grid)" and this knowledge base article: "Hit Information";
then use GridView.SelectCell to update selection
private void SelectCells(GridColumn column) {
for (int i = 0; i < column.View.RowCount; i++)
column.View.SelectCell(i, column);
}
If your grid view is editable, cell editors might steal the MouseDown event. In this case, you'll need to reconfigure the grid as described in this discussion: GridView - How to catch a cell click?
Related
I want to select the value of the firt cell when I click on a specific row in gridControl.
I tried this solution :
int i=Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString())
But it seems it works only on gridView.Any Suggestins please?
Take a look at the Obtaining and Setting Cell Values help-article - to obtain the specific row cell value you can use the ColumnView.GetRowCellValue method as follows:
ColumnView view = (ColumnView)gridControl1.MainView;
string idStr = view.GetRowCellValue(2, "ID").ToString()
If you are using the single-selection mode(the OptionsSelection.MultiSelect property is false) you can use the following API to obtain cell values of a focused row:
- ColumnView.GetFocusedRowCellValue;
- ColumnView.GetFocusedRowCellDisplayText.
These API utilized the handle of a currently focused row via the FocusedRowHandle property:
int id = (int)view.GetFocusedRowCellValue("Id");
To learn more, take a look at Accessing Rows in Code. Row Handles help article.
I have these members on my DataGridView.
private DataTable _dataTable = new DataTable();
public DataTable DataTable {
get { return _dataTable; }
set {
_dataTable = value;
int rowIndex = -1;
//want to preserve the location where the datagridview is scrolled to
if (this.Rows.Count > 0) rowIndex = this.FirstDisplayedScrollingRowIndex;
this.DataSource = new BindingSource() { DataSource = _dataTable };
//-1 is out of bounds and the datasource could have been set
//to a smaller amount of rows after the rowIndex was set
if (rowIndex > -1 && rowIndex < _dataTable.Rows.Count) this.FirstDisplayedScrollingRowIndex = rowIndex;
}
}
When rows are added, removed or edited they are done so in _dataTable. When the rows are changed, DataTable is set so that the DataGridView displays the changes by binding the data to it. My problem is when the DataGridView is sorted by clicking one of the column headers. This changes the display of the DataGridView, but not the order of the rows in the underlying DataTable. How can I get it so _dataTable is sorted like the DataGridView?
I have considered adding a column to keep track of row indexes, but it seems like there should be another way and I don't really want to add a column since this is an abstract class. I have tried handling it in the DataGridView.Sorted events but haven't come up with any solutions that would work.
There is no way to transfer the sorting of the view into the table - not any that are provided by the DataGridView or DataTable classes. Why would you want to? If you are sorting based on a column, that column is in the table, and will be in the table any time you want to iterate through or sort the table in the order of that column.
If you want to persist the user's choice of sort column for the view, that is a different kettle than trying to save the specific sort position for each row. I would recommend just saving what column the user chose. This would not belong in that same table, but in a separate settings storage. If you absolutely want to store the sort position of each row, then you do need a separate column that has the sort order for each row, and you will need to capture the header mouse click event and repopulate that column appropriately.
Also, the minute you add a sort-order column to that table, that is dependent on what column the user chose to sort the data, you de-normalize the data. This means you will run into problems of maintaining data integrity - you will have to capture all sort of events and re-update that column... for example, what if the user changes the value for a row in the middle of the table, such that it now belongs at the end... update the value of that sort order column for that row and every row between its old and new position. What if the user deletes a row - update the value of the sort order column for all rows between the deleted row and the last row of the table.... and on and on with all sorts of user operations.
Much better, store the column name in a setting, and whenever the table is loaded into the gridview, tell the gridview to sort on the column stored in that setting. I am not sure if I am being clear - sorry.
Well explained on msdn. You have to set Columns SortMode and the direction ascending/descending.
for example if I want to make certain column header hidden in Datagridview. I'll use this code:
dataGridView1.ColumnHeadersVisible = false;
But this will make all the column header to be invisible. Now what if I want only certain column header to be hidden.
For instance I have 3 column header. I just want the 3rd column header to be invisible without hidden all the rows data belongs to that column. Is that possible?
Please advise and correct me if I'm wrong.
You will need to handle the DataGridView.CellPainting Event.
In the event handler you will be given an instance of DataGridViewCellPaintingEventArgs. You can use the ColumnIndex and RowIndex properties of this object to determine if a header cell you want to hide is being painted. RowIndex will be -1 if the cell being painted is a column header.
It may just be a simple matter of doing nothing except e.Handled = true; when the header cell in question is being painted.
I found this post searching for help with a similar problem. I was hiding specific columns (anything past column 5) in my grid from the RowDataBound event handler using:
for (int i = 6; i<e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Visible = false;
}
...which worked, but all the headers for those columns were still there. I was able to solve that by adding this line to the loop:
for (int i = 6; i<e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Visible = false;
GridView1.HeaderRow.Cells[i].Visible = false;
}
Now the columns and headers are in sync. I'm not excited that I had to reference the GridView1 itself in the event handler, so if someone knows of a way to improve that, please go ahead.
I was eliminating everything after column 5, but you could use the same concept to hide any specific column/header. Hope that helps.
Just set the HeaderText of that column to the blank string.
Example: You want to hide the first column header:
myGridView.Columns[0].HeaderText = "";
How can I get the rearranged columns out of a datagridview in C#? I need to get all the columns currently being displayed in order. I can get the default columns order by setting datagridview.AutoGenerateColumns=false but I get the same old order even after I've rearranged the columns in my table.
Edit: Basically what I need is to be able to iterate through all the columns of a datagridview based on their display index
Thanks
Use the DisplayIndex property of your DataGridView Columns.
The DisplayIndex property:
Gets or sets the display order of the
column relative to the currently
displayed columns.
Edit:
It's ugly, and I'm sure people are going to point and laugh, but this might give you a rough idea on one implementation to improve upon.
Create a DGV with three columns and set their DisplayName properties like so:
dataGridView1.Columns[0].DisplayIndex = 1;
dataGridView1.Columns[1].DisplayIndex = 2;
dataGridView1.Columns[2].DisplayIndex = 0;
Add the following code to loop through all the columns access the columns in the DGV in their DisplayIndex order:
for (int index = 0; index < dataGridView1.Columns.Count; index++) {
foreach (DataGridViewColumn c in dataGridView1.Columns) {
if (c.DisplayIndex == index) {
// You've got your next column.
Console.WriteLine(c.Name);
}
}
}
How are you selecting? SELECT *? You need to specify the columns in the order you want them.
Have you rearranged their order in the ItemTemplate section of your grid? You can also change the order via the context menu for a GridView, alternative you can change your SQL statement and make sure autogenerate is off
hi i have a datagridview in a form... users by clicking the column name can sort the row data in that column either in ascending or descending orders... how is it possible to disable it? so that the data in rows of every columns stays in that order in which they were on the start of the form... thanks!
Programmatically:
YourDataColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
In the designer:
Right click your DGV and select 'Edit Columns...' from the popup menu. The 'Edit Columns' dialog appears.
In the 'Edit Columns' dialog, update the SortMode property to 'NotSortable' for the column(s) you want to disable sorting on.
Like the other answers state, there is no global property on the DataGrid, you will have to set on each column individually.
for(int x = 0; x < dataGridView1.Columns/Count; x++)
dataGridView1.Columns[x].SortMode = DataGridViewColumnSortMode.NotSortable;
Set the SortMode on the column to Programmatic.
Reference is in VB, but should work in C#.
use a repeater and custom pager control. Forget GridView, DataGrid, etc.