返回首页

简介
您好,读者。
,同时建立一个Web项目,我们可能经常遇到的记录或其他类型的数据显示的情况。我们可以做的最好方法,它使用的DataGrid / GridView的。这很简单,需要知识的C#,Web应用程序的建设和2005年的SQL,。
"一张图片胜过1000字quot;!所以我已经解释了很多东西用图片。我希望你会发现它真的很容易。这大概是最后的结果看起来像... ...设计信息
首先是MasterGrid。您可以设计为...{S1}
它有两列,如下所示。这里的零列的描述...{S2}
Label1的数据绑定到Emp_dept_name,这是我设计信息后。可设计为MasterGrid列[1] ...{S3}
在ItemTemplate中,我们有ChildGrid。所有列都包含在他们的ItemTemplate标签(单独绑定Emp_table,这也是设计信息后,每列的名称,描述领域)的模板列。所有列在他们的FooterTemplate中的文本框。这些都是新的(在修改列)按钮被点击时暴露。修改列,只MasterGrid第0列的编辑按钮被点击时,将会看到可设计为...{S4}的
我认为这足以让你设计的所有要求。
注:MasterGrid的DataKeyNames属性设置为Emp_dept_name。数据源
为我所用的数据,看到两个数据表Emp_table和Emp_dept,你可以在SQL创建下面的图片...{五}
{中六}
,您可以执行这些查询。在这种情况下,首先选择的数据库的名称。{七}
我用SQL过程... ...{S8}
,您可以保存此使用的程序的名称(BindMasterGrid)本身。
注:我已经使用了两个不同的表。数据绑定LT; Label1gt;系列是必然Emp_dept_name Emp_dept表。对于剩下的工作,Emp_table使用。使用代码
即将编码部分:不要忘记使用正确的数据库连接字符串。把它作为在宣布SQL连接的时间参数。此外,还要确保web.config文件中包含正确的连接字符串。

using System;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;



public partial class _Default : System.Web.UI.Page 

{

    SqlConnection con=new SqlConnection ("

        Your database connection string here(

        under which you create Emp_table,Emp_dept tables)");

    DataView dv=new DataView();

    

    //

    //

    //*******************Use of dataview to be noted here*************************

    //

    //

    

    protected void Page_Load(object sender, EventArgs e)

    {

        dv = ((DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty));

        Page.MaintainScrollPositionOnPostBack = true;

        if (!Page.IsPostBack)

        {

            BindMasterGrid();

            BindChildGrid();

            

        }

    }

    

    //

    //To bind MasterGrid the use of strored procedure named-----BindMasterGrid----

    //

    //



    private void BindMasterGrid()

    {

        SqlCommand cmd = new SqlCommand("BindMasterGrid",con);

        cmd.CommandType = CommandType.StoredProcedure;

        SqlDataAdapter da = new SqlDataAdapter(cmd);



        DataSet ds = new DataSet();

        da.Fill(ds);



        MasterGrid.DataSource = ds;

        MasterGrid.DataBind();

        

    }



    //Before the GridView control can be rendered, each row in the control must be 

    //bound to a record in the data source.The RowDataBound event is raised when a

    //data row (represented by a GridViewRow object)is bound to data in the GridView 

    //control.

  

    protected void MasterGrid_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        GridView gr;

        if (e.Row.RowType == DataControlRowType.DataRow)

        {



            gr = ((GridView)e.Row.FindControl("ChildGrid"));

            dv.RowFilter = "Emp_dept=" + Convert.ToString(

                MasterGrid.DataKeys[e.Row.RowIndex].Value);

            gr.DataSource = dv;

            gr.DataBind();

        }

    }



    //

    //Use of Select statement to bind the ChildGrid

    //

    //



    private void BindChildGrid()

