c# datagridview datatable - c#

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.

Related

Unable to select single column's values in WinForms DevExpress GridControl

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?

Allow row selecting in a dataGridview but don't allow cell selecting

I would like to allow row selecting in a DataGridView ( C#, WinForm ) and don't allow cell selecting.
Is there a way to do this please ?
Thanks a lot :)
Check your Properties window in Visual Studio after selecting the DataGridView. You'll notice a property called SelectionMode. Set it to FullRowSelect.
this.DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.DataGridView.Columns["Text"].ReadOnly = true;
DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect means individual cells cannot be selected.
DataGridViewColumn.ReadOnly = true means that cells cannot be double-clicked into.

C# WinForms: Adding rows to a dataGridView

I've just started to use dataGridView, and some things seem strange to me:
when there is a Columns property, in which you can add columns, why is there not a Rows property? It seems you can only add rows programmatically. Or am I wrong?
the 'star' icon to the left of the first row, can't it be removed?
is it possible to disable sorting? I mean when you click on a column, the "sorting-arrow" appears, suggesting that the cells would be sorted.
If it is only possible to add rows programmatically, I have this question:
I need a dataGridView with 1 column and x rows. How to do this the quickest and easiest way? The cells will be filled at runtime, programmatically.
update: About disabling sorting, I found out myself: there is a property to change this if you open the (Collections) of the columns.
You can add Rows by calling dataGridView1.Rows.Add();. For multiple rows there is an overload for the same available. dataGridView1.Rows.Add(5);
Now to fill the dataGridView1 rows you can either assign a DataSource and set the DataPropertyName for the Column.
Else loop through and fill the data cell wise like dataGridView1[columnindex,rowindex].Value =something
The star icon shows the current row which is being edited, you can choose to hide that cell by setting the RowHeadersVisible to false.

ListView how to select the text inside a cell?

well i have worked with datagridview now i need to work with a listview i dont know how to select the value it has in one cell..
my another option is when a cell was added or deleted, all this information is on datagridview
but it doesn't have a (datasourcechanged) then how can i anyone of these?
I tried secund with this code:
DataGridView1.DataSource = lvDevices
so i could select anyone but i need when in datagrid changed its value, it passed to datagridview well my another way is
how can i select the value of one cell in a ListView?
you could try this in this listview1_doubleclick event. ...
string a = listView1.SelectedItems[0].SubItems[column index corresponding selected cell].Text.

How to change the behavior of the DataGridView when clicking a column?

When I click a column of the DataGridView control, the control sorts all the data according to the column.
Is it possible to disable this function? (Actually I want to select the whole column as in Excel or Word tables.)
Thanks!
To disable sorting in the grid, all you need to is to set the SortMode property of each column to DataGridViewColumnSortMode.NotSortable, like this:
foreach (DataGridViewColumn column in dataGridView.Columns)
{
column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
As for the full column selection, that functionality is not present. Making it work will need quite some work (if it is possible at all, which I doubt is anyways!).

Categories