[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1048
  • Last Modified:

How do you access the value of a BoundColumn in a DataGrid in VB.NET?

I'm trying to access the value of a BoundColumn in my datagrid and write an If statement around it in my Sub (VB.Net) called from a button. I'm just having trouble figuring out what syntax it wants. I've tried everything I can think of and everything Intellisense suggests.

The subroutine is called by a button that toggles between "Show All" and "Active Only". It works, but I just need to have it access the BoundColumn where the ID="Status". If the value is "Not Active" and the user just clicked "Active Only", it will turn the whole row to visible=false.


Sub showButtonSwitch(ByVal Source As Object, ByVal e As EventArgs)
        If Me.ShowActiveOrAll.Text = "Show All" Then
            'show all
            '============================================================
            '      My Question is - How to write this If Statement:
            '============================================================
            'If the boundcolumn where id="Status" in the DataGrid Me.dg (where dg is a  DataGrid ID) = "Not Active"
                    'Don't display the row
            'End If
            '============================================================
            'switch button
            ShowActiveOrAll.Text = "Show All"
 
        Else
            'show all
            Me.dgOutput.Visible = True
            'switch button
            ShowActiveOrAll.Text = "Active Only"
        End If
    End Sub
0
pippen
Asked:
pippen
2 Solutions
 
Rajar AhmedConsultantCommented:
try this
Protected Sub dgBound_ItemDataBound(sender As Object, e As DataGridItemEventArgs) Handles dgBound.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Response.Write(e.Item.Cells(0).Text.ToString())
            If e.Item.Cells(0).Text.ToString() = "Status" Then 'Index 0 is the column you want to check of your bound column in datagrid

            End If

        End If
    End Sub

Open in new window

0
 
pippenAuthor Commented:
I changed dgBound to dgOutput because that's the id of my datagrid.

I get this error:

Method 'Protected Sub dgOutput_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.DataGridItemEventArgs)' does not have the same signature as delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs)'.

Is this because I'm trying to call the Sub from a button control?
0
 
pippenAuthor Commented:
I'm calling it from onClick.

<div style="width:85%;text-align:right" ><asp:Button ID="ShowActiveOrAll" runat="server" Text="Active Only"  OnClick="dgOutput_ItemDataBound" /></div>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Rajar AhmedConsultantCommented:
Nope, Itemdatabound is the event of datagrid not the event of button .

Please check the below example which i worked on previous post

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DgBound.aspx.vb" Inherits="Vb_DgBound" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DataGrid ID="dgBound" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundColumn DataField="id" HeaderText="ID"></asp:BoundColumn>
            <asp:BoundColumn DataField="Status" HeaderText="Name"></asp:BoundColumn>
        </Columns>

    </asp:DataGrid>
    </div>
    </form>
</body>
</html>

Codebehind

Imports System.Data

Partial Class Vb_DgBound
    Inherits System.Web.UI.Page

    
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim ds As DataSet = getDataSet()
        If Not IsPostBack Then
            dgBound.DataSource = ds
            dgBound.DataBind()
        End If
    End Sub
    Protected Sub dgBound_ItemDataBound(sender As Object, e As DataGridItemEventArgs) Handles dgBound.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            If e.Item.Cells(1).Text.ToString() = "Active" Then
                Response.Write("Status : Active Region<br>")
            ElseIf e.Item.Cells(1).Text.ToString() = "Not Active" Then
                Response.Write("Status : Not Active Region<br>")
            End If
        End If
    End Sub
#Region "Temp ds"
    Public Shared Function getDataSet() As DataSet
        Dim ds As New DataSet
        Dim dt As DataTable
        Dim dr As DataRow
        Dim idCoulumn As DataColumn
        Dim nameCoulumn As DataColumn
        Dim i As Integer

        dt = New DataTable()
        idCoulumn = New DataColumn("ID", Type.GetType("System.Int32"))
        nameCoulumn = New DataColumn("Status", Type.GetType("System.String"))

        dt.Columns.Add(idCoulumn)
        dt.Columns.Add(nameCoulumn)

        dr = dt.NewRow()
        dr("ID") = 1
        dr("Status") = "Active"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("ID") = 2
        dr("Status") = "Not Active"
        dt.Rows.Add(dr)

        ds.Tables.Add(dt)
        Return ds
    End Function
