根据某些条件更改网格视图值

时间:2019-06-10 09:16:57

标签: asp.net vb.net gridview

我有一个Grid view控件,其中有几列是从用户填写的表单中填充的。在表单中,某些值不是必需的,如果用户未选择它们,则将其显示为“ select”在网格视图中。加载网格视图时,如何将其值更改为“ none”之类的相对值。换句话说,如果该列包含“选择”,则将其更改为“无”

这是我后面的代码,用于填充我的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

1 个答案:

答案 0 :(得分:1)

首先,我不会将“ Select”保存到DB,保存空值(DBNull.Value)更有意义。然后,在您的select语句中,您可以使用函数IsNull(yourField,'None')。编辑:如果网格中有Null值,则还可以使用DataGridView的属性:

Me.GridViewAll.RowsDefaultCellStyle.NullValue = "None"

但是,如果您要像显示的那样进行操作,则非常接近-您只需将新的String值保存到DataTable:

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