GridView SelectedIndexChanged question

D

David C

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
 
G

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

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.
 
D

David C

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

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.
 
G

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.

You mean, in the following line is always false?

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

David C

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
 
G

Guest

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"
 
D

David C

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.
 
G

Guest

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").

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

David C

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").

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
 
G

Guest

>
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
 
D

David C

>
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
 
G

Guest

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
 

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,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top