error: Specified argument was out of the range of valid values. Parameter name: index

D

Dave

Hi,

I want to change the color of two fields (a checkbox and a 'normal
stringfield') of my gridview depending of their value. The checkbox works
with the code below.
My question is: what do i have to take as object for the 'normal
stringfield'? I tried with 'textbox', with 'label' ... but each time i get
the error:
"Specified argument was out of the range of valid values. Parameter name:
index"

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
Dim cb As New CheckBox
Dim a As ???
cb = e.Row.Cells(6).Controls(0)
a = e.Row.Cells(5).Controls(0)
If cb.Checked Then
e.Row.Cells(0).BackColor = Drawing.Color.Red
e.Row.Cells(6).BackColor = Drawing.Color.Red
End If

if a.text (or matbe a.value) = "fat" then
e.Row.Cells(6).BackColor = Drawing.Color.Green
End If
End Sub

Thanks
Dave
 
W

Winista

In case of text, the cell itself has the text value set on it. You can skip
the part where you are looking for control at index 0.
 
W

Winista

PArdon my VB :) I am not good at it..

You are trying to get the text in that cell. And then use that to check the
condition. You can do something like.

Dim aText As String


aText = e.Row.Cells(5).Text

if aText = "fat" then

Try it out...
 
D

Dave

Thanks for replying,but forgive me if i don't understand what you mean ...

I still don't know how to finish the line:
dim as as ... (textbox ??)
and when doing that, i still get the mentioned error ...

If possible, can you show me the right code ...
Thanks
 
D

Dave

Thanks, it works now ...

But i still don't undertsand why this statement works: (independantely of
property 'text')
Dim cb As New CheckBox
cb = e.Row.Cells(6).Controls(0)

but not this: (gives mentioned error)
Dim a As new textbox
a = e.Row.Cells(5).Controls(0)

Thansk again
 
J

Jesse Houwing

Dave said:
Thanks, it works now ...

But i still don't undertsand why this statement works: (independantely of
property 'text')
Dim cb As New CheckBox
cb = e.Row.Cells(6).Controls(0)

but not this: (gives mentioned error)
Dim a As new textbox
a = e.Row.Cells(5).Controls(0)

I guess it would be because Cells(5) contains no controls, so
Controls(0) is out of range.

Jesse
 
D

Dave

Hi Jesse,

The gridview contains 7 fields (i can see them on screen). The last is a
checkbox and the first six are textfield.
So e.Row.Cells(6).Controls(0) is the checkbox, but why are
e.Row.Cells(0).Controls(0) till e.Row.Cells(5).Controls(0) out of range ???

Maybe are these texrfield not 'textbox'? But then, what are they? How can i
know that?
Thanks
 
J

Jesse Houwing

Dave said:
Hi Jesse,

The gridview contains 7 fields (i can see them on screen). The last is a
checkbox and the first six are textfield.
So e.Row.Cells(6).Controls(0) is the checkbox, but why are
e.Row.Cells(0).Controls(0) till e.Row.Cells(5).Controls(0) out of range ???

Maybe are these texrfield not 'textbox'? But then, what are they? How can i
know that?

Maybe they're just plain HTML input boxes. Or other plain text. Can you
post the corresponding code from the ASPX?

Jesse
 
D

Dave

Hi,

Here below is the ASPX code:
as you can see, they are 6 'Boundfield' and 1 checkfield, but when doing
this:
dim c as new BoundField
c=e.Row.Cells(0).Controls(0)

i get the error:"type 'System.Web.UI.Control' cannot be converted to
'System.Web.UI.WebControls.BoundField'.

I also tried with"Htmlinputtext", "tablecell" ...

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\my.mdb"
ProviderName="System.Data.OleDb" SelectCommand="SELECT fld1,
fld2,fld3,fld4,fld5,fld6, fld7FROM mytable"></asp:SqlDataSource>

<asp:GridView ID="GridView1" runat="server"
<Columns>
<asp:BoundField DataField="fld1"
HeaderText="Computer"
SortExpression="fld1" />
<asp:BoundField DataField="fld2"
HeaderText="Lok"
SortExpression="fld2" />
<asp:BoundField DataField="fld3"
HeaderText="Oms"
SortExpression="fld3" />
<asp:BoundField DataField="fld4"
HeaderText="Type"
SortExpression="fld4" />
<asp:BoundField DataField="fld5"
HeaderText="Soft"
SortExpression="fld5" />
<asp:BoundField DataField="fld6
HeaderText="Thin"
SortExpression="fld6" />
<asp:CheckBoxField DataField="fld7"
HeaderText="Def"
SortExpression="fld7" />
</Columns>
</asp:GridView>
 
J

Jesse Houwing

* Dave wrote, On 9-7-2006 10:12:
Hi,

Here below is the ASPX code:
as you can see, they are 6 'Boundfield' and 1 checkfield, but when doing
this:
dim c as new BoundField
c=e.Row.Cells(0).Controls(0)

i get the error:"type 'System.Web.UI.Control' cannot be converted to
'System.Web.UI.WebControls.BoundField'.

I also tried with"Htmlinputtext", "tablecell" ...

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\my.mdb"
ProviderName="System.Data.OleDb" SelectCommand="SELECT fld1,
fld2,fld3,fld4,fld5,fld6, fld7FROM mytable"></asp:SqlDataSource>

<asp:GridView ID="GridView1" runat="server"
<Columns>
<asp:BoundField DataField="fld1"
HeaderText="Computer"
SortExpression="fld1" />
<asp:BoundField DataField="fld2"
HeaderText="Lok"
SortExpression="fld2" />
<asp:BoundField DataField="fld3"
HeaderText="Oms"
SortExpression="fld3" />
<asp:BoundField DataField="fld4"
HeaderText="Type"
SortExpression="fld4" />
<asp:BoundField DataField="fld5"
HeaderText="Soft"
SortExpression="fld5" />
<asp:BoundField DataField="fld6
HeaderText="Thin"
SortExpression="fld6" />
<asp:CheckBoxField DataField="fld7"
HeaderText="Def"
SortExpression="fld7" />
</Columns>
</asp:GridView>

The last field contains a checkbox which will be represented as a
control, the others are just bound text which will not create a control.
To get to that text from your code-behind you should use

e.Row.Cells(0).Text

A control will only be added if the row is in edit mode (then it wil
contain a textbox control).

Should you always want a conrtol in there for some reason, use a
templatecolumn and then you can put in any controls you'd want.

Jesse
 
D

dave

Thanks!


Jesse Houwing said:
* Dave wrote, On 9-7-2006 10:12:

The last field contains a checkbox which will be represented as a
control, the others are just bound text which will not create a control.
To get to that text from your code-behind you should use

e.Row.Cells(0).Text

A control will only be added if the row is in edit mode (then it wil
contain a textbox control).

Should you always want a conrtol in there for some reason, use a
templatecolumn and then you can put in any controls you'd want.

Jesse
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top