Accessing a BoundColumn Value Programmatically

This live demo illustrates how to access the value for a particular BoundColumn for a particular DataGrid row programmatically. If you click on one of the "Click Me" links, the values of the two BoundColumns will be displayed in a Label Web control...


Click MeAn Extensive Examination of the DataGrid Web Control: Part 1This article, by Scott Mitchell, examines the very basics of the DataGrid, illustrating how to bind data to a DataGrid. (This article complements Chapter 1 of ASP.NET Data Web Controls Kick Start.)
Click MeDisplaying Records in a DataGrid in Random OrderThis article, by Scott Mitchell, examines how to display the results of a SQL query in random order in a DataGrid.
Click MeAdding a New Record to the DataGrid in the FooterThis article, by John Sanborn, looks at how to use the DataGrid's Footer row as a means to add a new record to the DataGrid's underlying data store.
Click MeXML, the DataSet, and a DataGridThis article, by Scott Mitchell, examines how to display XML data in a DataGrid. Specifically, it shows using the DataSet's ReadXml() method to populate a DataSet with data from an XML store.
Click MeDataGrids, DataSets, and XML StringsThis article, by Anthony Hart, looks at how to allow a user to view data in either one of two formats: as an XML string or in a DataGrid.


Source Code

<%@ Page Language="VB" %>
<script runat="server">

    Sub Page_Load(sender as Object, e as EventArgs)
        If Not Page.IsPostBack then
           dgArticles.DataSource = GetArticles()
           dgArticles.DataBind()
        End If
    End Sub
    
    
         Function GetArticles() As System.Data.SqlClient.SqlDataReader
             Dim connectionString As String = "connection string..."
             Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString)
    
             Dim queryString As String = "SELECT TOP 5 ArticleID, [Articles].[Title], [Articles].[URL], [Articles].[dateAuthored], [Articles"& _
    "].[Comments], [Articles].[ClickThroughs] FROM [Articles]"
             Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
    
             sqlConnection.Open
             Dim dataReader As System.Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    
             Return dataReader
         End Function


     Sub DisplayBoundColumnValues(sender as Object, e as DataGridCommandEventArgs)
       Dim bc1 as String = e.Item.Cells(1).Text
       Dim bc2 as String = e.Item.Cells(2).Text
       
       lblBCValues.Text = "<b>Value of first BoundColumn</b><br />" & bc1 & _
                     "<p><b>Value of second BoundColumn</b><br />" & bc2 & "<p><hr><p>"
     End Sub

</script>

    <form runat="server">
        <asp:label runat="server" id="lblBCValues" />
        <p>
            <asp:DataGrid id="dgArticles" runat="server" AutoGenerateColumns="False"
                  ShowHeader="False">
                <Columns>
                    <asp:ButtonColumn Text="Click Me" ></asp:ButtonColumn>
                    <asp:BoundColumn DataField="Title"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Comments"></asp:BoundColumn>
                </Columns>
            </asp:DataGrid>            
        </p>
    </form>
    

[Return to the FAQ...]