简体   繁体   English

如何在同一个aspx页面中获取多个GridView

[英]How to get multiple GridView in same aspx page

I want to put multiple GridViews in same aspx page. 我想将多个GridViews放在同一aspx页面中。 I tried but its not working. 我尝试了,但是没有用。

Here is the code from cs page 这是cs页面中的代码

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. 现在,我想在同一页面中添加另一个GridView。 I am used table and put 2 GridView in different cells. 我用表并将2 GridView放在不同的单元格中。 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 如我所见,您尚未发布.aspx页的详细信息,而您仅绑定Gridview1因此我可以简单地建议您这样做

you can simply drag two Gridview in your .aspx page from toolbox 您只需从toolbox中的.aspx page拖动两个Gridview

<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 然后通过传递两个数据源通过.cs代码将它们绑定

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


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

and as you are asking for Sample here you can find that Sample Example 当您在这里索取样本时,您可以找到该样本示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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