Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display table from a datatable or dataset

I am doing a web project in VS 2010 and I am quite new to programming. I was just wondering how to display the data from a datatable or dataset in the web page once button_click event is fired/clicked?

Please help!

like image 421
Ash Avatar asked Feb 03 '26 14:02

Ash


1 Answers

Here is a quick code sample to get you started:

ASPX

    <asp:Button runat="server" Text="Click Me" ID="btnSubmit" 
        onclick="btnSubmit_Click" />
    <asp:GridView runat="server" AutoGenerateColumns="false" ID="GridView1">
        <Columns>
            <asp:BoundField DataField="ID" />
            <asp:BoundField DataField="productName" HeaderText="Product Name" />
            <asp:BoundField DataField="unitCost" HeaderText="Cost"  dataformatstring="${0:F2}" />
        </Columns>
    </asp:GridView>

C#

        public void BindData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("productName", typeof(string));
            dt.Columns.Add("unitCost", typeof(decimal));

            dt.Rows.Add(1, "Pineapple", 1.45);
            dt.Rows.Add(3, "Apple", 1.45);
            dt.Rows.Add(17, "Orange", 6.33);
            dt.Rows.Add(23, "Pear", 17.32);
            dt.Rows.Add(27, "Banana", 12.20);

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            BindData();
        }

There are so many different ways to go about what you are trying to do but the above will give you a quick introduction.

like image 123
NakedBrunch Avatar answered Feb 06 '26 04:02

NakedBrunch