Re: Datagridview.datasource triggers RowsAdded event twice

Tech-Archive recommends: Speed Up your PC by fixing your registry



The dataTable is populated as start of the program using a xml-file and
MSDataSetGenerator. Later when entering a specific view I will bind a
datagridview to that table. Why I need to use the RowsAdded event is that I
need to populate a combox column that will have items that are unique for
ech cell in that column.

So as I understand it the Bindingsource will react when I populate the
dataTable which happens at start of the program. Later when entering a
specific view I bind these values to a gridView and react to the RowsAdded
to be able to populate the combobox for each row. So the data i get from the
dataTable shall be the SelectedItem for the combobox.

/Patrik


"ClayB" <clayb@xxxxxxxxxxxxxx> wrote in message
news:1171975410.342184.154860@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
You could wrap your DataTable in a BindingSource object and then
listen to the BindingSource.ListChanged event (or the AddingNew event)
for ListChangedType.ItemAdded to catch (only once) the adding of new
rows to the DataTable. (Or, you could access the CurrecnyManager.List
for this bindingcontext and listen to its ListChanged event, but this
you could not do from the designer).

Here is the code changes in you sample to show this (I think you can
also do it from the designer as well.)

private BindingSource src;

...//in Form_Load
dataGridView1.DataSource = this.src;

...//in FillDataSet()
DataTable table = new DataTable("table1");
this.src = new BindingSource();
src.DataSource = table;
src.ListChanged += new
ListChangedEventHandler(src_ListChanged);


void src_ListChanged(object sender, ListChangedEventArgs e)
{
if (e.ListChangedType == ListChangedType.ItemAdded)
{
Console.WriteLine(string.Format("added {0}", e.NewIndex));
}
}

===================
Clay Burch
Syncfusion, Inc.



.



Relevant Pages

  • Re: whats a neater way of writing this simple code...
    ... I think you could bind an array vs. the array list that would be easier to ... > All I can seem to find on the net is to populate an ArrayList with the ... hear freedom comin" ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: When/Where to load data in DataList Update Template
    ... populate a textbox in the edit template with data, but I found I could do it ... in the template itself by binding it there. ... when I tried to bind it using the properties window, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: combo box problem
    ... You can use combo.ControlSource="yourVar" to bind it to a variable. ... OTOH, I believe you may not need an extra variable?, because you can ... I have a combo box that I populate from a table called ... > capture the users selection and store into a memory variable. ...
    (microsoft.public.fox.vfp.forms)
  • Re: Programmatically populate grid
    ... I bound the dataviewgrid to the dataset, but no records show in my grid. ... code to populate table ... then bind to that. ... database in order to use them. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: DataGridView Cascading DataGridViewComboBoxColumn
    ... I think the simplest way is to make use of the data relation between the ... parent table and the child table to implement the above function. ... The next step is to use BindingSource. ... The last step is to populate the two DataGridViewComboBoxColumn with the ...
    (microsoft.public.dotnet.framework.windowsforms.controls)