Results 1 to 7 of 7

Thread: what am i doing wrong(bindingsource loop)

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Thumbs up what am i doing wrong(bindingsource loop)

    Hi all

    Im trying to loop through spesific coloums of a bindingsource but i am having some trouble

    the code i have so far is

    c# Code:
    1. private void CountLines(BindingSource bs)
    2.         {
    3.             int x = 0;
    4.             foreach (DataRowView IncidentRow in bs)
    5.             {
    6.                 MessageBox.Show(IncidentRow.ToString());
    7.                
    8.                 x++;
    9.             }//foreach (DataRowView invoiceRow in invoicebindingsource)
    10.             MessageBox.Show(x.ToString());
    11.            
    12.         }

    But all i get out is



    so my question is how would i say messagebox.show each summary or each ref num?


    thanks

    Crash

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: what am i doing wrong(bindingsource loop)

    Hi there,
    You need to specify which column in the row you want to view buy its index or name:
    Code:
    MessageBox.Show(IncidentRow.Row["ref_num"].ToString());

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: what am i doing wrong(bindingsource loop)

    bingo



    thanks

    if i were to want to save it back how would that go?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: what am i doing wrong(bindingsource loop)

    Save what back, and back to what?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: what am i doing wrong(bindingsource loop)

    Sorry i was not very spesific

    after i can access the data by row and col if i wanted to make a change to that cell how would i?



    I mocked up some sudo code that might give you an idea of what im trying to figure out.
    c# Code:
    1. private void CountLines(BindingSource bs)
    2.         {
    3.             int x = 0;
    4.             foreach (DataRowView IncidentRow in bs)
    5.             {
    6.                 If(IncidentRow.Row["ref_num"].ToString(); == stringimadeup)
    7.                 {
    8.                     replace incidentrow.row["ref_num"].tostring with "N/A"
    9.                 }
    10.                
    11.                 x++;
    12.             }//foreach (DataRowView invoiceRow in invoicebindingsource)
    13.             MessageBox.Show(x.ToString());
    14.            
    15.         }

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: what am i doing wrong(bindingsource loop)

    DataRowViews are almost the same as a DataRow. They are a view to the data in that row and can be used in almost exactly the same way:
    Code:
    foreach (DataRowView view in myBindingSource)
    {
        if ((string)view["ref_num"] = "some string")
        {
            view["ref_num"] = "N/A";
        }
    }
    Note that if the column already contains string objects then you should cast as type string rather than call the ToString method. Why convert a string to a string? If you're unsure of the type then you should cal ToString to ensure it works in all cases.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: what am i doing wrong(bindingsource loop)

    Quote Originally Posted by jmcilhinney
    DataRowViews are almost the same as a DataRow. They are a view to the data in that row and can be used in almost exactly the same way:
    Code:
    foreach (DataRowView view in myBindingSource)
    {
        if ((string)view["ref_num"] = "some string")
        {
            view["ref_num"] = "N/A";
        }
    }
    Note that if the column already contains string objects then you should cast as type string rather than call the ToString method. Why convert a string to a string? If you're unsure of the type then you should cal ToString to ensure it works in all cases.
    thanks so much ill give it a shot when i get into work tommrow

    i would give you a +rate but i cant give you anymore

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width