PDA

Click to See Complete Forum and Search --> : [RESOLVED] [2.0] DataGridView


shakti5385
Apr 17th, 2007, 11:46 PM
Hi all :wave:
I want to fill the DataGridView Manully and set the column value.
This I does in VB.NET

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

This is in C#
but

int i = 0;
for (i = 0 ; i< dataTable.Rows.Count;i++)
{
this.gridTable.Rows.Add(1);
//I am Confused Here
}

Thanks

jmcilhinney
Apr 18th, 2007, 12:04 AM
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:gridTable.Item(0, I).Value = DataTable.Rows(I)(2).ToStringbecomes this: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.

shakti5385
Apr 18th, 2007, 12:59 AM
Thanks sir
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.

jmcilhinney
Apr 18th, 2007, 01:25 AM
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:gridTable.Item(0, I)or omitting the property name and indexing the object directly:gridTable(0, I)In C# you don't have the choice. Only the second option is available:gridTable[0, I]

shakti5385
Apr 18th, 2007, 05:25 AM
Thanks Sir