Monday, January 19, 2009

Repeater Contorl within GridView




In the Aspx page :


Ans in the .cs page :

public partial class ProjectReports : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
fillGridView();

}
private void fillGridView()
{

string FileDirectory = Server.MapPath("Database/admin.mdb");
OleDbConnection ocon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileDirectory.ToString() + "");

try
{
ocon.Open();
string str = "SELECT Project_master.Project_name,Project_master.Project_id, Project_master.Project_total_hours, Project_master.Project_start_date, Project_master.Project_end_date, User_master.User_name" + " " +
"FROM Project_master INNER JOIN User_master ON Project_master.User_id = User_master.User_id";

//string str = "SELECT Project_assignment.ProjAssig_opening_hours, Project_assignment.ProiAssig_Allolet_hours, Project_assignment.ProjAssig_status, Project_master.Project_name, Project_master.Project_total_hours, Project_master.Project_start_date, Project_master.Project_end_date, User_master.User_name" + " " +
//"FROM Project_assignment INNER JOIN (Project_master INNER JOIN User_master ON Project_master.User_id = User_master.User_id) ON Project_assignment.Project_id = Project_master.Project_id";
//String str = "select *,User_name from Project_master inner join User_Master on Project_master.User_id=User_Master.User_id order by Project_master.Project_end_date desc";
OleDbDataAdapter osap = new OleDbDataAdapter(str, ocon);

DataSet ds = new DataSet();
osap.Fill(ds, "temp");
GVProjectReports.DataSource = ds;
GVProjectReports.DataBind();


}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}

}
private DataSet FillRepeater(string pid)
{

string FileDirectory = Server.MapPath("Database/admin.mdb");
DataSet ds = new DataSet();
OleDbConnection ocon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileDirectory.ToString() + "");

try
{
ocon.Open();
string str = "SELECT User_master.User_name,ProjAssig_opening_hours,ProiAssig_Allolet_hours " + " " +
"FROM Project_assignment INNER JOIN User_master ON User_master.User_id = Project_assignment.User_id where Project_assignment.Project_id="+ pid +" ";

OleDbDataAdapter osap = new OleDbDataAdapter(str, ocon);


osap.Fill(ds, "temp");

}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}

return ds;

}
protected void Button1_Click(object sender, EventArgs e)
{

}

private void Update()
{

}
protected void GVProjectReports_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Separator)
{

Repeater rep = (Repeater)e.Row.FindControl("ProReport");
Label pid=(Label)e.Row.FindControl("projectID");
Label lblTotHr = (Label)e.Row.FindControl("lblTotHr");
Label _lblRemaingHr = (Label)e.Row.FindControl("lblRemaingHr");



rep.DataSource = FillRepeater(pid.Text);
rep.DataBind();
int totalHr = 0;
foreach (RepeaterItem repe in rep.Items)
{
Label lblopenAllocated = (Label)repe.FindControl("lblopenHr");
totalHr = totalHr + Convert.ToInt32(lblopenAllocated.Text);
}

_lblRemaingHr.Text =Convert.ToString((Convert.ToInt32(lblTotHr.Text) - totalHr));

}
}
}


In this Article , Repeater within GridView . In GridView there are there are multiple records. And in the one record means in one row there would appear Repeater and in the Repeater Controle There would also be some record . So Getting this First we get all data in GridView and then we did the same process for the Repeater , but its some titical. For Repeater We have to make DataSet by a function and in the RowDataBound of the GridView we take will find Repeater Control and bind the data throgh rep.DataSource = FillRepeater(pid.Text);
rep.DataBind();
See this function : private DataSet FillRepeater(string pid), here we have taken data type of this function is DataSet becaues we provide DataSet for Reapeater Control. This function is made for collecting the data from DataBase. By this function we collect all data in the DataSet which are required for the Reapeater.

1 comment:

Anonymous said...

amazing... I can't stop reading your blog, there is huge amount of interesting information... I'll go to take my favorite tea and will return to your website. Btw, I recommend to everybody this tea http://www.iherb.com/Yogi-Tea-Kava-Stress-Relief-Caffeine-Free-16-Tea-Bags-1-27-oz-36-g/5473?at=0
It helps to concentrate and gives me power. You can use this code YAB426 for discount.

Thanks for the wonderful website!