Home > Enterprise >  How can I populate a DataGridView with texts?
How can I populate a DataGridView with texts?

Time:10-03

I have a datagridview called DGVpayStub I added 4 columns: Income, Rate, Hours, CurrentTotal

Now, I need to fill my first row with some variables and texts, so it looks like this:

INCOME      | RATE | HOURS | CURRENT TOTAL
gross wages |  0   |   0   |    salary

where 'gross wages', '0' and '0' are texts and salary is a variable

I've Tried this:

        DGVpayStub[1, 1].Value = "Gross Wages";
        DGVpayStub[1, 2].Value = "0";
        DGVpayStub[1, 3].Value = "0";
        DGVpayStub[1, 4].Value = salary; 

But I get this error message:

Index was out of range. Must be non-negative and less than the size of the collection parameter name:index

Which I could not find a solution that suits my problem

How can I fill my dataGridView from code?

Edit: Solution

this worked perfectly:

    DGVpayStub[0, 0].Value = "Gross Wages";
    DGVpayStub[1, 0].Value = "0";
    DGVpayStub[2, 0].Value = "0";
    DGVpayStub[3, 0].Value = salary; 

CodePudding user response:

You have 4 columns and index starts with 0, so maximum value of j will be 3 in this case. Where k is the column index.

You need to access like [1,0] [1,1] [1,2][1,3].

Here I’m considering that you are reading the correct row.

  • Related