简体   繁体   中英

How to get multiple GridView in same aspx page

I want to put multiple GridViews in same aspx page. I tried but its not working.

Here is the code from cs page

protected void GridView1_DataBound(object sender, EventArgs e)
{
    {
        GridView1.HeaderRow.Cells[0].Text = "AGENT ID";
        GridView1.HeaderRow.Cells[1].Text = "NAME";
        GridView1.HeaderRow.Cells[2].Text = "MOBILE";
    }
}

public void fetch()
{
    try
    {
        SqlConnection con1 = new SqlConnection(s);
        con1.Open();

        SqlCommand cmd = new SqlCommand("select did,name,mobile,doj  from dealer", con1);
        DataTable dt = new DataTable();

        SqlDataReader dr = cmd.ExecuteReader();
        dt.Load(dr);
        GridView1.DataSource = dt;
        GridView1.DataBind();

        con1.Close();
    }
    catch (Exception t)
    {
        Label1.Text = "Unable to load database";
    }
}

Now, i want to add another GridView in the same page. I am used table and put 2 GridView in different cells. Try to answer in detail with a sample code, so that i can understand it properly.

as i can see you haven't posted your .aspx pages details and you are only binding Gridview1 so i can simply suggest you to do that like this

you can simply drag two Gridview in your .aspx page from toolbox

<asp:GridView ID="GridView1" runat="server" >   
</asp:GridView>
<br />
<asp:GridView ID="GridView2" runat="server">   
</asp:GridView>

and then bind them through your .cs code by passing both data source

 GridView1.DataSource = //Datasource;
 GridView1.DataBind();


 GridView2.DataSource = //Datasource;
 GridView2.DataBind();

and as you are asking for Sample here you can find that Sample Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM