DataGrid - 3 problems

(1)
I was going through the example at http://msdn2.microsoft.com/en-us/library/ms229684.aspx

I fixed the small problem with the EditView constructor with these 2 lines:

if (drView["Discontinued"] is DBNull) drView["Discontinued"] = false;
cbxDiscontinued.DataBindings.Add("Checked", CurrentBindingSource, "Discontinued");

 


However, the application works fine for most part, except when I tried to add a new record a second time. It seems that after the AddNew call, the current item of the productsBindingSource somehow points to the first item in the list instead of the new item. This leaves the EditView dialog editing the first item, instead of the new item. I tried calling MoveLast immediately after AddNew, but to no avail.

(2)
In the "Smart Task menu" of a datagrid, there is a "Generate Data Forms". However, I discovered that the generated code will give compilation errors. I have to fix them by amending the constructor of the generated EditViewDialog form to take in a BindingSource parameter. Could anyone confirm this I'm using VS.Net 2005 Professional v8.0.50727.42.

(3)
In a separate project, I wanted to try out the master-detail acceleration feature. I dragged an Order table from Data Sources and its related OrderDetail table to the form. I was able to hide columns in the Order datagrid (by setting zero widths), but not in the OrderDetail datagrid. Am I missing something

Thanks for helping.



Answer this question

DataGrid - 3 problems

  • Fran Garcia

    Thanks for helping. I've fixed my problems through some trial-and-error.

    I added these three lines to the end of menuItem1_Click and menuItem2_Click of the main form to dispose of the dialog object and to rebind the datagrid:

    EditViewDialog.Dispose();
    dataGrid1.DataSource = null;
    dataGrid1.DataSource = productsBindingSource;

     


    And miraculously, it worked.

    As for my other problem (3), a simple rebinding will hide the columns (whose widths I have set to zero in the designer):

    masterDataGrid.DataSource = null;
    masterDataGrid.DataSource = masterBindingSource;
    childDataGrid.DataSource = null;
    childDataGrid.DataSource = fk_master_childBindingSource;

     


    Seems that rebinding is the cure-all for most datagrid problems, at least for me. :)


  • bubixMister

    Any quick workaround for (1) (other than the obvious straightforward but tedious way of not binding the controls in the dialog.)

    Thanks for any insight.


  • mariascandella

    Did you remove duplicated 
         CurrentBindingSource = bsource;
         InitializeComponent();
         CurrentBindingSource = bsource;
         InitializeComponent();
    in the beginnng of EditView constructor Works for me like this (with your other fix of course and a few typos corrected). Overall I'd say this sample needs some editing


  • Gusi

    Yes I did (before I posted here). Did you try clicking on "New" a second time For my case, adding a second item actually causes the topmost item to be overwritten.

    I tried printing the position of current in the EditView constructor:

    CurrentBindingSource = bindingSource;
    MessageBox.Show(CurrentBindingSource.Count + " " + CurrentBindingSource.IndexOf(CurrentBindingSource.Current));
    InitializeComponent();

     


    ... and as expected of the problem, on the first "New", the index of Current is correct, but on the second "New", the index is zero.



  • TurboDoc

    I did use New menu twice successfully. In my case the active item in the data grid was the last one and would move down to a (null) item before the edit form shows

  • DataGrid - 3 problems