DataGridView Moving - c#

I am trying to move selected rows from one DGV to another DGV at the click of a button.
I really don't know where to begin..
I have two seperate DGVs that are bound by a DataSource..
choiceDGV.DataSource = theChoiceList;
universalDGV.DataSource = theUniversalList;
I would like to move any selected items in the choiceDGV to the univeralDGV by a click of a button. I need to make sure the selected rows are removed from the one DGV and added to the 2nd DGV.
Both of the DataGridView's have the same amount of columns.

Did you try:
foreach (DataGridViewRow row in choiceDGV.SelectedRows)
{
universalDGV.Rows.Add(row);
choiceDGV.Rows.Delete(row);
}
or (edited: DataGridViewRow doesn't have ItemArray unfortunately):
foreach (DataGridViewRow row in choiceDGV.SelectedRows)
{
object[] items = new object [row.Cells.Count];
for (int i = 0; i < row.Cells.Count; i++)
items[i] = row.Cells[i].Value;
universalDGV.Rows.Add(items);
choiceDGV.Rows.Delete(row);
}

I can't see the issue. You just need move the items you like to the UniversalList within the onclick button handler and call .DataBind() on both the datagridview.
In other words you have to hit the lists(datasource) instead of the data grid view

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.

Programmatically updating selected rows misses the last one in dgv.DataSource.GetChanges()?

I have a data grid view with a column (combobox column). The following function has been implemented.
Select several rows (click the left most row header and drag).
Programmingly set the value to something (see following). All the selected rows changes.
foreach (DataGridViewRow item in dgv.SelectedRows)
{
item.Cells["cbxxxxx"].Value = p;
}
dgv.EndEdit();
Click the save button to save the changes. However, the last row is excluded in the (dgv.DataSource as DataTable).GetChanges(). The count of the changed data table is always one less than the selected rows. The missing row is the last one (with the black triangle).
How to fix the problem?
The last row presumably is still the active row, so you need to end the edit through the BindingContext:
foreach (DataGridViewRow item in dgv.SelectedRows) {
item.Cells["cbxxxxx"].Value = p;
}
this.BindingContext[dgv.DataSource].EndCurrentEdit();

How to get values from selected cells DataGridView C#?

How can I get to get values from selected cells DataGridView C# ?
not one
i need many one , all what i selected to save in db
in windows form
i want to check every cell in the first Column and then get its value "id"
this Column is a combo box
you can use this code
DataGridViewSelectedCellCollection DGV = this.dataGridView1.SelectedCells;
for (int i = 0; i <= DGV.Count - 1; i++)
{
MessageBox.Show(DGV [i].Value.ToString());
}
DataGridViewSelectedRowCollection t = this.theDataGridView.SelectedRows;
object theValue = this.theDataGridView.Rows[t[0].Index].Cells[index].Value;
Loop the Datagridview
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
Label lbl1=new lable();
lbl1=(label) cell.findcontrol('lbl');
}
}
find the rowindex of the selected the rows and find the controls, you will get all the selected rows.

Creating combobox in different rows on button click..inside gridview?

i want to create combobox inside gridview on button click event...
each time when i cklick on button a combobox will be created in the next row ..means every time only row will change column will remains same...
private void Buttton_Click(object sender, Event e)
{
DataGridViewComboBoxCell CellColumn1, CellColumn2, CellColumn3;
dataGridView1.Columns.Add("Col1", "Column1");
dataGridView1.Columns.Add("Col2", "sanjeev");
//make row 1 at all columns into combobox cell
dataGridView1.Rows[j].Cells[0] = new DataGridViewComboBoxCell();
dataGridView1.Rows[j].Cells[1] = new DataGridViewComboBoxCell();
CellColumn1 = (DataGridViewComboBoxCell)this.dataGridView1.Rows[j].Cells[0];
CellColumn2 = (DataGridViewComboBoxCell)this.dataGridView1.Rows[j].Cells[1];
j++;
}
i tried this logic ..this will create combox inside gridview only at first button click but when i click the button again this is not working..
can any one help me out....
finally i got the answer...
private void BTN_ADD_Click(object sender, EventArgs e)
{ int j=0;
DataGridViewComboBoxCell ColumnItem2 = new DataGridViewComboBoxCell(); // create a combobox cell
ColumnItem2.ValueMember = "Display";
ColumnItem2.DisplayMember = "Display";
for(int i=0;i<10;i++)// it will add elements to a combox
{
ColumnItem2.Items.Add(i);
}
dataGridView1.Rows.Add(); // add row everytime to add a new combobox to the next row.
int columncount = dataGridView1.ColumnCount;// count no of coloumns
int rowcount = dataGridView1.RowCount;;// count no of rows
//i kept count no of columns and rows so that by rows and columns count u can specify to which row or you want to add combobox inside gridview
// below i hard coded the combobox row and column to 2,2 so it will add a combobox to 2nd row and 2nd column.
dataGridView1[2, 2] = ColumnItem2;
//dataGridView1[2, j] = ColumnItem2;
// general way of adding combox to second column and row is everytime added by one as i m incrementing j value every time to each time on button click it will add combox on next row on second column..same way you can specify row and column as per your need.
j++;
}
hope this will help others....
http://csharpprobsandsoln.blogspot.in/2013/04/how-to-add-combobox-dynamically-to.html

DGV ComboBox Cell remain updateable

I have a DataGridView that I want users to be able to add records to directly. This is done by clicking a link beneath the DGV, at which point a new row is programmatically added, and the first visible cell is of ComboBoxCell type. Code excerpt for how I'm doing this is:
// Add a new row to DGV
DataGridView dgv = this.dgvInformation;
DataGridViewRow newRow = new DataGridViewRow();
dgv.Rows.Add(newRow);
// Create cells and add to row
DataGridViewComboBoxCell cellInfoType = new DataGridViewComboBoxCell();
newRow.Cells["InfoType"] = cellInfoType;
// Create DataSource based off LINQ query here
List<ComboItemAccountInfoType> comboDataSource = new List<ComboItemAccountInfoType>();
// List is populated here
// Assign DataSource to combo cell
cellInfoType.DataSource = comboDataSource;
cellInfoType.ValueMember = "AccInfoTypeID";
cellInfoType.DisplayMember = "InfoType";
// Scroll new row into view and begin editing
dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1;
dgv.CurrentCell = dgv[1, dgv.Rows.Count - 1];
dgv.BeginEdit(true);
The DataSource contains some values with an ID of -1, which are categories that the user should not be able to select, and all other values have their valid database ID. This all works fine, except that if a user has selected a -1 row I am unable to keep the combo cell in edit more, as it simply stores the value in the cell and I can no longer activate the drop-down. I have added the following code to the _CellValueChanged event, but it has no effect:
private void dgvInformation_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = this.dgvInformation;
DataGridViewCell cell = dgv[e.ColumnIndex, e.RowIndex];
// If type of cell is ComboBox then
if (cell.GetType() == typeof(DataGridViewComboBoxCell))
{
DataGridViewComboBoxCell cellInfoType = (DataGridViewComboBoxCell)cell;
if ((int)cellInfoType.Value == -1)
{
MessageBox.Show("Going back into edit mode...");
dgv.CurrentCell = cell;
dgv.BeginEdit(true);
}
else
{
MessageBox.Show(cellInfoType.Value.ToString());
}
}
}
I do get the "Going back into edit mode..." message here after moving onto another cell, but it doesn't then do what it says! Could anyone please explain why it won't go back into edit mode, or is there a way of preventing the value becoming locked as soon as it has been selected?
Many thanks!
There should be a "validating" event for you to intercept and cancel if the value is -1.

Categories