c# example - how to get all DataColumn name from DataTable

Using for loop - How to programmatically get all DataColumn name from DataTable in ado.net
UsingForLoopDataColumnListFromDataTable.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Button1_Click(object sender, System.EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.TableName = "Books";

        DataColumn dc1 = new DataColumn();
        dc1.ColumnName = "BookID";
        dc1.DataType = typeof(int);
        dc1.AllowDBNull = false;

        DataColumn dc2 = new DataColumn();
        dc2.ColumnName = "BookName";
        dc2.DataType = typeof(string);
        dc2.MaxLength = 90;

        DataColumn dc3 = new DataColumn();
        dc3.ColumnName = "Author";
        dc3.DataType = typeof(string);
        dc3.MaxLength = 45;

        dt.Columns.AddRange(new DataColumn[] { dc1, dc2,dc3 });

        dt.Rows.Add(new object[] { "1", "Photoshop CS3 Accelerated", "Blues Kim" });
        dt.Rows.Add(new object[] { "2", "Adobe InDesign CS3 One-on-One", "Deke McClelland" });
        dt.Rows.Add(new object[] { "3", "Photoshop CS3 Photo Effects Cookbook", "Tim Shelbourne" });
        dt.Rows.Add(new object[] { "4", "Photoshop Lightroom Adventure", "Mikkel Aaland" });
        
        GridView1.DataSource = dt;
        GridView1.DataBind();

        Label1.Text = "<b><u>Columns in DataTable....</u></b><br />";
        for (int i=0; i< dt.Columns.Count; i++)
        {
            Label1.Text += dt.Columns[i].ColumnName + "<br/ >";
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Using for loop - How to programmatically get all DataColumn name from DataTable in ado.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkBlue; font-style:italic;">
            Using for loop - How to get all DataColumn
            <br /> name from DataTable programmatically in ado.net
        </h2>
        <hr width="500" align="left" color="CornFlowerBlue" />
        <asp:GridView 
            ID="GridView1"
            runat="server"
            BorderColor="Snow"
            ForeColor="Snow"
            Width="525"
            HeaderStyle-BackColor="DeepSkyBlue"
            RowStyle-BackColor="DarkMagenta"
            AlternatingRowStyle-BackColor="Navy"
            >
        </asp:GridView>
        <br />
        <asp:Label
             ID="Label1"
             runat="server"
             Font-Size="X-Large"
             ForeColor="DeepPink"
             >
        </asp:Label>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Populate GridView"
            Height="45"
            Font-Bold="true"
            ForeColor="DarkBlue"
            />
    </div>
    </form>
</body>
</html>






Related asp.net example