I have bindingList bound to TextBoxes. When I enter new value in textboxes and press save button the value from last Textbox dosen't save in database. But when I pressed Tab key before save button every values from texboxes were saved. How resolve this problem with unsaved last value?
....
BindingList<Zamo> myList;
pg = new PGEntities();
var query = (from zam in pg.Zamo where zam.ID == rekord.ID).Take(1);
myList = new BindingList<Zamo>(query.ToList());
zamoBindingSource.DataSource = myList;
....
private void SaveButton()
{
pg.SaveChanges();
}
This occurs only when I save by button in BindingNavigation.
When you press the tab key it is getting saved? that probably means the saveButton event is reading the text before the entered text is considered. Try to lose focus on the textbox, then save.
Related
I'm using the table adapter in C# where I created a DataGridView. When I double click a row, I am displaying all the cells in the row in textboxes in a different Form. After I edit the textboxes and click my save button, I would like to take the values from the textboxes and replace them in the database. I can see the changes on the DataGrid, however I am not able to save them in the database.
private void InventoryData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//make new form and display them there
ViewInventoryItem ViewItem = new ViewInventoryItem();
ViewItem.textBox1.Text = this.InventoryData.CurrentRow.Cells[1].Value.ToString();
ViewItem.textBox2.Text = this.InventoryData.CurrentRow.Cells[2].Value.ToString();
ViewItem.textBox3.Text = this.InventoryData.CurrentRow.Cells[3].Value.ToString();
ViewItem.ShowDialog();
if (ViewItem.DialogResult == DialogResult.OK)
{
//save button was pressed
this.InventoryData.CurrentRow.Cells[0].Value = ViewItem.textBox1.Text;
this.InventoryData.CurrentRow.Cells[1].Value = ViewItem.textBox2.Text;
this.InventoryData.CurrentRow.Cells[2].Value = ViewItem.textBox3.Text;
this.Validate();
this.InventoryData.EndEdit();
this.booksTableAdapter.Update(this.InventoryDataSet.Books);
}
}
Found the problem. In the Dataset the Table had only Fill and Get Data. Even after selecting the Wizard to add the other statements it was failing. After adding the UpdateQuery manually, the code works without issues. Thank you Crowcoder for pointing me in the right direction.
I have a form which its controls are binded to a bindingsrource named "userbindingsource". When the form loads all the value in bindingsrource will be set to equivalent Textbox. But when the textboxes values change , the bindingsrource won't update. and still it shows the very first value
For example after load I change the first name in textbox and then click save Button to call the saveRecord(). when I check (userBindingSource.DataSource as User) , it still contains the first firstname without any changes.
public void SaveRecord()
{
int i = 0;
User user = userBindingSource.DataSource as User;
if (user.Id > 0)
user.State = State.Modified;
using (ECarServiceClient client = new ECarServiceClient())
{
i = client.SaveUser(user);
}
}
how can make the bindingsource updates automatically?
If the textbox binding has DataSourceUpdateMode.OnValidation (the default) and you go right from typing in a textbox to clicking the Save button, the Save click is handled before the textbox validates. Validation is what makes the BindingSource update the User object.
The fix would be to call this.Validate() at the beginning of SaveRecord. This triggers validation of the focused control within the current form and all its ancestors.
I have a DataGridView where the first column is a DataGridViewCheckBoxColumn. The user checks some checkboxes to indicate which items are to be deleted.
When I hide the form and reload it, I need the DataGridView to remember which checkboxes were checked.
You need to save the changes at least at the point where you close your Form (if you just hide it, why reload then?).
The way I usually work this out is listening to the Dgv's CellEndEdit event:
SomeDataGridView.CellEndEdit += ObjectPropertyChanged
then in the Callback you can get the Object back by using the "DataBoundItem" prop of the Dgv and process / save it however you need:
protected virtual void ObjectPropertyChanged(object sender, DataGridViewVellEventArgs e)
{
var selectedObject = ((DataGridView)sender).Rows[e.RowIndex].DataBoundItem;
//Assuming you stored in a List and each Object has an ID as prop:
var indx = _Objects.IndexOf(_Objects.Where(o => o.ID.Equals(selectedObject.ID)))
_Objects.Remove(indx)
_Objects.Insert(indx, selectedObject)
}
You could also do database update, write to a text-file, save in configuration, ...
I have two DataGridViews with editable by the user.
When the user select a row in DataGridViewA then DatagridViewB is populated with different data accordingly.
Once edited the current row in DataGridViewA, the user can click on the save button. In case he didn't save and select another row in DataGridViewA, a message for save will be prompted to the user asking him if he want to save before moving to another row. (I have a method that check if there are changes and if yes, prompt a message to the user if he wants to save - yes: Save, No: Restore Data).
The method that checks if there are changes is triggered on DataGridViewA_Leave(). The reason is that I have the information of the DataGridViewA.CurrentRow before leaving the row so I can Save if needed.
The problem is if I am clicking on any other control on my form such as Save button the Save message to the user prompt (because the DataGridViewA.CurrentRow is left). In this case, I do not want the user to get a message.
I found a workaround to fix it but it is not the best way to do it I presume. Before prompting the message, I am checking if the Save button has focus. This check need to be done for all the controls that I don't want to get a Save message when clicking on them.
Is there any better solution to solve this issue?
My code, the record state is Added (user add a row) or Edited (user update some data) :
private void AlternateDGV_RowLeave(object sender, DataGridViewCellEventArgs e)
{
if (IsSaveNeeded)
{
if (SaveButton.Focused) // This is the work around
{
return;
}
if (!WasSavedPerUserRequest())
{
var recordstate = AlternateDGV.CurrentRow.Cells[Glossary.RecordStateName].Value.ToString();
if (recordstate == Glossary.AddedRecordState) // if it was a new row added and the user doesn't want to save
{
AlternateDGV.CurrentRow.Delete();
}
else if (recordstate == Glossary.EditedRecordState) // if it was a row from DB and the user doesn't want to save, refresh with saved data
{
AlternateDGV.CurrentRow.Refresh(_items);
}
}
}
}
In my opinion, you should change something in the method you use to load data and manage the grid.
If you load the data in DataGridViewB using the DataGridViewA.SelectionChanged event, you should be able to check for unsaved changes and then reload the DataGridViewB content.
NOTE: SelectionChanged event is fired only after the selected row is changed, so you should at least save the index of the current selected row when loading the rows in DataGridViewB, so you can check the correct row when a new row is selected.
For example:
void DataGridViewA_SelectionChange(object sender, EventArgs e)
{
if (previousSelectedIndex != -1)
{
//Check for changes end if necessary prompt the message
[...]
}
//Load data in DataGridViewB
[...]
previousSelectedIndex = dataGridViewA.SelectedRows[0].Index;
}
EDIT:
Another solution could be to check if the grid still has the focus. If it has not, then the user clicked outside the grid and you don't have to prompt the message.
private void DataGridViewA_RowLeave(object sender, DataGridViewCellEventArgs e)
{
if (IsSaveNeeded)
{
if (dataGridViewA.Focused == false)
{ //DataGridViewA without focus, then nothing to do
return;
}
if (!WasSavedPerUserRequest())
{
var recordstate = AlternateDGV.CurrentRow.Cells[Glossary.RecordStateName].Value.ToString();
if (recordstate == Glossary.AddedRecordState) // if it was a new row added and the user doesn't want to save
{
AlternateDGV.CurrentRow.Delete();
}
else if (recordstate == Glossary.EditedRecordState) // if it was a row from DB and the user doesn't want to save, refresh with saved data
{
AlternateDGV.CurrentRow.Refresh(_items);
}
}
}
}
i guess you make visible 'Save' button once User click on Edit button at row level , So when user click on edit button and suddenly leave the row you can check visibility of save button at row level on your AlternateDGV_RowLeave event if it is still visible that means that user has not clicked the save button yet and you can prompt user.
I have a textbox and a button. On page load I select one column from one row and put its value in the textbox. I have a button click method that updates the same column/row with the value in the textbox.
The problem i'm having is that when I clear the text in the text box, type in new data and hit submit the new text value is not being saved, it uses the old one.
I put a breakpoint at the end of my button click method and it appears that asp.net is sending the old value of the textbox rather than the new one I put in. I'm totally stumped.
This problem persists even if I have viewstate false on the textbox.
Both of my LINQ queries are wrapped in a using statement.
Any Ideas?
Edit: Here is the full code:
protected void Page_Load(object sender, EventArgs e)
{
using (StoreDataContext da = new StoreDataContext())
{
var detail = from a in da.Brands
where a.BrandID == 5
select new
{
brand = a,
};
foreach (var d in detail)
{
txtEditDescription.Text = d.brand.Description;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
using (StoreDataContext dk = new StoreDataContext())
{
Brand n = dk.Brands.Single(p => p.BrandID == 5);
n.Description = txtEditDescription.Text;
dk.SubmitChanges();
}
}
As you said, in Page_Load you are retrieving the original value into the text box which overwrites any changes.
Yes, Page_Load DOES execute before Button Click (or any other control events) is executed. I'm going to guess you have done Windows Forms development before this, the ASP.Net web forms event model is quite different. Check out the ASP.NET Page Life Cycle Overview.
I figured it out. I should be be using if(!IsPostBack) on the code that originally fills the textbox with its value.
However this makes no sense to me. If the page is loaded and the textbox text gets a value, then you clear this value and try to insert it into the database, it should insert this value and then when the page post back it will fetch the new value from the database and put the value in the textbox.
The way it's actually working makes it seem like it is executing the page load code before the button click code is executed on post back.
just to trace the error,please try to put a label =( lblDescription.Text ) and leave the rest of code as is,put the new value in the textbox (editdescription.text) try it and tell me what you see
foreach (var d in detail)
{
lblDescription.Text = d.brand.Description;
}