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

Discussion in 'ASP .Net Datagrid Control' started by Dave, Jul 7, 2006.

  1. Dave

    Dave Guest

    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
     
    Dave, Jul 7, 2006
    #1
    1. Advertisements

  2. Dave

    Winista Guest

    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.
     
    Winista, Jul 7, 2006
    #2
    1. Advertisements

  3. Dave

    Winista Guest

    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...
     
    Winista, Jul 8, 2006
    #3
  4. Dave

    Dave Guest

    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
     
    Dave, Jul 8, 2006
    #4
  5. Dave

    Dave Guest

    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
     
    Dave, Jul 8, 2006
    #5
  6. I guess it would be because Cells(5) contains no controls, so
    Controls(0) is out of range.

    Jesse
     
    Jesse Houwing, Jul 8, 2006
    #6
  7. Dave

    Dave Guest

    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
     
    Dave, Jul 8, 2006
    #7
  8. Maybe they're just plain HTML input boxes. Or other plain text. Can you
    post the corresponding code from the ASPX?

    Jesse
     
    Jesse Houwing, Jul 8, 2006
    #8
  9. Dave

    Dave Guest

    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>
     
    Dave, Jul 9, 2006
    #9
  10. * 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
     
    Jesse Houwing, Jul 9, 2006
    #10
  11. Dave

    dave Guest

    Thanks!


     
    dave, Jul 9, 2006
    #11
  12. * dave wrote, On 9-7-2006 15:14:
    You're welcome!

    Jesse
     
    Jesse Houwing, Jul 9, 2006
    #12
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.