GridView SelectedIndexChanged question

Discussion in 'ASP .Net' started by David C, Oct 24, 2009.

  1. David C

    David C Guest

    I have a GridView that I want to show or hide LinkButtons that exist outside
    of the GridView based on a data column in the GridView when that row is
    selected. I have the code below for the SelectedIndexChanged event but it
    always falls through to the False condition. The data column named
    VoidCheck is a SQL Server bit data type. Can someone help me spot the
    problem? Thanks in advance.

    David

    Protected Sub gvVendorChecks_SelectedIndexChanged(ByVal sender As
    Object, ByVal e As System.EventArgs) Handles
    gvVendorChecks.SelectedIndexChanged
    ' Get the currently selected row using the SelectedRow property.
    Dim row As GridViewRow = gvVendorChecks.SelectedRow

    If row.RowState = DataControlRowState.Selected Then
    Dim bolvoid As Boolean =
    Convert.ToBoolean(DataBinder.Eval(row.DataItem, "VoidCheck"))
    If bolvoid = True Then
    ' voided check selected so show the remove void button
    LBtnRemoveVoid.Visible = True
    LBtnVoidCheck.Visible = False
    Else
    LBtnRemoveVoid.Visible = False
    LBtnVoidCheck.Visible = True
    End If
    End If
    End Sub
    David C, Oct 24, 2009
    #1
    1. Advertising

  2. On Oct 24, 2:59 pm, "David C" <> wrote:
    > I have a GridView that I want to show or hide LinkButtons that exist outside
    > of the GridView based on a data column in the GridView when that row is
    > selected.  I have the code below for the SelectedIndexChanged event but it
    > always falls through to the False condition.  The data column named
    > VoidCheck is a SQL Server bit data type.  Can someone help me spot the
    > problem?  Thanks in advance.
    >
    > David
    >
    >     Protected Sub gvVendorChecks_SelectedIndexChanged(ByVal sender As
    > Object, ByVal e As System.EventArgs) Handles
    > gvVendorChecks.SelectedIndexChanged
    >         ' Get the currently selected row using the SelectedRow property.
    >         Dim row As GridViewRow = gvVendorChecks.SelectedRow
    >
    >         If row.RowState = DataControlRowState.Selected Then
    >             Dim bolvoid As Boolean =
    > Convert.ToBoolean(DataBinder.Eval(row.DataItem, "VoidCheck"))
    >             If bolvoid = True Then
    >                 ' voided check selected so show the remove void button
    >                 LBtnRemoveVoid.Visible = True
    >                 LBtnVoidCheck.Visible = False
    >             Else
    >                 LBtnRemoveVoid.Visible = False
    >                 LBtnVoidCheck.Visible = True
    >             End If
    >         End If
    >     End Sub


    The RowType property is a bitwise combination of the values and not a
    boolean. Either remove

    ** If row.RowState = DataControlRowState.Selected Then **

    because it makes no sense, or replace it by

    ** If (row.RowState And DataControlRowState.Edit) > 0 Then **

    Hope this helps.
    Alexey Smirnov, Oct 24, 2009
    #2
    1. Advertising

  3. David C

    David C Guest

    I changed the first If statement but for some reason the boolean check
    always returns false even though I can see the checkbox is checked on some
    of the rows I select. Can you see anything wrong with the boolean statement
    with bolvoid setting? Thanks.

    David

    "Alexey Smirnov" <> wrote in message
    news:...
    On Oct 24, 2:59 pm, "David C" <> wrote:
    > I have a GridView that I want to show or hide LinkButtons that exist
    > outside
    > of the GridView based on a data column in the GridView when that row is
    > selected. I have the code below for the SelectedIndexChanged event but it
    > always falls through to the False condition. The data column named
    > VoidCheck is a SQL Server bit data type. Can someone help me spot the
    > problem? Thanks in advance.
    >
    > David
    >
    > Protected Sub gvVendorChecks_SelectedIndexChanged(ByVal sender As
    > Object, ByVal e As System.EventArgs) Handles
    > gvVendorChecks.SelectedIndexChanged
    > ' Get the currently selected row using the SelectedRow property.
    > Dim row As GridViewRow = gvVendorChecks.SelectedRow
    >
    > If row.RowState = DataControlRowState.Selected Then
    > Dim bolvoid As Boolean =
    > Convert.ToBoolean(DataBinder.Eval(row.DataItem, "VoidCheck"))
    > If bolvoid = True Then
    > ' voided check selected so show the remove void button
    > LBtnRemoveVoid.Visible = True
    > LBtnVoidCheck.Visible = False
    > Else
    > LBtnRemoveVoid.Visible = False
    > LBtnVoidCheck.Visible = True
    > End If
    > End If
    > End Sub


    The RowType property is a bitwise combination of the values and not a
    boolean. Either remove

    ** If row.RowState = DataControlRowState.Selected Then **

    because it makes no sense, or replace it by

    ** If (row.RowState And DataControlRowState.Edit) > 0 Then **

    Hope this helps.
    David C, Oct 24, 2009
    #3
  4. On Oct 24, 10:12 pm, "David C" <> wrote:
    > I changed the first If statement but for some reason the boolean check
    > always returns false even though I can see the checkbox is checked on some
    > of the rows I select. Can you see anything wrong with the boolean statement
    > with bolvoid setting?  Thanks.
    >


    You mean, in the following line is always false?

    Dim bolvoid As Boolean =
    Convert.ToBoolean(DataBinder.Eval(row.DataItem, "VoidCheck"))
    Alexey Smirnov, Oct 24, 2009
    #4
  5. David C

    David C Guest

    "Alexey Smirnov" <> wrote in message
    news:...
    On Oct 24, 10:12 pm, "David C" <> wrote:
    > I changed the first If statement but for some reason the boolean check
    > always returns false even though I can see the checkbox is checked on some
    > of the rows I select. Can you see anything wrong with the boolean
    > statement
    > with bolvoid setting? Thanks.
    >


    You mean, in the following line is always false?

    Dim bolvoid As Boolean =
    Convert.ToBoolean(DataBinder.Eval(row.DataItem, "VoidCheck"))

    Yes. And when I check the actual database record the VoidCheck column = 1
    David C, Oct 25, 2009
    #5
  6. On Oct 25, 1:36 pm, "David C" <> wrote:
    > Yes. And when I check the actual database record the VoidCheck column = 1


    Before that line add

    Response.Write ("VoidCheck=" & DataBinder.Eval(row.DataItem,
    "VoidCheck"))

    and see what it would return. I suppose it will be "VoidCheck=0"
    Alexey Smirnov, Oct 25, 2009
    #6
  7. David C

    David C Guest

    "Alexey Smirnov" <> wrote in message
    news:...
    On Oct 25, 1:36 pm, "David C" <> wrote:
    > Yes. And when I check the actual database record the VoidCheck column = 1


    Before that line add

    Response.Write ("VoidCheck=" & DataBinder.Eval(row.DataItem,
    "VoidCheck"))

    and see what it would return. I suppose it will be "VoidCheck=0"

    Yes, that is what it returned. I even tried converting to integer and it
    still returns 0.
    David C, Oct 25, 2009
    #7
  8. On Oct 25, 8:11 pm, "David C" <> wrote:
    > "Alexey Smirnov" <> wrote in message
    >
    > news:...
    > On Oct 25, 1:36 pm, "David C" <> wrote:
    >
    > > Yes. And when I check the actual database record the VoidCheck column = 1

    >
    > Before that line add
    >
    > Response.Write ("VoidCheck=" & DataBinder.Eval(row.DataItem,
    > "VoidCheck"))
    >
    > and see what it would return. I suppose it will be "VoidCheck=0"
    >
    > Yes, that is what it returned.  I even tried converting to integer and it
    > still returns 0.


    Ok, but this means there is no error. Convert.ToBoolean(0) equals to
    false and your [if] statement will not be executed. You should check
    an ID or a DataKey of the row you check in DataBinder.Eval
    (row.DataItem,"VoidCheck").

    >> I check the actual database record the VoidCheck column = 1


    It's another record, and not the one you have in the line with
    DataBinder.Eval(row.DataItem,"VoidCheck")
    Alexey Smirnov, Oct 26, 2009
    #8
  9. David C

    David C Guest

    "Alexey Smirnov" <> wrote in message
    news:...
    On Oct 25, 8:11 pm, "David C" <> wrote:
    > "Alexey Smirnov" <> wrote in message
    >
    > news:...
    > On Oct 25, 1:36 pm, "David C" <> wrote:
    >
    > > Yes. And when I check the actual database record the VoidCheck column =
    > > 1

    >
    > Before that line add
    >
    > Response.Write ("VoidCheck=" & DataBinder.Eval(row.DataItem,
    > "VoidCheck"))
    >
    > and see what it would return. I suppose it will be "VoidCheck=0"
    >
    > Yes, that is what it returned. I even tried converting to integer and it
    > still returns 0.


    Ok, but this means there is no error. Convert.ToBoolean(0) equals to
    false and your [if] statement will not be executed. You should check
    an ID or a DataKey of the row you check in DataBinder.Eval
    (row.DataItem,"VoidCheck").

    >> I check the actual database record the VoidCheck column = 1


    It's another record, and not the one you have in the line with
    DataBinder.Eval(row.DataItem,"VoidCheck")

    I thought that the following puts you on the selected row. What am I
    missing?

    If row.RowState = DataControlRowState.Selected Then

    -David
    David C, Oct 26, 2009
    #9
  10. On Oct 26, 2:35 pm, "David C" <> wrote:
    >
    > I thought that the following puts you on the selected row.  What am I
    > missing?
    >
    > If row.RowState = DataControlRowState.Selected Then
    >


    David,

    The RowState can be a bitwise combination of the 4 following values:

    DataControlRowState.Normal (=0)
    DataControlRowState.Alternate (=1)
    DataControlRowState.Selected (=2)
    DataControlRowState.Edit (=4)
    DataControlRowState.Insert (=8)

    This means that when you selected an alternating row, then RowState is
    equal to (2+1) and in this case

    row.RowState (2+1) <> DataControlRowState.Selected (2)

    Please look at the examples on MSDN

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.rowstate.aspx

    You need to check selection in the following way:

    If (e.Row.RowState And DataControlRowState.Selected) > 0 Then

    OR like this

    If e.Row.RowState = (DataControlRowState.Normal Xor
    DataControlRowState.Selected) OrElse e.Row.RowState =
    (DataControlRowState.Alternate Xor DataControlRowState.Selected) Then

    Hope this helps
    Alexey Smirnov, Oct 26, 2009
    #10
  11. David C

    David C Guest

    "Alexey Smirnov" <> wrote in message
    news:...
    On Oct 26, 2:35 pm, "David C" <> wrote:
    >
    > I thought that the following puts you on the selected row. What am I
    > missing?
    >
    > If row.RowState = DataControlRowState.Selected Then
    >


    David,

    The RowState can be a bitwise combination of the 4 following values:

    DataControlRowState.Normal (=0)
    DataControlRowState.Alternate (=1)
    DataControlRowState.Selected (=2)
    DataControlRowState.Edit (=4)
    DataControlRowState.Insert (=8)

    This means that when you selected an alternating row, then RowState is
    equal to (2+1) and in this case

    row.RowState (2+1) <> DataControlRowState.Selected (2)

    Please look at the examples on MSDN

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.rowstate.aspx

    You need to check selection in the following way:

    If (e.Row.RowState And DataControlRowState.Selected) > 0 Then

    OR like this

    If e.Row.RowState = (DataControlRowState.Normal Xor
    DataControlRowState.Selected) OrElse e.Row.RowState =
    (DataControlRowState.Alternate Xor DataControlRowState.Selected) Then

    Hope this helps


    Still does not work. I assumed that when you used e.Row.RowState that you
    were referring to RowDatabound event because e.row does not work in
    selectedindexchanged event. So I put the following code and it never fires
    because I traced it. What can be wrong? Thanks.

    David

    Protected Sub gvVendorChecks_RowDataBound(ByVal sender As Object, ByVal
    e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    gvVendorChecks.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
    If e.Row.RowState = (DataControlRowState.Normal Xor
    DataControlRowState.Selected) OrElse e.Row.RowState =
    (DataControlRowState.Alternate Xor DataControlRowState.Selected) Then
    Dim isvoid As Boolean =
    Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "VoidCheck"))
    If isvoid = True Then
    ' voided check selected so show the remove void button
    LBtnRemoveVoid.CssClass = "Show"
    LBtnVoidCheck.CssClass = "Hide"
    Else
    LBtnRemoveVoid.CssClass = "Hide"
    LBtnVoidCheck.CssClass = "Show"
    Dim tb As TextBox = Page.Master.FindControl("txtMsg")
    tb.Text = "Value in VoidCheck is " & isvoid.ToString
    End If
    End If
    End If
    End Sub
    David C, Oct 26, 2009
    #11
  12. On Oct 26, 8:25 pm, "David C" <> wrote:
    > "Alexey Smirnov" <> wrote in message
    >
    > news:...
    > On Oct 26, 2:35 pm, "David C" <> wrote:
    >  >
    >
    > > I thought that the following puts you on the selected row. What am I
    > > missing?

    >
    > > If row.RowState = DataControlRowState.Selected Then

    >
    > David,
    >
    > The RowState can be a bitwise combination of the 4 following values:
    >
    > DataControlRowState.Normal (=0)
    > DataControlRowState.Alternate (=1)
    > DataControlRowState.Selected (=2)
    > DataControlRowState.Edit (=4)
    > DataControlRowState.Insert (=8)
    >
    > This means that when you selected an alternating row, then RowState is
    > equal to (2+1) and in this case
    >
    > row.RowState (2+1) <> DataControlRowState.Selected (2)
    >
    > Please look at the examples on MSDN
    >
    > http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gri...
    >
    > You need to check selection in the following way:
    >
    > If (e.Row.RowState And DataControlRowState.Selected) > 0 Then
    >
    > OR like this
    >
    > If e.Row.RowState = (DataControlRowState.Normal Xor
    > DataControlRowState.Selected) OrElse e.Row.RowState =
    > (DataControlRowState.Alternate Xor DataControlRowState.Selected) Then
    >
    > Hope this helps
    >
    > Still does not work.  I assumed that when you used e.Row.RowState that you
    > were referring to RowDatabound event because e.row does not work in
    > selectedindexchanged event.  So I put the following code and it never fires
    > because I traced it.  What can be wrong?  Thanks.
    >
    > David
    >
    >     Protected Sub gvVendorChecks_RowDataBound(ByVal sender As Object, ByVal
    > e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    > gvVendorChecks.RowDataBound
    >         If e.Row.RowType = DataControlRowType.DataRow Then
    >             If e.Row.RowState = (DataControlRowState.Normal Xor
    > DataControlRowState.Selected) OrElse e.Row.RowState =
    > (DataControlRowState.Alternate Xor DataControlRowState.Selected) Then
    >                 Dim isvoid As Boolean =
    > Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "VoidCheck"))
    >                 If isvoid = True Then
    >                     ' voided check selected so show the remove void button
    >                     LBtnRemoveVoid.CssClass = "Show"
    >                     LBtnVoidCheck.CssClass = "Hide"
    >                 Else
    >                     LBtnRemoveVoid.CssClass = "Hide"
    >                     LBtnVoidCheck.CssClass = "Show"
    >                     Dim tb As TextBox = Page.Master..FindControl("txtMsg")
    >                     tb.Text = "Value in VoidCheck is " & isvoid.ToString
    >                 End If
    >             End If
    >         End If
    >     End Sub


    Ok, I see the problem now. The DataItem property (you used for
    DataBinder.Eval(e.Row.DataItem, "VoidCheck")) is only available during
    and after the RowDataBound event of a GridView control and it's not
    available in SelectedIndexChanged. So, if you need to use it in
    SelectedIndexChanged and VoidCheck column is presented in the GridView
    control, you may access it as

    row.Cells(X).Text

    where X is id of the column.

    If you don't want to show that column in the GridView then put it to
    the GridView to DataKeys, so that you can access it without the actual
    data source row.

    Here's an example:

    1) Define VoidCheck as a datakey, if you have a single datakey
    already, you can add VoidCheck as a second key column.

    Dim _dataKeyNames As String() = {"VoidId", "VoidCheck"}
    gvVendorChecks.DataKeyNames = _dataKeyNames
    gvVendorChecks.DataSource = cmd.ExecuteReader()
    gvVendorChecks.DataBind()

    2) Now you will be able to get VoidCheck out of DataKeys collection

    Protected Sub gvVendorChecks_SelectedIndexChanged(...)

    ' Get the currently selected row using the SelectedRow property.
    Dim row As GridViewRow = gvVendorChecks.SelectedRow
    Dim bolvoid As Boolean = gvVendorChecks.DataKeys(row.DataItemIndex)
    ("VoidCheck")

    If bolvoid Then
    ' voided check selected so show the remove void button
    LBtnRemoveVoid.Visible = True
    LBtnVoidCheck.Visible = False
    Else
    LBtnRemoveVoid.Visible = False
    LBtnVoidCheck.Visible = True
    End If

    End Sub

    Let me know if this is working now for you
    Alexey Smirnov, Oct 26, 2009
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QVQ=?=
    Replies:
    1
    Views:
    4,453
    =?Utf-8?B?QVQ=?=
    Apr 1, 2005
  2. sck10
    Replies:
    1
    Views:
    7,292
    Steven Cheng[MSFT]
    Apr 3, 2006
  3. =?Utf-8?B?LU1U?=

    Raising SelectedIndexChanged event in a gridview

    =?Utf-8?B?LU1U?=, Jun 5, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    16,356
    =?Utf-8?B?SmF5?=
    Jun 6, 2006
  4. graphicsxp
    Replies:
    0
    Views:
    470
    graphicsxp
    Jul 11, 2006
  5. Alexey Smirnov

    Re: GridView SelectedIndexChanged question

    Alexey Smirnov, Oct 24, 2009, in forum: ASP .Net
    Replies:
    0
    Views:
    537
    Alexey Smirnov
    Oct 24, 2009
Loading...

Share This Page