且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用VB编程从ASP .NET的GridView中的数据库中突出显示具有特定值的单元格?

更新时间:2023-10-07 14:50:58

好,这样做的方法是在几乎所有情况下都使用行数据"绑定事件.

此事件针对每个"事件触发在渲染过程中排成一行.因此,您可以在渲染的行和最终输出之间进行切换.

所以,说我有这个标记:

 < asp:GridView ID =" GridView1"runat =服务器"AutoGenerateColumns ="false"<列>< asp:BoundField runat =服务器"DataField ="HotelName";HeaderText =" HotelName"SortExpression =" HotelName"/>< asp:BoundField DataField =城市";HeaderText =城市"SortExpression =城市"/>< asp:TemplateField HeaderText ="酒店名2">< ItemTemplate>< asp:TextBox ID =" HotelName2"runat =服务器"Text ='<%#Eval("HotelName")%>'></asp:TextBox></ItemTemplate></asp:TemplateField>< asp:TemplateField HeaderText =" City2">< ItemTemplate>< asp:TextBox ID ="City2"runat =服务器"Text ='<%#Eval(城市")%>'></asp:TextBox></ItemTemplate></asp:TemplateField></列></asp:GridView> 

现在,我故意将常见的边界域"包括在内.而且我还包括了模板字段.

我在这篇博文中都包括了这两个内容,因为您到达和更改一个的方式与另一个不同.

所以,我想展示如何更改两种类型.

仅供参考:模板字段-您使用FindControl(按ID名称)

非模板化-您使用单元格(N)-数字从0到n(您不能在此处使用名称!!).

因此,如上所述,在我的载入页面事件中,我有以下内容:

 受保护的子Page_Load(ByVal发送者作为对象,ByVal e作为System.EventArgs)处理Me.Load如果IsPostBack = False,则将strSQL暗淡为字符串strSQL ="SELECT ID,FirstName,LastName,City from tblHotels ORDER BY HotelName"使用cmdSQL作为新的SqlCommand(strSQL,New SqlConnection(My.Settings.Test3))cmdSQL.Connection.Open()将Dim rData作为新数据表rData.Load(cmdSQL.ExecuteReader)GridView1.DataSource = rDataGridView1.DataBind()最终使用万一结束子 

我们得到了:

到目前为止,很简单-我们看到了网格(带有这些边界域,然后带有两个模板化字段(如我所指出的-通常您有一个或另一个-甚至可能是两种类型).

好的,因此,对于City ="Edmonton",我们要说天蓝色".颜色

对于City ="Calgary",我们要说"Plum"

因此,我们可以使用RowDataBound事件.

因此,我们的代码是这样的:

 受保护的子GridView1_RowDataBound(作为对象发送,作为GridViewRowEventArgs发送)处理GridView1.RowDataBound如果e.Row.RowType = DataControlRowType.DataRow然后Dim txtBoxCity为TextBox = e.Row.FindControl("City2")如果txtBoxCity.Text ="Edmonton",然后txtBoxCity.BackColor = Drawing.Color.SkyBlue万一如果txtBoxCity.Text =卡尔加里",然后txtBoxCity.BackColor = Drawing.Color.Plum万一Dim dF如TableCell = e.Row.Cells(1)如果dF.Text ="Edmonton",则返回"F".然后dF.BackColor = Drawing.Color.SkyBlue万一如果dF.Text =卡尔加里",则为.然后dF.BackColor = Drawing.Color.Plum万一万一结束子 

现在如上所述,由于我同时显示了两种控件,因此我在这里有两个代码.

现在,当它运行上面的内容时?我们得到这个:

请记住,当我们把手放在那一行上时,然后是控件吗?

您不仅限于设置背景.您可能会说为每个城市显示一张自定义图片.也许您在两个控件上进行了一些数学计算,然后将结果推到第三行或控件中.

因此,课程"这是因为行数据绑定事件使您可以动手操作该行-然后您几乎可以***地通过设置颜色,按不同的值进行操作-几乎可以完成任何您想做的事情.

I have a GridView on an ASP .NET Web Page with templatefields. I am using VB as the programming language. I want to Highlight the Cells red that show a value 'not submitted' and green if it shows 'submitted' Is there a way to do this? Thanks

Ok, the approach to do this is to in near all cases use the "row data" bound event.

This event fires for "each" row during the rendering process. Thus you can get between the rendered row, and the final output.

So, say I have this Markup:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>

            <asp:BoundField  runat="server" DataField="HotelName" HeaderText="HotelName" SortExpression="HotelName" />
            <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />

            <asp:TemplateField HeaderText="Hotel Name2">
                <ItemTemplate>
                  <asp:TextBox ID="HotelName2" runat="server" Text='<%# Eval("HotelName") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="City2">
                <ItemTemplate>
                  <asp:TextBox ID="City2" runat="server" Text='<%# Eval("City") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>


        </Columns>
    </asp:GridView>

Now, on purpose I included the common "boundfield" and I ALSO included template fields.

I included BOTH in this post since how you get at and change one is different then the other.

So, I wanted to show how both types can be changed.

FYI: Templated fields - you use FindControl (by id name)

Non Templated - you use cells(N) - by a number 0 to n (you can't use name here!!!).

So, with above, in my on-load page event, I have this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If IsPostBack = False Then
     dim strSQL as string
     strSQL = "SELECT ID, FirstName, LastName,City FROM tblHotels ORDER BY HotelName"

     Using cmdSQL As New SqlCommand(strSQL, New SqlConnection(My.Settings.Test3))

            cmdSQL.Connection.Open()
            Dim rData As New DataTable
            rData.Load(cmdSQL.ExecuteReader)

            GridView1.DataSource = rData
            GridView1.DataBind()

        End Using

    End If

End Sub

And we get this:

So far, simple - and we see our grid (with those boundfields and then the same with two templated fields (as I noted - often you have one or the other - or even maybe both types).

Ok, so, for City = "Edmonton", we want say "sky blue" color

And for City = "Calgary", we want say "Plum"

So, we can use the RowDataBound event.

Our code is thus this:

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim txtBoxCity As TextBox = e.Row.FindControl("City2")

        If txtBoxCity.Text = "Edmonton" Then
            txtBoxCity.BackColor = Drawing.Color.SkyBlue
        End If

        If txtBoxCity.Text = "Calgary" Then
            txtBoxCity.BackColor = Drawing.Color.Plum
        End If

        Dim dF As TableCell = e.Row.Cells(1)

        If dF.Text = "Edmonton" Then
            dF.BackColor = Drawing.Color.SkyBlue
        End If
        If dF.Text = "Calgary" Then
            dF.BackColor = Drawing.Color.Plum
        End If



    End If
End Sub

Now as noted, i have DOUBLE the code here - since I am showing both types of controls.

And now, when it run the above? We get this:

Keep in mind that when we get our hands on that row, and THEN a control?

You not limited to just setting say the background. You might say show a custom picture for each city. Maybe you do some math calc on two controls and shove the result into a 3rd row or control.

So the "lesson" here is that the row data bound event lets you get your hands on that row - and then you are near free to do what you want from setting colors, shoving in different values - quite much whatever you want.