I want to pass rows from datagridview to another in same form.I have numericupdown and i want to choose one row and pass it as many as in my numericupdown value. But in second datagridview i only want to show 1 of them.others will be copied to datatable.
I don't know how to do that.Can anyone help me with this problem?
private void button1_Click(object sender, EventArgs e)
{
if (Dgw1.CurrentRow == null)
return;
foreach (DataGridViewRow row in Dgw1.SelectedRows)
{
((DataTable)Dgw2.DataSource).ImportRow(((DataRowView)row.DataBoundItem).Row);
}
}
Related
I'm developing a C# Windows Form program and I've implemented a DataGridView on it. Now, after setting the data source, when I click the top left button on the datagridview, it selects all rows, just like Microsoft Excel. However, I dynamically hide and show rows on it, and after clicking that button I realized that it also selects the invisible ones. I don't want to implement "SelectionChanged" event because I constantly select some rows and normally I can't select the invisible ones. Only this button selects it. I'm looking for an event like this:
datagridView1_SelectAllClicked(object sender, EventArgs e)
{
// do stuff
}
Something like this will also work since I don't have to check all selections:
dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if(dataGridView1.IsSelectAllCells())
{
// do stuff
}
}
If I have to, I will add the event to deselect the invisible rows, but I prefer some solution like the first one. Any advices? Thanks in advance.
Edit: I'm checking "dataGridView1.SelectedRows" property on button clicks only, not after the selection was made. So, some function that I can implement to button click events will also solve my problem.
The DataGridView class provides the AreAllCellsSelected methode:
Returns a value indicating whether all the DataGridView cells are currently selected. (MSDN)
With that we can get a solution like your second one:
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataGridView view = sender as DataGridView;
if (view.AreAllCellsSelected(true))
{
foreach (DataGridViewRow row in view.Rows)
{
//deselect all invisible rows
if (!row.Visible)
row.Selected = false;
}
}
}
I've managed to solve it finally, the solution was simpler than I expected.
private void RemoveInvisibleSelection()
{
if (dataGridView1.SelectedRows.Count == dataGridView1.Rows.Count)
{
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
if (!dataGridView1.SelectedRows[i].Visible)
dataGridView1.SelectedRows[i--].Selected = false; // decreased the index value since SelectedRows property loses an object
}
}
I have a DataGridView which includes a checkbox column for a Boolean variable in the database.
When I click on this checkbox, it fires the events below.
Inside the uiPersonGridView_CellValueChanged event, I call the Data Table Adapter Update method on the Persons Data Table which is the Datasource for the DataGridView.
The Update works fine, the second time but it will still not Update the Current Rows Data?
For example, if I had two rows, if I tick in the first rows check box; Update returns 0 rows updated for int i and I check the database and nothing has been changed. But if I tick on the second rows check box, the Update returns 1 row updated for int i - I check the database and the first record has changed and not the second?
How can I get it work so the update works for the initial change and changes thereafter?
private void uiPersonGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (uiPersonGridView.IsCurrentCellDirty)
{
uiPersonGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void uiPersonGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (uiPersonGridView.DataSource == null)
return;
if (e.RowIndex == -1)
return;
int i = _personAdapter.Update(_person);
}
What I had to do in order to get this working was set the datarow state to modified and this has done what I need it to do.
private void uiPersonGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (uiPersonGridView.IsCurrentCellDirty)
{
uiPersonGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
DataRowView drv = uiPersonGridView.CurrentRow.DataBoundItem as DataRowView;
drv.Row.SetModified();
}
}
I have a unbound DataGridView with 6 columns, the first being a DataGridCheckBoxColumn. When a user clicks on a checkbox cell, I want to determine what cells have been checked and what ones are not. Here is my code:
private void UpdateSelectedPlaces()
{
//Clear out the places list each time the user selects a new item (or items)
_selectedPlaces.Clear();
foreach (DataGridViewRow row in placesGridView.Rows)
{
if (row.Cells[0].Value != null && row.Cells[0].Value.Equals(true))
{
_selectedPlaces.Add((TripPlace)row.DataBoundItem);
}
}
}
//Click event handler
private void placesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
UpdateSelectedPlaces();
}
I am finding that the DataGridCheckBoxCells are not holding the correct value at the time of the click. This occurs for all rows. There seems to be no pattern really. I was hoping that the event was just not called at the right time (I.e. the checking of the checkbox was yet to be completed) but I cannot prove that.
In short, even though the GUI displays a checked checkbox, the back end thinks the checkbox is not checked when using .Value Is there a simpler way to just determine if each cell[0] is checked or not checked in a datagridview?
I am finding that the DataGridCheckBoxCells are not holding the
correct value at the time of the click.
This is normal and by design because you are using DataGridView.CellContentClick event. From MSDN:
Use this event to detect button clicks for a DataGridViewButtonCell or
link clicks for a DataGridViewLinkCell. For clicks in a
DataGridViewCheckBoxCell, this event occurs before the check box
changes value, so if you do not want to calculate the expected value
based on the current value, you will typically handle the
DataGridView.CellValueChanged event instead. Because that event occurs
only when the user-specified value is committed, which typically
occurs when focus leaves the cell, you must also handle the
DataGridView.CurrentCellDirtyStateChanged event.
The corrected code is similar to the other answer but unfortunately, the reason of the problem was not explained in that answer.
private void UpdateSelectedPlaces()
{
//Clear out the places list each time the user selects a new item (or items)
_selectedPlaces.Clear();
foreach (DataGridViewRow row in placesGridView.Rows)
{
if (row.Cells[0].Value != null && row.Cells[0].Value.Equals(true))
{
_selectedPlaces.Add((TripPlace)row.DataBoundItem);
}
}
}
private void placesGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
UpdateSelectedPlaces();
}
private void placesGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (placesGridView.IsCurrentCellDirty)
{
placesGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void placesGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
Boolean bl;
if (placesGridView.Columns[e.ColumnIndex].Name == "name of check column")
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)placesGridView.Rows[e.RowIndex].Cells[2]; //2 number of check column
//bl is the check box current condition.
//Change only this in your list eg list[e.RowIndex] = bl; No need to check all rows.
bl = (Boolean)checkCell.Value;
}
}
private void placesGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (placesGridView.IsCurrentCellDirty)
{
placesGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
Hi
I have a DataGridView which is bound to an XML source.
I have a problem in editing cells. The cell on click becomes selected and when it is edited, by default we overwrite it. My requirement says it should be ready for editing and not selected when clicked.
I want to generate a row dynamically whenever the 'tab' key is pressed.
How can I achieve this?
If I understand you correctly you want the cell to enter edit mode as soon as it is clicked. This can be achieved by setting the EditMode property of the DataGridView to EditOnEnter.
This leaves the text in the editing control selected however, so if you don't want that you could use:
dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
dataGridView1.BeginEdit(false);
}
Can you explain what you mean by adding row dynamically?
With respect to question 1)
You can try this:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.dataGridView1.CellEnter += new DataGridViewCellEventHandler(myDataGrid_CellEnter);
}
void myDataGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if ((this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewTextBoxColumn) ||
(this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn))
{
this.dataGridView1.BeginEdit(false);
}
}
is it possible to display a DataRow from a DataTable which has the DataRowState.Deleted?
Scenario:
The user can edit some kind of lookup-informations which are presented in the grid.
Now he/she can delete, modify or insert multiple entries and finally store all his/her
changes with one click to the database (assuming there is no primary-key-violation
or some other problem).
Now i want to colorize the different rows according to their edit-status, but the
deleted rows disappear immediatly.
Do you have any idea or another approach to solve this problem?
Edit: I realized that the Grid you are using is not DataGridView. For anyone who wants to do the same with DataGridView, they can do the following:
Create a DataView:
DataView myDataView =
new DataView(myDataTable,
String.Empty, // add a filter if you need one
"SortByColumn",
DataViewRowState.OriginalRows | DataViewRowState.Deleted);
myDataGridView.DataSource = myDataView;
Handle UserAddedRow, UserDeletedRow and CellValueChanged Events:
private void myDataGridView_UserAddedRow
(object sender, DataGridViewRowEventArgs e)
{
e.Row.DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#CCFF99");
}
private void myDataGridView_UserDeletedRow
(object sender, DataGridViewRowEventArgs e)
{
e.Row.DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#FFCC99");
}
private void myDataGridView_CellValueChanged
(object sender, DataGridViewCellEventArgs e)
{
myDataGridView[e.ColumnIndex, e.RowIndex].DefaultCellStyle.BackColor
= ColorTranslator.FromHtml("#FFFF99");
}