#End Region
End Class

Open in new window

0
 
Rajar AhmedConsultantCommented:
You can show the active/not active rows using the below method just reloading the datagrid with filter on front end or at back end .
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div style="width:85%;text-align:left" >
            <asp:Button ID="ShowActiveOrAll" runat="server" Text="Active Only"  OnClick="ShowActiveOrAll_Click" /></div><br />

    <asp:GridView ID="dgBound" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="id" HeaderText="ID"></asp:BoundField>
            <asp:BoundField DataField="Status" HeaderText="Name"></asp:BoundField>
        </Columns>

    </asp:GridView>
    </div>
    </form>
</body>
</html>

Codebehind:
Imports System.Data

Partial Class Vb_DgBound
    Inherits System.Web.UI.Page

    
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim ds As DataSet = getDataSet()
        If Not IsPostBack Then
            dgBound.DataSource = ds
            dgBound.DataBind()
        End If
    End Sub
    'Protected Sub dgBound_ItemDataBound(sender As Object, e As DataGridItemEventArgs) Handles dgBound.ItemDataBound
    '    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

    '        If e.Item.Cells(1).Text.ToString() = "Active" Then
    '            Response.Write("Status : Active Region<br>")
    '        ElseIf e.Item.Cells(1).Text.ToString() = "Not Active" Then
    '            Response.Write("Status : Not Active Region<br>")
    '        End If
    '    End If
    'End Sub
#Region "Temp ds"
    Public Shared Function getDataSet() As DataSet
        Dim ds As New DataSet
        Dim dt As DataTable
        Dim dr As DataRow
        Dim idCoulumn As DataColumn
        Dim nameCoulumn As DataColumn
        Dim i As Integer

        dt = New DataTable()
        idCoulumn = New DataColumn("ID", Type.GetType("System.Int32"))
        nameCoulumn = New DataColumn("Status", Type.GetType("System.String"))

        dt.Columns.Add(idCoulumn)
        dt.Columns.Add(nameCoulumn)

        dr = dt.NewRow()
        dr("ID") = 1
        dr("Status") = "Active"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("ID") = 2
        dr("Status") = "Not Active"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("ID") = 3
        dr("Status") = "Active"
        dt.Rows.Add(dr)


        dr = dt.NewRow()
        dr("ID") = 4
        dr("Status") = "Not Active"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("ID") = 5
        dr("Status") = "Active"
        dt.Rows.Add(dr)


        dr = dt.NewRow()
        dr("ID") = 6
        dr("Status") = "Not Active"
        dt.Rows.Add(dr)
        dr = dt.NewRow()
        dr("ID") = 7
        dr("Status") = "Active"
        dt.Rows.Add(dr)


        dr = dt.NewRow()
        dr("ID") = 8
        dr("Status") = "Not Active"
        dt.Rows.Add(dr)

        ds.Tables.Add(dt)
        Return ds
    End Function
#End Region

    Protected Sub ShowActiveOrAll_Click(sender As Object, e As EventArgs)
        Dim ds As DataSet = getDataSet()
        Dim btnText As String = ""
        Dim btnStatus As String = ""
        If ShowActiveOrAll.Text = "Active Only" Then
            btnText = "Not Active Only"
            btnStatus = "Active"
        Else
            btnText = "Active Only"
            btnStatus = "Not Active"
        End If
        
        Dim view As New DataView(ds.Tables(0))
        view.RowFilter = "Status = '" & btnStatus & "'"
        
        dgBound.DataSource = view
        dgBound.DataBind()

        ShowActiveOrAll.Text = btnText
    End Sub
End Class

Open in new window

0
 
pippenAuthor Commented:
Wow. Thanks for writing that, but my I guess I didn't explain it very well. It's actually pulling the "Active" status from the database. There is a field named "DriverStatus" that has the value of "A" or "D" (deleted)

What I'm looking for is on the BoundColumn where the DataField="DriverStatus", when the value of that is "A" (coming from the database), I want to show the row. When the value is "D" I want to hide the row.

Here is what the datagrid looks like on the front end (simplified somewhat)

