【问题标题】:Change Grid view value based on some condition根据某些条件更改网格视图值
【发布时间】:2019-10-24 16:28:48
【问题描述】:

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

这是我用来填充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

【问题讨论】:

    标签: asp.net vb.net gridview


    【解决方案1】:

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

    Me.GridViewAll.RowsDefaultCellStyle.NullValue = "None"
    

    然而,如果你想像你展示的那样做,你已经很接近了——你只需要将新的 String 值保存到 DataTable:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多