hi guys,

i have a databound listbox. the problem is i can't save the last record i entered without clicking some other record first. the datatable just does not treat it as an added or modified record. why is this so?

regards,
Han

Hi,

If you had used the DataSource property it should not let you modified the ListBox. Can you post some code.

Regards,
Camilo

sorry i forgot to mention that other than the listbox, there are another two textboxes, txt1 and txt2 bound to the datatable.

what i wanted it to do was when i click on a button (add), i added a default row to the datatable:

DataRow newdatarow = dtRemarks.NewRow();
newdatarow["Title"] = "New Remark"; (bound to txt1 as well as the listbox)
newdatarow["Remark_Value"] = ""; (bound to txt2)
dtRemarks.Rows.Add(newdatarow);

when i do this, a new row will be created and thus bound to txt1 and txt 2.

the problem comes when i have only one item in the list box. when i change the value of txt1 and/or txt2 and click save using the following code segments:

DataRow[] drAdded = dtRemarks.Select("", "", DataViewRowState.Added);
DataRow[] drModified = dtRemarks.Select("", "", DataViewRowState.ModifiedCurrent);

the modified data does not appear in the drModified datarow array. however, if i have two items in the listbox, it will appear in it if i first click on another item in the list box.

any idea?

Hi,

In this code

DataRow[] drAdded = dtRemarks.Select("", "", DataViewRowState.Added);
DataRow[] drModified = dtRemarks.Select("", "", DataViewRowState.ModifiedCurrent);

I think that the problem is that each row has a property RowState and the DataTable.Select method compare the paramter that you passed against the value of this property and can only be either DataViewRowState.Added or DataViewState.ModifiedCurrent for a single row.


Also you can go to this link

http://msdn.microsoft.com/en-us/library/thc1eetk(VS.80).aspx

that it shows another way of retrieving the changed rows in a DataTable.

And in this link you can see how the RowState change with each operation.

http://msdn.microsoft.com/en-us/library/system.data.datarow.rowstate(VS.80).aspx


Regards,
Camilo.

thanks for the info. however i tried using the datatable.get and it returned exactly the same thing.

i think my problem is concerning not being able to tell the system that i have changed a new row in the datatable without clicking on another selection first. even datatable.get does not get the new data unless i click on another item on the listbox.

updating the data in a datagridview is possible because it captures the cell value changed, or celldirty event, etc. however when working with a databound textbox, the system just does not seem to get the updated data unless i 'manually' change the selection to another item on the listbox, then and only then will the data be updated.

any more ideas?

Hi,

Try refreshing the DataGridView control programmatically(DataGridView.Refresh()) right after inserting the new row and if it doesn't then copy and paste the code as appear in you application in a new post.

Regards,
Camilo

ok. here goes.

the listbox, together with the text fields are bound to a binding source and datatable.

//List box coding binding code:
bnsrcRemarks.DataSource = dtRemarks;
lstbxRemark.ValueMember = "ID";
lstbxRemark.DisplayMember = "Title";

//text field binding code:
txtTitle.DataBindings.Add("Text", bnsrcRemarks, "Title");
txtRemarks.DataBindings.Add("Text", bnsrcRemarks, "Remark_Value");

//code used to add a new temporary row into the datatable when the user clicks add.
DataRow newdatarow = dtRemarks.NewRow();
newdatarow["Title"] = "New Remark";
newdatarow["Remark_Value"] = "";
dtRemarks.Rows.Add(newdatarow);
bnsrcRemarks.DataSource = dtRemarks;
lstbxRemark.SelectedIndex = lstbxRemark.Items.Count - 1;

//code used to save
if (lstbxRemark.Items.Count > 0)             
{
CurrencyManager gridCurrencyManager = (CurrencyManager)this.BindingContext[bnsrcRemarks.DataSource];
gridCurrencyManager.EndCurrentEdit();
lstbxRemark.SelectedIndex = 0;
}
bool saveSuccess = false;
DataRow[] drAdded = dtRemarks.Select("", "", DataViewRowState.Added);
DataRow[] drModified = dtRemarks.Select("", "", DataViewRowState.ModifiedCurrent);
DataRow[] drAll = Tools.MergeDataRowArray(drModified, drAdded);
record.SaveRemark(drAll, shipment_id);

therefore the listbox on the left hand side shows the records added while the text boxes store the values the user entered.

if i have two items in the list box and i just added a new record, say record 2 and edit the values in the textboxes, i have to select record 1 in the listbox, then save the latest changes in record 2. if i modify the text values for record 2 and click save right away, it will only save the old data.

hope this clarifies matters.

regards.

anyway i seem to notice that only text boxes and listboxes exhibite this behaviour. i used the currency manager line of code on a datagridview and it worked fine. i wonder if i'm missing anything on the text boxes and listbox?

Paste this before the save code and try modifying the first row in the DataGridView(I am assuming that you have two or more rows),

int selectIndex = this.listBox1.SelectedIndex;
            this.listBox1.SelectedIndex = 1;
            this.listBox1.SelectedIndex = selectIndex;

Regards,
Camilo

i think the code you gave me is an attempt to switch the focus away from the row i'm editing so that it 'commits' the data?

i'm actually using a variation of that right now. it works fine with small input but when the user has a very long remark, switching to the first record will make the remark's value flash from the second to the first, then back again (since its databound to the list) giving a very funny flashing effect.

any more ideas?

thank so much for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.