Tevin
Tevin

Reputation: 21

Change Grid view value based on some condition

I have a Grid view control with a few columns that is populated from a form that the user fills in. In the form some values are not mandatory and if the user does not select them, then it is displayed as "select" in the Grid view. How do i go about changing that value to something for relative like "none" when i load the Grid view. In other words , if that column contains "select" then change it to "None"

This is my code behind to populate my Gridview

Private Sub LoadGridAll()

    txtSearchEmployee.Text = ""
    Try
        Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("XXX").ConnectionString)

            Using cmd As New SqlCommand("SELECT EmployeeCodes.Code, EmployeeCodes.FirstName , EmployeeCodes.LastName , EmployeeCodes.EmployeeID , CostCentre , ExaminationType.ExaminationTypeName  , PhysicalExam.PhysicalExamName , ExaminationType.ExaminationTypeName  , PhysicalExam.PhysicalExamName , Audiogram.AudiogramName , AudiogramRec.AudiogramRecName,LungFunction.LungFunctionName,ChestResults,ECGResult,DrugScreeningResult,BloodGlucoseResult,GGTResult,LeftEyeDayNight,RightEyeDayNight,LeftEyeCorrDayNight,RightEyeCorrDayNight,VisualFieldLeftDayNight,VisualFieldRightDayNight,ColourVisionDayNight,DeptPerceptionDayNight,OptometristYesNo,Outcome.Name , OutcomeRecommendations.OutcomeRecommendationsName,OtherProblems,Notes,DateTested,NextDueDate FROM  MedicalResults,EmployeeCodes,ExaminationType,PhysicalExam,Audiogram,AudiogramRec,LungFunction,Outcome,OutcomeRecommendations WHERE MedicalResults.EmployeeID = EmployeeCodes.EmployeeID AND ExaminationType.ExaminationTypeID = MedicalResults.ExaminationTypeID AND PhysicalExam.PhysicalExamID = MedicalResults.PhysicalExamType AND Audiogram.AudiogramID = MedicalResults.AudiogramID AND MedicalResults.AudiogramRecID = AudiogramRec.AudiogramRecID AND LungFunction.LungFunctionID = MedicalResults.LungFunctionID AND OutcomeRecommendations.OutcomeRecommendationsID = MedicalResults.OutcomeRecommendationsID AND Outcome.OutcomeID = MedicalResults.OutcomeID ", conn)

                conn.Open()

                Dim sda As New SqlDataAdapter(cmd)
                Dim dt As New DataTable()
                sda.Fill(dt)

                For Each row As DataRow In dt.Rows
                    Dim strDetail As Object
                    strDetail = row.Item(10)

                    If strDetail = "Select" Then

                        strDetail = "None"

                    End If
                Next row

                GridViewAll.DataSource = dt
                GridViewAll.DataBind()                   


                conn.Close()

            End Using

        End Using

    Catch ex As Exception

    End Try

End Sub

Upvotes: 0

Views: 337

Answers (1)

okkko
okkko

Reputation: 1070

First of all, I wouldn't save "Select" to DB, it would make more sense to save a null value (DBNull.Value). Then in your select statement you could use a function IsNull(yourField,'None'). Edit: if you have Null values in grid, you could also use DataGridView's property:

Me.GridViewAll.RowsDefaultCellStyle.NullValue = "None"

However, if you want to do it like you've shown, you're very close - you just need to save the new String value to DataTable:

row.Item(10) = "None" 'strDetail 

Upvotes: 1

Related Questions