be aware of insert delete and update data into gridview in c# - c#

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();
}

Related

C# SQL DataGridView change field before updating database

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);
}

DevExpress Grid, Bool rows change not reflect immediatly

I have a DX grid view and it has 2 bool columns. My purpose is when I check for ex 2nd row in column1, column2 2nd must be changed immediatly, but not. It changes after clicking another row. Here is my rowcellvaluechanged event code :
void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
//button1.PerformClick();
if (e.Column.Name == "col1")
{
var x = gridView1.GetRowCellValue(e.RowHandler, col1);
gridView1.SetRowCellValue(e.RowHandler, col2, x);
}
}
I looked for in DX website and there is no solution. How can I handle on it ?
You can use the GridView.PostEditor method to immediately pass the changed value from the editor into the Grid's underlying data source. The best place for doing this is the EditValueChanged event of real cell editor. You can handle this event as follows:
gridView1.PopulateColumns();
var checkEdit = gridView1.Columns["Value1"].RealColumnEdit as RepositoryItemCheckEdit;
checkEdit.EditValueChanged += checkEdit_EditValueChanged;
//...
void checkEdit_EditValueChanged(object sender, EventArgs e) {
gridView1.PostEditor();
}
Then you can implement all needed dependencies:
gridView1.CellValueChanged += gridView1_CellValueChanged;
//...
void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) {
if(e.Column.FieldName == "Value1")
gridView1.SetRowCellValue(e.RowHandle, gridView1.Columns["Value2"], e.Value);
}
The Grid posts an editor value when you switch to another cell. To force it, call the PostEditor method. But, in this case, you will need to use the EditValueChanged event of the active editor. To get it, use the ActiveEditor property. To catch the moment, when an editor is created, use the ShownEditor event.
gridView1.ShownEditor += gridView1_ShownEditor;
private void gridView1_ShownEditor(object sender, EventArgs e) {
GridView view = (GridView)sender;
view.ActiveEditor.EditValueChanged += editor_EditValueChanged;
}
private void editor_EditValueChanged(object sender, EventArgs e) {
if (!gridView1.FocusedColumn.Name == "col1")
return;
gridView1.PostEditor();
var x = gridView1.GetRowCellValue(e.RowHandler, col1);
gridView1.SetRowCellValue(e.RowHandler, col2, x);
}

Problems with DataGridView when add new items

When I save a new item on datagridview the statement
MessageBox.Show(this.tb_aprovacao_admissaoDataGridView.CurrentRow.Cells[0].Value.ToString());
shows the value -1. How can I change it to show the real number of ID?
Thanks to everyone.
Depends how you want to get the value..
Do you want to get the value after you click on the cell, or on the click of a button or?
If you want to do it on the cell click event, you can do it like this:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
}
To get it with a button:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
}

c# .net datagridview SelectionChanged

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

Display a deleted DataRow in a (Infragistics.Ultra-) grid

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");
}

Categories

Resources