سؤال

Using VWD 2010 Express, ASP.NET, VB.NET.

I have a gridview that is bound to a database table, it contains 10 columns, one of which is hidden.

I would like to set a background colour of the rows which have a due date (the last column, index 8 in gridview) of anything before the current day.

So far I have this: (defined under the RowCreated event)

Dim i As Integer = 8
If e.Row.RowType = DataControlRowType.DataRow Then
    If e.Row.Cells(i).Text <= Date.Now.ToString Then
        e.Row.Cells(i).ForeColor = System.Drawing.Color.Red
    End If
End If

However, problem, I receive an error:

"Specified argument was out of the range of valid values, parameter: index"

All the objects in the gridview are boundfields. The only index which doesn't return this error is index 0, any advice?

When using index 0, it only colours the very first cell in each of the rows, and I don't think that's dependant on the date at all, as it seems to colour them all red.

هل كانت مفيدة؟

المحلول

Managed to find a fix, instead of using the index of the column, I used the column name, like so:

    If e.Row.DataItem("DueDate") < Date.Now.ToString Then
        e.Row.BackColor = Drawing.Color.Red
    End If

NOTE: I declared this in RowDataBound, not in RowCreated.

Works perfectly, left this here for anyone else with a similar problem. Indexing doesn't seem very accurate in this instance.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top