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");
}
Related
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);
}
}
I have a WindowsForms app with a DataGridView bound to a SQL database. All columns except one ("ts_last_edit") are editable.
I want to update the "ts_last_edit" column only programmatically with the current DateTime every time someone changes some of the other columns AND saves the changes.
I tried this:
...
this.MyTableAdapter.Adapter.RowUpdating += Adapter_RowUpdating;
...
private void Adapter_RowUpdating(object sender, SqlRowUpdatingEventArgs e)
{
e.Row["ts_last_edit"]=DateTime.Now;
}
But it did not work because e.Row is a read-only property apparently.
I tried changing the UPDATE SQL command and it works:
private void Adapter_RowUpdating(object sender, SqlRowUpdatingEventArgs e)
{
// pseudo code
string newSqlCommand= "STUFF CURRENT DATE AND TIME IN COLUMN ts_last_edit";
e.Command.CommandText = newSqlCommand;
}
This works OK, but I was wondering if there is a better way to do it.
Thank you
you need to merge changes back to dataset after updating the time
private void brandBookBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
var changes = dataSet1.GetChanges();
if(changes!=null && changes.Tables["BrandBook"].Rows.Count>0)
{
foreach(row in changes.Tables["BrandBook"].Rows)
{
row["ts_last_edit"] = DateTime.Now;
}
}
dataSet1.Merge(changes, false,System.Data.MissingSchemaAction.Add);
this.brandBookBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.dataSet1);
}
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 grid view that has a column called Amount .I have a textbox that shows the sum of amount column in gridview ,but i need to be aware when items in gridview are inserted changed and deleted because i need to update the textbox
Has anyone handled such a scenario. I would like to know which datagrid event I should be using,
I am using windows form application
Best regards
You can make use of RowsRemoved, RowsAdded and CellValueChanged events.
Updated
You can use these assuming your user is only adding and deleting 1 row at a time. also added a method with a linq statement to sum up your amount and assign it to the textbox.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex.Equals(dataGridView1.Columns["Amount"].Index))
{
UpdateTotal();
}
}
private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
UpdateTotal();
}
private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
UpdateTotal();
}
private void UpdateTotal()
{
textBox1.Text = (dataGridView1.Rows.Cast<DataGridViewRow>()
.Sum(t => Convert.ToInt32(t.Cells["Amount"].Value))).ToString();
}
I'm racking my head over something that should be pretty simple to do. Its been a day now so I finally give up and I will ask the question. How can I actually trigger the selectionChanged event on the datagridview in .net?
I would basically like to grab the row values when the user double-clicks/ or single clicks any where on a row. but I cant for the life of me get this event to fire even tough I read here that this should be the even I need to use?
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in AddrGrid.SelectedRows)
{
string value1 = row.Cells[0].Value.ToString();
string value2 = row.Cells[1].Value.ToString();
//...
}
}
I have tried something similar to this but im hopeless I click on the datagrid cells or rows and this does not fire what I'm I missing?
when I click on a cell I get this event to fire.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
Addresses.aTyp = AddrGrid.Rows[AddrGrid.CurrentCell.RowIndex].Cells["Address Type"].Value.ToString();
Addresses.seq = AddrGrid.Rows[AddrGrid.CurrentCell.RowIndex].Cells["Sequence"].Value.ToString();
}
But I like to capture the double click or click on a row not just a cell.
Any help would be most appreciated.
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
string value1 = row.Cells[0].Value.ToString();
string value2 = row.Cells[1].Value.ToString();
}
}
you can use this
private void AddrGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
{
Addresses.aTyp = AddrGrid.Rows[AddrGrid.CurrentCell.RowIndex].Cells["Address Type"].Value.ToString();
Addresses.seq = AddrGrid.Rows[AddrGrid.CurrentCell.RowIndex].Cells["Sequence"].Value.ToString();
}
On your grid object lets call it 'foo'. You will do something like..
foo.SelectionChanged += dataGridView1_SelectionChanged
you will need to do that somewhere to wire the event up. I normally do it in the constructor for the form
Many answers relating to the selectionchanged event for a DataGridview use "DG1.SelectedRows(0)". This is fundementally wrong. There may be multiple rows selected and the first selected row may not be the newly selected row. Unless you want to parse over all of the selected rows each time, "DG1.Rows(DG1.CurrentCell.RowIndex)" should be used