Results 1 to 7 of 7

Thread: [RESOLVED] Easy data view syntax question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    134

    Resolved [RESOLVED] Easy data view syntax question

    Code:
    DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
            DataView dv = new DataView();
            da.SelectCommand = propertiesCmd;
            da.Fill(ds, "Solution Properties");
            dv = ds.Tables["Solution Properties"].DefaultView;
            
            dv.RowFilter = "[type key] = '-1689071302'";
            String propertyStr = dv.Table.Columns["Value"].ToString();
    All I want to do is get the data from a filtered data view in a string. See the last two lines of code. Thats what Im trying but propertyStr="Value". "Value" is the name of the column whos data I want. propertyStr should be something from my dataview, not just the title of the column. Whats the correct syntax to accomplish this.
    censored

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

    Re: Easy data view syntax question

    This:
    Code:
    String propertyStr = dv.Table.Columns["Value"].ToString();
    is specifically getting a column from the table. That's not what you want. First of all you have to get a row, then you have to get the value from that row that is in the column you want. There's no need to go via the table for this because the view exposes the row itself, e.g.
    vb.net Code:
    1. For Each row As DataRowView in dv
    2.     MessageBox.Show(row("Value").ToString())
    3. Next row
    That will get the value from the row in the column named "Value".
    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

  3. #3
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Easy data view syntax question

    Code:
    dv.RowFilter = "[type key] = '-1689071302'";
    string propertyStr = dv[0].Row[0].ToString();

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

    Re: Easy data view syntax question

    Oops. Forgot I was in C# land:
    Code:
    foreach (DataRowView row in dv)
    {
        MessageBox.Show(row["Value"].ToString());
    }
    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
    Addicted Member
    Join Date
    Jun 2007
    Posts
    134

    Re: Easy data view syntax question

    Quote Originally Posted by mar_zim
    Code:
    dv.RowFilter = "[type key] = '-1689071302'";
    string propertyStr = dv[0].Row[0].ToString();
    this worked for me perfectly. Thanks!
    censored

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

    Re: [RESOLVED] Easy data view syntax question

    Like I said, there's no need to go via the DataRow when the DataRowView exposes the same data. This:
    Code:
    string propertyStr = dv[0].Row[0].ToString();
    does the same as this:
    Code:
    string propertyStr = dv[0][0].ToString();
    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
    Addicted Member
    Join Date
    Jun 2007
    Posts
    134

    Re: [RESOLVED] Easy data view syntax question

    Quote Originally Posted by jmcilhinney
    Like I said, there's no need to go via the DataRow when the DataRowView exposes the same data. This:
    Code:
    string propertyStr = dv[0].Row[0].ToString();
    does the same as this:
    Code:
    string propertyStr = dv[0][0].ToString();
    i see what your saying. very good. thanks.
    censored

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