none
How to access current data row while populating DataGridView? RRS feed

  • Question

  • I have the following table adapters. I'm populating a DataGridView from the ReqSecretarial table adapter. Instead of showing the foreign key StaffID, I'd like to show the StaffName. It's a read-only DGV, by the way, for display only.

    I tried creating a query that joined the two tables, creating a results table that included the staff name. This worked, but then the Designer wouldn't create Insert, Delete, and Update methods.

    Now I'm trying this. In my DataGridView, I have set Virtual Mode true and added an unbound column. This causes the DataGridView to fire the CellValueNeeded event when it hits the unbound column while it's populating, which is what I needed. But the way I was getting the staff name from the staff id was by pre-loading the Staff table into an array and then getting the element index from the hidden StaffID column in the DataGridView. That doesn't seem efficient.

    Is there a way, as the DataGridView is populating, to get the current data row being processed, so that I can use GetChildRows? The relation is right there. It seems that we should be able to use it.

    This is googled code that didn't work. In both cases, I keep getting the same row, the first one displayed in the DGV.

    CurrencyManager cm = (CurrencyManager)this.BindingContext[this.dgvReqSecretarial.DataSource, this.dgvReqSecretarial.DataMember];
    DataRow dr = ((DataRowView)cm.Current).Row;
    DataRow[] child = dr.GetChildRows(dsSecretarialCust.Relations["ReqSecretarial_Staff"]);
    
    var drow = (DataRowView)this.reqSecretarialBindingSource.Current;
    DataRow[] child = drow.Row.GetChildRows(dsSecretarialCust.Relations["ReqSecretarial_Staff"]);
    Much appreciated if someone knows the proper way to do this!

    Wednesday, April 17, 2019 8:34 PM

Answers

  • Hi Robert,

    >> How to access current data row while populating DataGridView

    Could you elaborate on the meaning of this sentence? Are you sure to be "while" instead of "after"? It would be better if you can explain it in details.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Robert in SF Sunday, September 8, 2019 5:16 PM
    Thursday, April 18, 2019 6:01 AM
    Moderator

All replies

  • Hi Robert,

    >> How to access current data row while populating DataGridView

    Could you elaborate on the meaning of this sentence? Are you sure to be "while" instead of "after"? It would be better if you can explain it in details.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Robert in SF Sunday, September 8, 2019 5:16 PM
    Thursday, April 18, 2019 6:01 AM
    Moderator
  • Sure, Kyle. A DataGridView has many rows, and it's possible to intercept execution at each row while it is populating. However, I now realize that the query to the database has finished before the DGV is populated, so it's not quite right to talk about current row. As the DGV is populated, I'm sure there is a pointer to the current row in the data source, but I can't seem to find it -- but it's ok. It doesn't matter.

    I have realized that my approach is wrong. I'm trying to use the relation as a lookup, but it's really only for parent-child relationships. I will revisit the architecture of what I'm trying to do, and will post back.

    Saturday, April 20, 2019 2:58 AM