Results 1 to 5 of 5

Thread: [RESOLVED] [2.0] DataGridView

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [RESOLVED] [2.0] DataGridView

    Hi all
    I want to fill the DataGridView Manully and set the column value.
    This I does in VB.NET


    vb Code:
    1. For I As Integer = 0 To DataTable.Rows.Count - 1 Step I + 1
    2.             gridTable.Rows.Add(1)
    3.             gridTable.Item(0, I).Value = DataTable.Rows(I)(2).ToString
    4.  Next

    This is in C#
    but


    C# Code:
    1. int i = 0;
    2.             for (i = 0 ; i< dataTable.Rows.Count;i++)
    3.             {
    4.                 this.gridTable.Rows.Add(1);
    5.                 //I am Confused Here
    6.             }

    Thanks

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

    Re: [2.0] DataGridView

    The code is exactly the same in C#. The only difference is the colon on the end and the fact that when you index something in C# you use square brackets instead of parentheses like in VB. This:
    vb Code:
    1. gridTable.Item(0, I).Value = DataTable.Rows(I)(2).ToString
    becomes this:
    Code:
    gridTable.Item[0, I].Value = DataTable.Rows[I][2].ToString();
    Note also that you must ALWAYS include the parentheses when calling a method in C#, as I have done with ToString. I think that you should always do so in VB too to differentiate methods from properties. This is enforced in C# but not in VB.
    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

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] DataGridView

    Thanks sir
    C# Code:
    1. this.gridTable.Item[0, i].Value = dataTable.Rows[i][2].ToString();

    But the error saying that System.Windows.Forms.DataGridView' does not contain a definition for 'Item'

    and what that mean

    you must ALWAYS include the parentheses when calling a method in C#, as I have done with ToString. I think that you should always do so in VB too to differentiate methods from properties. This is enforced in C# but not in VB.

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

    Re: [2.0] DataGridView

    Ah, sorry. Missed that. In VB.NET you have default properties. In C# the equivalent are indexers. With a default property in VB you have the choice of specifying the property:
    vb Code:
    1. gridTable.Item(0, I)
    or omitting the property name and indexing the object directly:
    vb Code:
    1. gridTable(0, I)
    In C# you don't have the choice. Only the second option is available:
    Code:
    gridTable[0, I]
    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
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] DataGridView

    Thanks Sir

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