<asp:datagrid id="dgOutput" runat="server" AutoGenerateColumns="False" >
<Columns>
     <asp:BoundColumn DataField="DriverStatus" HeaderText="Status"></asp:BoundColumn>
</Columns>
</asp:datagrid>

The datagrid gets populated with a function in the code behind that calls a stored procedure.
0
 
Alan WarrenCommented:
Hi zolla,

Re:
When the value is "D" I want to hide the row.
You could modify your SQL to exclude rows where the DriverStatus field = 'D'
Select * from YourTable where DriverStatus <> 'D'

Open in new window

Alan
0
 
CodeCruiserCommented:
What is your grid bound to? DataSource or DataTable?
0
 
Rajar AhmedConsultantCommented:
What I'm looking for is on the BoundColumn
This is something you got to understand, on every postback you need to bind the datasource control ie datagrid.Since on postback you probably dont have datasource in the datagrid and so you cant do much without reassigning the dataset .
Conclusion:[2 possibilities]
 1. Just create a filter in storeprocedure with where condition .
If ShowActiveOrAll.Text = "Active Only" Then
            btnText = "Not Active Only"
            btnStatus = "A" 
        Else
            btnText = "Active Only"
            btnStatus = "D"
        End If
       'Pass btnStatus to the storeprocure which can filter dataset accordingly 

Open in new window

 Or
 2.With your currect storeprocedure that is returning a dataset and do a filter in the dataset .
Meeran
0
 
pippenAuthor Commented:
Got it working. I had several things wrong, due to some things I didn't know about .Net, one of which was the need to bind the  datagrid to a data source.

The solution was to create a Click page event on the button control and a ItemDataBound on the datagrid.

Here is the code:

FRONT END BUTTON:
<asp:Button ID="btnToggle" runat="server" Text="Active Only" />

IN THE CODE BEHIND:

Private Sub btnToggle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnToggle.Click
        If Me.btnToggle.Text = "Active Only" Then
            Me.btnToggle.Text = "Show All"
        Else
            Me.btnToggle.Text = "Active Only"
        End If
        GetDrivers()
    End Sub
 
Private Sub xxOutput_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgOutput.ItemDataBound
        Try
            If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
                If e.Item.Cells(8).Text.Trim() <> "A" AndAlso (Me.btnToggle.Text = "Show All") Then
                    e.Item.Visible = False
                End If
            End If
        Catch ex As Exception
            logError("xxOutput_ItemDataBound", ex.Message, ex.StackTrace)
        End Try
    End Sub

Private Sub logError(ByVal process As String, ByVal errorMessage As String, ByVal stackTrace As String)
        Try
            Dim er As New xxxxxxxxUserControls.ErrorLog
            er.logError("xxxxxx.aspx.vb", Process, errorMessage, StackTrace)
        Catch ex As Exception

        End Try
End Sub
0
 
pippenAuthor Commented:
Oops. Here is one more piece of the code --- the datagrid (details and private info removed of course), on the front end .aspx file.

Notice that the index of e.Item.Cells(8).Text.Trim() corresponds to the location of the item I was looking for in the columns: index = 8, and it is 8th in the list of columns (0 based index). The index has nothing to do with the location in the database. It's all about the datagrid.

<asp:datagrid id="dgOutput" runat="server" >
                                                            
<Columns>
<asp:BoundColumn DataTextField="xxxxxx" ....></asp:BoundColumn >
<asp:BoundColumn DataTextField="xxxxxx" ....></asp:BoundColumn >
<asp:BoundColumn DataTextField="xxxxxx" ....></asp:BoundColumn >
<asp:BoundColumn DataTextField="xxxxxx" ....></asp:BoundColumn >
<asp:BoundColumn DataTextField="xxxxxx" ....></asp:BoundColumn >
<asp:BoundColumn DataTextField="xxxxxx" ....></asp:BoundColumn >
<asp:BoundColumn DataTextField="xxxxxx" ....></asp:BoundColumn >
<asp:BoundColumn DataTextField="xxxxxx" ....></asp:BoundColumn >
<asp:BoundColumn DataTextField="Status" ....></asp:BoundColumn >
</Columns>
</asp:datagrid>
0
 
pippenAuthor Commented:
My colleague helped me find the solution and I wanted to share the code I used. meeran03 made a suggestion that was key, so I awarded him all the points.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now