    {



        for (int i = 0; i < MasterGrid.Rows.Count; i++)

        {



            ((GridView)MasterGrid.Rows[i].Cells[1].Controls[1]).DataSource = null;

            Label lbl1 = (Label)(MasterGrid.Rows[i].Cells[0].Controls[3]);

            DataSet ds1 = new DataSet();

            SqlCommand cmd = 

                new SqlCommand(

                "SELECT Emp_no, Emp_name, Emp_sal FROM Emp_table where Emp_dept ='" + 

                lbl1.Text + "'", con);

            SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, con);

            da.Fill(ds1, "Emp_table");

            ((GridView)MasterGrid.Rows[i].Cells[1].Controls[1]).DataSource = ds1;

            ((GridView)MasterGrid.Rows[i].Cells[1].Controls[1]).AllowSorting = true;

            ((GridView)MasterGrid.Rows[i].Cells[1].Controls[1]).DataBind();



        }

    }



    //

    //The event below is fired when "Edit" button in Department column is clicked

    //

    //The point to be noted here is that----we store the index of the row in which

    //Edit button was clicked----using Sessions.

    //

    //Modify column in the ChildGrid apears

    //



    protected void MasterGrid_RowEditing(object sender, GridViewEditEventArgs e)

    {  

        int indx = e.NewEditIndex;

        Session["ind"] = indx;



        int i = (int)Session["ind"];

        GridView Childgrid = (GridView)(MasterGrid.Rows[i].Cells[1].Controls[1]);

        Childgrid.Columns[3].Visible = true;

        MasterGrid.Rows[i].FindControl("CancelMaster").Visible = true;



    }
{S9}{C}结论
我发现它非常有趣的工作与电网。我们可以进一步插入一个网格一个网格内扩展,但在实际使用中,我们包括其他各种功能,如JavaScript,等等,以避免在Web批量传输,一次又一次。可能有许多其他方法可以有效地做到这一点,但我也是一个初学者水平,这将是最好的开始。我欢迎任何从你身边的查询和响应。继续作出真诚的努力,有一天,你会是一个好的程序员。
感谢您,
苛刻巴巴尔历史 2008年1月15日 - 原始版本发表于

回答

评论会员:安德鲁蒙代尔 时间:2011/12/15
嗨,你可以寄给我你的aspx文件以及。为了andrwmund@yahoo.co.uk

干杯
评论会员:。ManiMphil 时间:2011/12/15
超级概念
评论会员:会员7915312 时间:2011/12/15
,能否请您给我的前端代码文件吗?我完全的一切,但在何处放置标签1的BoundField。预先感谢。很好的例子!

jamie_winterbottom@hotmail.com
评论会员:会员3855330 时间:2011/12/15
我也可以发送aspx文件,以更好地理解代码:dilonghi@hotmail.com 2011年8月17日(星期三),下午01:10
修改
评论会员:会员2706276 时间:2011/12/15
请你给我的aspx文件marco.yandun @ acitus.com请


马可孛
评论会员:poojafhf 时间:2011/12/15
这是我一直在寻找完美的代码。
一个非常丰富和详细的文章
评论会员:比奈普拉萨德 时间:2011/12/15
你只要给的解释,在加入chilGrid项模板,因为我没有得到子网格调用的任何事情,其迫切
评论会员:。比奈普拉萨德 时间:2011/12/15
你可以请邮件我ASPX文件的电子邮件ID Binay_bin_prasad@yahoo.co.in
评论会员:hvap 时间:2011/12/15
您好,可以请你送我一个完整的文件尽快
?我的意思是正确理解ASPX和代码隐藏文件。
我的电子邮件地址是hiralpatel2002@gmail.com

谢谢
评论会员:。Pwilson 时间:2011/12/15
我有一个3列FacilityID,标准和GISID(AJAX启用)gridview1。 Gridview1最终用户添加新记录的页脚启用。我想发生的情况是,用户输入到Gridview1 FacilityID TextBox和标准的TextBox的数据,当他们单击Gridview2持久性有机污染物UPS GISID文本框,并显示可选择的数据,基于FacilityId和标准Texboxes进入。 Gridview2使用一个存储过程参数@ FacilityId和@标准的一个SqlDataSource Gridview2返回结果。我已经得到了Gridview2工作不需要参数的存储过程,但我真的需要它的工作与带参数的存储过程。问题是,我不能带参数的存储过程,找到gridview1位于设施标准文本框。

任何帮助将不胜感激
评论会员:。krkr 时间:2011/12/15
你可能发表您aspx文件。
评论会员:巴巴尔 时间:2011/12/15
,请告诉我您的邮件ID.I会邮寄给您,主席先生。

恶劣巴巴尔
评论会员:MKauffman 时间:2011/12/15
你可能要考虑读一个访问数据库的所有子表的数据和适当的个别子表的子集绑定。否则,你将数据库(其中n是多个部门)N 1人次。这是一个小数目n没有大不了,但它成为一个问题,当n增长。
评论会员:巴巴尔 时间:2011/12/15
是的,你做了一个非常好的point.As,没有。部门增加了没有。
数据班次将增加simultaneously.But,正如我在结论中说,在初级水平,这似乎是最好的选择。
 0; 在现实世界中的情况的情况下,同时建立一个网站
的车次应该而且可以被限制为1,无论条件是什么。

恶劣巴巴尔
评论会员:tparsnick 时间:2011/12/15
苛刻,
你会如何重写这个减少量的行程。
你会使用缓存的数据集?你有任何示例代码?