how to change value in gridview

kahvedzic
kahvedzic used Ask the Experts™
on
Have a gridview with invisible columns (using for tooltip text), in invisible cells can be values X or NULL. I want to change for X to be + and for NULL to have -. This is the code-behind.
Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
           e.Row.Cells(0).ToolTip = "abc" + ":" + " " + e.Row.DataItem(4) + Chr(13) + "abc" + ":" + " " + e.Row.DataItem(5) + Chr(13) + "abc" + ":" + " " + e.Row.DataItem(6) + Chr(13) + "abc" + ":" + " " + e.Row.DataItem(7) + Chr(13) + "abc" + ":" + " " + e.Row.DataItem(8) + Chr(13) + "abc" + ":" + " " + e.Row.DataItem(9) + Chr(13) + "abc" + ":" + " " + e.Row.DataItem(10) + Chr(13) + "abc" + ":" + " " + e.Row.DataItem(11) + Chr(13) + "abc" + ":" + " " + e.Row.DataItem(12)

            End If
    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Not really clear. You want to replace X with + and Null with - or do you want to return + when X and - when Null?

Author

Commented:
data is loading from sql table and default values are X and NULL, I want to have in gridview + for X and - for NULL. Not to replace anything in sql table, just to load in gridview.
Most Valuable Expert 2012
Top Expert 2014

Commented:
I think you can add checks like below in the above function

If IsDBNull(e.Row.DataItem(columnindex)) Then
   e.Row.DataItem(columnindex) = "-"
ElseIf e.Row.DataItem(columnindex) = "X"
   e.Row.DataItem(columnindex) = "+"
End If
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Author

Commented:
@CodeCruiser
this part of code I should also add in RowDataBound event?
Most Valuable Expert 2012
Top Expert 2014

Commented:
Yes you can try to add these to rowdatabound event.

Author

Commented:
Add to rowdatabound event and now have this message Object variable or With block variable not set. Also tried only this part of code:

If e.Row.DataItem(columnindex) = "X"
    e.Row.DataItem(columnindex) = "+"
End If

and again same message.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Change this code

If e.Row.DataItem(columnindex) = "X"
   e.Row.DataItem(columnindex) = "+"
End If

to
If e.Row.Cells(columnindex).Text = "X"
   e.Row.Cells(columnindex).Text = "+"
End If

Author

Commented:
now my code behind looks like this and I still have X and empty cells.

Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            If IsDBNull(e.Row.Cells(4)) Then
                e.Row.Cells(4).Text = "-"
            ElseIf e.Row.Cells(4).Text = "X" Then
                e.Row.Cells(4).Text = "+"
            End If

            e.Row.Cells(0).ToolTip = "a" + ":" + " " + e.Row.DataItem(4) + Chr(13) + "b" + ":" + " " + e.Row.DataItem(5) + Chr(13) + "c" + ":" + " " + e.Row.DataItem(6) + Chr(13) + "d" + ":" + " " + e.Row.DataItem(7) + Chr(13) + "e" + ":" + " " + e.Row.DataItem(8) + Chr(13) + "f" + ":" + " " + e.Row.DataItem(9) + Chr(13) + "g" + ":" + " " + e.Row.DataItem(10) + Chr(13) + "h" + ":" + " " + e.Row.DataItem(11) + Chr(13) + "i" + ":" + " " + e.Row.DataItem(12)

        End If

    End Sub

Open in new window

Most Valuable Expert 2012
Top Expert 2014

Commented:
Which lines execute if you step through the code?

Author

Commented:
it executes every line except those two:

e.Row.Cells(4).Text = "-"
 e.Row.Cells(4).Text = "+"

Most Valuable Expert 2012
Top Expert 2014
Commented:
So this line returns false even when the value is X?
ElseIf e.Row.Cells(4).Text = "X"

Point on e.Row.Cells(4).Text to see what it actually has so it would help understand. Also the indices in .NET start at 0 so index 4 is column 5.

Author

Commented:
e.Row.Cells(4).Text.Trim() = "X" worked. Actual value in sql table is "X   ". Now works fine. Thanks. Column index is OK I know it starts from 0.

Thanks once more.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Glad that it worked :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial