زمانی که یک پروژه تحت وب مینویسید، در بسیاری شرایط نیاز دارید که رکوردها را به کاربران نمایش دهید. برای این کار یکی از کنترلهای بسیار مناسب و کارامد همان GridView یا نسخه قبلی آن Datagrid است.
در انی بنرامه فرض را بر این گذاشته ایم که شما با gridview و مفاهیمی مثل ویرایش ستونها و TemplateColumn و ... آشنایی دارید
به تصویر زیر دقت کنید :
همانگونه که میبینید در یک ساختار جدولی کاملا پویا ، در سمت چپ نام سازمان ها نوشته شده و در سمت راست مشخصات کارمندان این سازمان ها.
برای نمایش این ساختار از دو gridview استفاده کرده ایم. gridView بیرونی که آن را MasterGrid نام مینهیم برای نمایش سازمانها و gridview داخلی برای نمایش کارمندان.
MasterGrid ما در ابتدا چنین خواهد بود
همانگونه که میبینید دارای دو ستون است که ستون اول آن به شرح زیر میباشد
در این ستون برچسب Label1 به ستون Emp_dept_name در منبع داده مقید شده است.
ستون دوم نیز بدین شرح میباشد:
در ستون ItemTemplate میتوانیم gridView دوم که ChildGrid نام دارد را قرار دهیم و آن را به منبع داده های مورد نظر که همان جدول Emp_table میباشد مقید سازیم.
همانگونه که در MasterGrid ملاحظه کردید دکمه ای به نام Edit داشتیم و میخواستیم این امکان را فراهم کنیم که کاربر با کلیک کردن بر روی آن بتواند محتویات داخل ChildGrid را ویرایش کند.
بنابراین در ChildGrid یک ستون به نام Modify قرار میدهیم. این ستون به شرح زیر خواهد بود :
نکته : DataKeyNames در MasterGrid برابر Emp_dept_name مقداردهی شده است.
بررسی منبع داده ها
ریا این برنامه از دو جدول استفاده میکنیم
که برای ایجاد این جداول میتوانیم از این کوئری ها استفاده نماییم:
همچنین در کد برنامه از یک رویه SQL به نام BindMasterGrid استفاده کرده ایم.
استفاده از کد برنامه
این نکته را گوشزد میکنیم که از رشته ارتباط مناسب برای اتصال به پایگاه داده ها استفاده کنید. این رشته را متیوانید در فایل 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;
}
//
//The event below is fired when "Edit" button in Modify column of CildGrid is clicked
//
//
protected void ChildGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
int i = (int)Session["ind"];
GridView Childgrid = (GridView)(MasterGrid.Rows[i].Cells[1].Controls[1]);
Childgrid.EditIndex = e.NewEditIndex;
BindChildGrid();
}
//
//The event below is fired when "Cancel" button in Department column is clicked
//
//
protected void MasterGrid_RowCancelingEdit(
object sender, GridViewCancelEditEventArgs e)
{
int i = (int)Session["ind"];
MasterGrid.Rows[i].FindControl("CancelMaster").Visible = false;
GridView Childgrid = (GridView)(MasterGrid.Rows[i].Cells[1].Controls[1]);
Childgrid.Columns[3].Visible = false;
Childgrid.EditIndex = -1;
BindMasterGrid();
BindChildGrid();
}
//
//The event below is fired when "Cancel" button in
//Modify column(ChildGrid) is clicked
//
protected void ChildGrid_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
int i = (int)Session["ind"];
GridView Childgrid = (GridView)(MasterGrid.Rows[i].Cells[1].Controls[1]);
Childgrid.EditIndex = -1;
BindChildGrid();
}
//
//To update the editing done in the ChildGrid
//
//
protected void ChildGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int i = (int)Session["ind"];
GridView Childgrid = (GridView)(MasterGrid.Rows[i].Cells[1].Controls[1]);
int empno =
Convert.ToInt16(((Label)Childgrid.Rows[e.RowIndex].FindControl(
"Label1")).Text);
string empname =
((TextBox)Childgrid.Rows[e.RowIndex].FindControl("TextBox2")).Text;
double salary =
Convert.ToDouble(((
TextBox)Childgrid.Rows[e.RowIndex].FindControl("TextBox3")).Text);
SqlCommand cmd =
new SqlCommand("Update Emp_table set Emp_name='" +
empname + "',Emp_sal='" + salary +"'where Emp_no='" + empno+"'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Childgrid.EditIndex = -1;
BindChildGrid();
}
//
//Delete button will fire event below to delete a row in ChildGrid
//
//
protected void ChildGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int i = (int)Session["ind"];
GridView Childgrid = (GridView)(MasterGrid.Rows[i].Cells[1].Controls[1]);
int empno =
Convert.ToInt16(((Label)Childgrid.Rows[e.RowIndex].FindControl(
"Label1")).Text);
SqlCommand cmd =
new SqlCommand("Delete from Emp_table where Emp_no='" + empno + "'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Childgrid.EditIndex = -1;
BindChildGrid();
}
//
//
//Add a record in selected department
//
protected void Add_Click(object sender, EventArgs e)
{
int i = (int)Session["ind"];
GridView Childgrid = (GridView)(MasterGrid.Rows[i].Cells[1].Controls[1]);
int empno =
Convert.ToInt16(((TextBox)Childgrid.FooterRow.FindControl("TextBox4")).Text);
string empname = ((TextBox)Childgrid.FooterRow.FindControl("TextBox5")).Text;
double salary =
Convert.ToDouble(((
TextBox)Childgrid.FooterRow.FindControl("TextBox6")).Text);
string deptname = ((Label)MasterGrid.Rows[i].FindControl("Label1")).Text;
SqlCommand cmd =
new SqlCommand("Insert into Emp_table values('" +
empno + "','" + empname + "','" + deptname + "','" + salary + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
BindChildGrid();
((TextBox)Childgrid.FooterRow.FindControl("TextBox4")).Visible = false;
((TextBox)Childgrid.FooterRow.FindControl("TExtBox5")).Visible = false;
((TextBox)Childgrid.FooterRow.FindControl("TextBox6")).Visible = false;
}
//
//New button is used to Expose TextBoxes to Enter new record
//
//
protected void New_Click(object sender, EventArgs e)
{
int i = (int)Session["ind"];
GridView Childgrid = (GridView)(MasterGrid.Rows[i].Cells[1].Controls[1]);
BindChildGrid();
((TextBox)Childgrid.FooterRow.FindControl("TextBox4")).Visible = true;
((TextBox)Childgrid.FooterRow.FindControl("TExtBox5")).Visible = true;
((TextBox)Childgrid.FooterRow.FindControl("TextBox6")).Visible = true;
((LinkButton)Childgrid.FooterRow.FindControl("New")).Visible = false;
((LinkButton)Childgrid.FooterRow.FindControl("Add")).Visible = true;
((LinkButton)Childgrid.FooterRow.FindControl("Cancel")).Visible = true;
}
//
//When Cancel button in Modify column is clicked
//
//
protected void Cancel_Click(object sender, EventArgs e)
{
int i = (int)Session["ind"];
GridView Childgrid = (GridView)(MasterGrid.Rows[i].Cells[1].Controls[1]);
BindChildGrid();
((TextBox)Childgrid.FooterRow.FindControl("TextBox4")).Visible = false;
((TextBox)Childgrid.FooterRow.FindControl("TExtBox5")).Visible = false;
((TextBox)Childgrid.FooterRow.FindControl("TextBox6")).Visible = false;
((LinkButton)Childgrid.FooterRow.FindControl("New")).Visible = true;
((LinkButton)Childgrid.FooterRow.FindControl("Add")).Visible = false;
((LinkButton)Childgrid.FooterRow.FindControl("Cancel")).Visible = false;
}
}