Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 5 tables in my database. I want to bind all tables one by one to the grid view by clicking the 5 buttons in frontend. My doubt is every table data fields are different.
How to rectify this problem?
Posted

On every button click get the data from back end, put those data either in a DataTable or in a DataSet. Bind the DataTable or DataSet to your GridView...Done.


C#
GridView1.DataSource = dt; // this will be either DataSet or DataTable which is binding every time
GridView1.DataBind();
 
Share this answer
 
v2
Why 5 buttons? These tables must be related in some way through primary keys and foreign keys otherwise there is no meaning or purpose to put them all in one gridview. Once you have established the relationship in these table, use the appropriate SQL JOIN[^] to select data from related rows of these table, then use one of these [^] to populate the gridview.
 
Share this answer
 
v2
Hi,

ASPX File
ASP.NET
<div>
            <asp:Button Text="Table 1" runat="server" ID="btnTable1" OnClick="btnTable1_Click" /><br />
            <asp:Button Text="Table 2" runat="server" ID="btnTable2" OnClick="btnTable2_Click" /><br />
            <asp:Button Text="Table 3" runat="server" ID="btnTable3" OnClick="btnTable3_Click" /><br />
            <asp:Button Text="Table 4" runat="server" ID="btnTable4" OnClick="btnTable4_Click" /><br />
            <asp:Button Text="Table 5" runat="server" ID="btnTable5" OnClick="btnTable5_Click" /><br />

        </div>
        <div>
            <asp:GridView runat="server" ID="gridDisplay" AutoGenerateColumns="true"></asp:GridView>
        </div>


CS File

protected void btnTable1_Click(object sender, EventArgs e)
        {
            DataTable dtTable = new DataTable();

            dtTable = DBHelper.GetDataTable("Table1");

            gridDisplay.DataSource = dtTable;
            gridDisplay.DataBind();

        }

        protected void btnTable2_Click(object sender, EventArgs e)
        {
            DataTable dtTable = new DataTable();

            dtTable = DBHelper.GetDataTable("Table2");

            gridDisplay.DataSource = dtTable;
            gridDisplay.DataBind();

        }

        protected void btnTable3_Click(object sender, EventArgs e)
        {
            DataTable dtTable = new DataTable();

            dtTable = DBHelper.GetDataTable("Table3");

            gridDisplay.DataSource = dtTable;
            gridDisplay.DataBind();

        }

        protected void btnTable3_Click(object sender, EventArgs e)
        {
            DataTable dtTable = new DataTable();

            dtTable = DBHelper.GetDataTable("Table3");

            gridDisplay.DataSource = dtTable;
            gridDisplay.DataBind();

        }

        protected void btnTable4_Click(object sender, EventArgs e)
        {
            DataTable dtTable = new DataTable();

            dtTable = DBHelper.GetDataTable("Table4");

            gridDisplay.DataSource = dtTable;
            gridDisplay.DataBind();

        }

        protected void btnTable5_Click(object sender, EventArgs e)
        {
            DataTable dtTable = new DataTable();

            dtTable = DBHelper.GetDataTable("Table5");

            gridDisplay.DataSource = dtTable;
            gridDisplay.DataBind();

        }


Above code will help you.
 
Share this answer
 
v2
Comments
Kanaparthi Sureshma Reddy 15-Jun-15 1:21am    
While writing DBHelper, I am getting the below error "The name DBHelper doesn't exist in the current context".
How to solve this error?
Dusara Maulik 16-Jun-15 3:32am    
You can get DBHelper from following link.

http://code4forever.blogspot.in/2012/05/dbhelper-class-for-c.html
http://www.codeproject.com/Articles/23853/DbHelper-for-DotNet-using-DbProviderFactory
http://www.codeproject.com/Articles/17962/Very-Lightweight-Data-Access-Layer-in-C-and-NET

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900