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