Groups | Blog | Home
all groups > dotnet windows forms databinding > september 2005 >

dotnet windows forms databinding : DataGrid With SortList DataSource (update datasource problem)



rbfigueira
9/1/2005 5:55:06 AM




Friends,

I have tried to find some information about my problem BUT i can't.

I have one DataGrid (Windows Forms) that uses for the database one
(SortedList - HashTable)
SortedList h = DataBase.FillShortList(); // Fill my SortList "Hashtable"
DataGrid.DataSource= new ArrayList(h.Values); // Convert my SortList to
ArrayList.


MY BIG PROBLEM:

When the user change one value on the DataGrid (field: Adeb or Acred) I what
to UPDATE my SortedList - HashTable !!

I what to:

1 - Bind the SortList to the DataGrid ( ok, done)
2 - User update the field (field: Adeb or Acred) in DATAGRID (windows forms):
2.1 - Update the SortList
3 - Refresh the DataGrid

How to do the step 2.1 ???

How to update my SortedList (HashTable) when the user updates the DataGrid
(field: Adeb or Acred) ???




More details:

I have one DataGrid (Windows Forms) that use one ArrayList for the DataSource

SortedList h = DataBase.FillShortList(); // Fill my SortList "Hashtable"
DataGrid.DataSource= new ArrayList(h.Values); // Convert my SortList to
ArrayList.


The FillShortList() Function is responsable to fill my SortList "Hashtable":

{
....

SortedList h = new SortedList();
Data d = new Data(); // Uses custom class


d.NumConta=Convert.ToString(result["NumConta"]).Trim();
d.Adeb=Convert.ToDouble(result["Adeb"]);
d.Acred=Convert.ToDouble(result["Acred"]);

h.Add(Convert.ToString(result["NumConta"]).Trim(),d); // My SortedList uses
the numConta for the Key

return h;

}


[Custom Class]


public class Data
{
private string numConta = "";
private double adeb = 0;
private double acred = 0;


public string NumConta
{
get
{
return numConta;
}
set
{
numConta = value;

}
}


public double Adeb
{
get
{
return adeb;
}
set
{
adeb = Math.Round(value,0);
}
}

public double Acred
{
get
{
return acred;
}
set
{
acred = Math.Round(value,0);
}
}

}


Best Regards,
Ricardo Figueira
http://rbfigueira.net




rbfigueira
9/2/2005 3:53:02 AM
Jeffrey,
Thanks, again for the explanation ;p

[quoted text, click to view]
v-jetan NO[at]SPAM online.microsoft.com (
9/2/2005 6:58:09 AM
Hi Ricardo,

Thanks for your post.

Yes, this is by design. Winform databinding is a 2 way databinding, so when
we changed the values in DataGrid UI side, Winform DataGrid will
automatically push the value into the underlying datasource.

However, in your scenario, the datasource of DataGrid is not the
SortedList, it is the new created ArrayList. Also, if we see ArrayList
constructor, we will see that DataGrid.DataSource= new ArrayList(h.Values)
will copy the elements in h.Values into the ArrayList. So the data in the
ArrayList is changed, but original SortedList will not reflect the change
at all.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
v-jetan NO[at]SPAM online.microsoft.com (
9/5/2005 12:00:00 AM
Hi Ricardo,

I am glad my reply makes sense to you. If you need further help, please
feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
AddThis Social Bookmark Button