selected index in datagridview - c#

I Have two tables I select a record from table 1 to show information in table 2 on double click in one of information I got window to update data after closing this window I have to refresh the two tables and I want the same row be selected in table 1.
I was working in wpf and I use selectedindex.
Now I use this for the moment:
int index = dgTable1.Grid.CurrentRow.Index;
///**
frm.ShowDialog();
frm.Dispose();
ReloadTable1();
selectedindex(index)
with
private void selectindex(int index)
{
dgwTable1.Grid.Rows[index].Selected = true;
DATAtype data= dgwTable1.GetObjectFromRow<DATAtype>(index);
LoadTable2(data);
}
It work But I have Grid_SelectionChanged for table1 and don't fire i have to reload data, also If the scroll bar is down when I use this I return to the top of table 1!
But I know this not the right way to do it:( and it's too simple in wpf :/
dgTable is a UserControl with a DataGridView as a Grid

To Save Current Index :
int index = dataGridView1.CurrentRow.Index;
After Editing:
dataGridView1.Rows[index].Selected = true;
Hope this helps.

May be DataView and BindingSource with filters better to use?
DataView view = new DataView(_table);
BindingSource tSource = new BindingSource();
tSource.DataSource = view;
_dataGridView.DataSource = _tSource;
_tSource.Filter = "Value=0";
like this...
To save scrolling use myDataGridView.FirstDisplayedScrollingRowIndex
int scrollIndex = 0;
if (myDataGridView.FirstDisplayedScrollingRowIndex >= 0)
scrollIndex = myDataGridView.FirstDisplayedScrollingRowIndex;
after editing:
if (myDataGridView.Rows.Count > 0)
myDataGridView.FirstDisplayedScrollingRowIndex = scrollIndex;
Hope this helps.

In my opinion, the easiest way to solve your problems, is to use the MVVM pattern. In your ViewModel for your View that contains the two DataGrids, you have a property like "SelectedNameOfContentClass" that is bound to first DataGrids SelectedValue-property. This "SelectedNameOfContentClass"-property is also bound to seconds datagrid DataSource. So if you changes the selected row in first DataGrid, the Source of second DataGrid automatically updates.
The list, that is bound to first Datagrids DataSource, should be an ObservableCollection. For the dialog you can use the IEditableObject interface in your objects.

Related

how to move multiple rows from one Grid View to another | WPF

I have a GridView in which the user can select rows he wants to copy (move it to another grid view). up until now, the user had to select each row one by one. I want to improve the UX by letting the user select multiple rows at once and copy multiple rows.
Is there any way I can do this in an easy way?
The image shows what i want to do, move the selected rows to the right grid view I am using C# WPF and the data is stored in a Datatable which is linked to the grid view
The code I used for moving one line:
DataRowView viewRow = (DataRowView)DBElementGrid.SelectedItem;
DataRow row = selectTable.NewRow();
for (int i = 0; i < DBElementGrid.Columns.Count; i++)
{
row[i] = viewRow[i];
}
selectTable.Rows.Add(row);
for (int i = 0; i < DBElementGrid.Columns.Count; i++)
{
selectTable.Columns[i].ColumnName = DBElementGrid.Columns[i].Header.ToString();
}
selectGrid.DataContext = selectTable.DefaultView;
This should work provided that the right DataGrid (targetDataGrid) is bound to a DataView that contains the same number of columns as the original one that you select from:
DataView dv = targetDataGrid.ItemsSource as DataView;
if (dv != null)
{
foreach (var selectedItem in selectGrid.SelectedItems.OfType<DataRowView>())
{
dv.Table.Rows.Add(selectedItem.Row.ItemArray);
}
}
You can directly access the selected Items of the dataGrid in case you are using DataGrid. So DataGrid.SelectedItems will give you the selected items which you can set as the items source for the right DataGrid.

DataGridView prevent auto increasing of id bound column

I have a database with id (identity) column. This column is bound to my DataGridView (WindowsForms). Whenever I add a row to DataGridView (it's not yet added to database, just to DataGridView), number in this column is increasing by one. Extremely annoying behaviour and I don't know how to stop it?
I am not using EF or antyhing similar. Just columns in DataGridView (not automatically generated, but user defined columns - AutoGenerateColumns is switched off), added to DataGridView and with each column set the DataPropertyName.
I don't know your code, but did you try to do this:
datagridview1.AllowUserToAddRows = false;
and make a button for adding the rows , and add it with
datagridview1.Rows.Add(-1,..,...);
Why not just hide the column?
dgvMyDataGridView.Columns["Id"].Visible = false;
I haven't found a way to prevent DataGridView from auto increasing id. But, I could change any value in DataGridView now, with the DataSource being bound to BindingList instead of DataTable, as before (each ComboBoxItem must implement INotifyPropertyChanged).
private void initializeDgvNotesDataSource()
{
List<ComboBoxItem> list = new List<ComboBoxItem>();
BindingList<ComboBoxItem> bindingList = new BindingList<ComboBoxItem>(list);
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = bindingList;
dgvNotes.DataSource = bindingSource;
}
Cast your integer type id column as string before binding to grid. like
select CAST(ID as varchar(100)) as from yourtablename.
You can also set the default value to identify the new record.

datagridview manually filled column value initially not showed

I have an application with multiple datagridviews in a tabcontrol.
One of the tabcontrols has 4 columns that are filled by the datasource.
I don't use autogenerate columns, but use the DataPropertyName to bind values to the columns.
I also have 2 columns i create, wich are filled manually, after assigning the DataSource.
Values in this datagridview are filtered values from a datagridview somewhere else in the application.
When i filter, then go to the tab containing the datagridview, the manual columns are empty.
Changing the filter (but not the results) fills those colums.
How can i get the columns that are manually filled to always show their values?
Calling Refresh() or Update() on the datagridview doesn't solve my problem
Short version of my code:
I actually use a class that inherits from DataGridView
//fill datagridview
PcbLink[] links = Service.Instance.Client.queryPcbLinks();
List<Pcb> pcbs = new List<Pcb>();
foreach (PcbLink link in links)
{
PcbFilter pcbfilter = new PcbFilter();
pcbfilter.pcb_id = link.pcb_id;
Pcb[] res = Service.Instance.Client.queryPcbs(pcbfilter);
pcbs.Add(res[0]);//only first element because pcb_id should always be unique -> only one row
}
cdgvUsage.SetData(pcbs);//setData is used as cdgvUsage.DataSource = pcbs, but does some stuff internally
for (int i = 0; i < links.Length; i++)
{
SortableBindingList<Pcb> dataSource = cdgvUsage.GetData<Pcb>();
cdgvUsage["count", i].Value = links[i].count;
char variantchar = (char)('a' + (char)(dataSource[0].variant));
cdgvUsage.Columns["variant"].ValueType = typeof(string);
cdgvUsage["variant", i].Value = variantchar.ToString();
}
I believe after asigning DataSource to the dataGridView changes must be saved in datasource also otherwise they will not be reflected in the dataGridView (as it takes data from datasource everytime you do Refresh() or Update()).
Make sure manually filled values are stored in dataSource.

How to hide all columns of Grid Grouping Control ( Syncfusion Control ) at once?

I am developing a window application(C#) in which i am using Syncfusion Grid Grouping Control. I have attached a DataTable to the DataSource property of Grid Grouping Control, the DataTable have 24 columns but i wanted to show only 3 columns in the Grid Grouping Control. I can hide column one by one as follows : gridGroupingControl1.TableDescriptor.VisibleColumns.Remove(colName);
But this is a long process to hide column one by one if columns count is more. What i want to hide all columns by-default and then show/unhide the columns which i wants ?Thanks in advance.
You can hide a range of columns using "ColHiddenEntries". Here is the code that could be used to perform the operation.
GridColHidden[] hiddenCols = new GridColHidden[ 3];
for (int i = 0; i < 3; i++)
{
hiddenCols[i] = new GridColHidden(i + 1);
}
this.gridGroupingControl1.TableControl.Model.ColHiddenEntries.AddRange(hiddenCols);
I hope this would simplify your task of removing columns.
One simple way, is to create your columns with the Width property with 0;
column.Width = 0;
grid.TableDescriptor.Columns.Add(column);
Works fine for me.

Load rows as needed in DataGridView

I have a WinForm DataGridView bound to a List<> of objects and I would like to set the datasource of the DataGridView to display only so many records at a time. From a few searches it looks like there is a way to do this however I have not found the exact method. Is there a way to set the total row count and then set an event that fires when more rows are needed? I am thinking that I need to do something like:
private const int AMOUNT = 1000;
private int pageCount = 0;
this.grdItems.VirtualMode = true;
// Initial Load
this.grdItems.RowCount = myList.Count();
this.grdItems.DataSource = myList.Take(AMOUNT);
// When the user scrolls to the bottom of the list
this.grdItems.DataSource = myList.Skip(pageCount++).Take(AMOUNT);
You need to set VirtualMode =true to get it working

Categories