Adding Dynamic columns to gridview

2010-05-23

private void FillGrid(int BOQTenderID)
{
try
{
IBOQSubcontractRateComparisonManager objManager = TBDBusinessFactory.CreateBOQSubcontractRateComparisonManager();
List lst = objManager.GetBOQSubcontractRateComparison(BOQTenderID);
Session["SubcontractorActivityRatesAgainstBOQByAllSubContractors"] = lst;
AddNewColumnsToGrid(lst);
gvSubContractorActivitiesRates.DataSource = lst;
gvSubContractorActivitiesRates.DataBind();
SaveViewState();
}
catch (Exception ex)
{
}
}

private void AddNewColumnsToGrid(List lst)
{
try
{
BoundField column = null;
int noOfNewColoumnsToAdd = 0;
int totalColumnCount = gvSubContractorActivitiesRates.Columns.Count;
//Remove all columns
for (int i = 1; i <= totalColumnCount; i++)
{
gvSubContractorActivitiesRates.Columns.RemoveAt(totalColumnCount - i);
}
//Add fixed columns(fixed columns = 2)([ESTSubContractActivityID,SubContractActivity]-single column &
//Select Lowest Rate
column = new BoundField();
column.HeaderText = "SubContractActivity";
gvSubContractorActivitiesRates.Columns.Add(column);
column = new BoundField();
column.HeaderText = "Select Lowest Rate";
gvSubContractorActivitiesRates.Columns.Add(column);
//==find the no of columns to add dynamically where no of dynamic columns = no of subcontracotrs==//
List lstContractorsAlreadyAdded = new List();
foreach (SCTActivitiesAndRatesByContractorsInfo info in lst)
{
foreach (TBDBOQSubContractRateComparisonGetInfo x in info.LstContractorWithRate)
{
if (!lstContractorsAlreadyAdded.Exists(k => k.ContractorShortName == x.ContractorShortName))
{
noOfNewColoumnsToAdd++;
lstContractorsAlreadyAdded.Add(x);
}
}
}
for (int i = 1; i <= noOfNewColoumnsToAdd; i++)
{
column = new BoundField();
column.HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
column.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
gvSubContractorActivitiesRates.Columns.Add(column);
}
//Adding Selected Rate Column as the last column
column = new BoundField();
column.HeaderText = "Selected Rate";
column.HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
column.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
gvSubContractorActivitiesRates.Columns.Add(column);
}
catch (Exception ex)
{
}
}
///
/// Adding controls to the newly created columns
///
///
///

protected void gvSubContractorActivitiesRates_RowCreated(object sender, GridViewRowEventArgs e)
{
List lst = (List)Session["SubcontractorActivityRatesAgainstBOQByAllSubContractors"];
List lstActivitiesAlreadyAdded = null;
List lstContractorsAlreadyAdded = null;
if (Session["activitiesAlreadyAdded"] != null)
{
lstActivitiesAlreadyAdded = (List)Session["activitiesAlreadyAdded"];
}
else
{
lstActivitiesAlreadyAdded = new List();
}
try
{
        #region HEADER ROW
if (e.Row.RowType == DataControlRowType.Header)
{
lstContractorsAlreadyAdded = new List();
int columnIndex = 1;
//Adding checkbox to the header of the 2nd column "Select Lowest Rate"
CheckBox chkSelectAllLowest = new CheckBox();
chkSelectAllLowest.ID = string.Format("chkSelectAllLowest{0}", columnIndex);
chkSelectAllLowest.Text = "Select Lowest Rate";
//Adding the newly created controls to header row
e.Row.Cells[columnIndex].Controls.Add(chkSelectAllLowest);
//Dynamic no of columns to be added from 2 onwards
columnIndex = 2;
foreach (SCTActivitiesAndRatesByContractorsInfo info in lst)
{
foreach (TBDBOQSubContractRateComparisonGetInfo x in info.LstContractorWithRate)
{
if (!lstContractorsAlreadyAdded.Exists(k => k.ContractorShortName == x.ContractorShortName))
{
Label lblSubContractor = new Label();
CheckBox chkSelectContractor = new CheckBox();
HiddenField hdnSCTContractorID = new HiddenField();
//Setting IDs to the newly created controls
lblSubContractor.ID = string.Format("lblSubContractor{0}", columnIndex);
chkSelectContractor.ID = string.Format("chkSelectContractor{0}", columnIndex);
hdnSCTContractorID.ID = string.Format("hdnSCTContractorID{0}", columnIndex);
//Setting Text values to the newly created controls
lblSubContractor.Text = x.ContractorShortName;
hdnSCTContractorID.Value = x.SCTContractorID.ToString();
//Adding the newly created controls to header row
e.Row.Cells[columnIndex].Controls.Add(chkSelectContractor);
e.Row.Cells[columnIndex].Controls.Add(lblSubContractor);
e.Row.Cells[columnIndex].Controls.Add(hdnSCTContractorID);
columnIndex++;
lstContractorsAlreadyAdded.Add(x);
}
}
}
}
#endregion


#region DATAROW
if (e.Row.RowType == DataControlRowType.DataRow)
{
lstContractorsAlreadyAdded = new List();
int columnIndex = 0;
//Adding control to first column [ESTSubContractActivityID,SubContractActivity &
// TBDBOQSubcontractRateComparisonID]-single column
Label lblSubContractActivity = new Label();
lblSubContractActivity.ID = string.Format("lblSubContractActivity{0}", columnIndex);
e.Row.Cells[columnIndex].Controls.Add(lblSubContractActivity);
HiddenField hdnESTSubContractActivityID = new HiddenField();
hdnESTSubContractActivityID.ID = string.Format("hdnESTSubContractActivityID{0}", columnIndex);
e.Row.Cells[columnIndex].Controls.Add(hdnESTSubContractActivityID);
//Adding TBDBOQSubcontractRateComparisonID to the first column
HiddenField hdnTBDBOQSubcontractRateComparisonID = new HiddenField();
hdnTBDBOQSubcontractRateComparisonID.ID = string.Format("hdnTBDBOQSubcontractRateComparisonID{0}", columnIndex);
e.Row.Cells[columnIndex].Controls.Add(hdnTBDBOQSubcontractRateComparisonID);
columnIndex = 2;
foreach (SCTActivitiesAndRatesByContractorsInfo info in lst)
{
if (!lstActivitiesAlreadyAdded.Exists(k => k.ESTSubContractActivityID == info.ESTSubContractActivityID))
{
foreach (TBDBOQSubContractRateComparisonGetInfo x in info.LstContractorWithRate)
{
if (!lstContractorsAlreadyAdded.Exists(k => k.ContractorShortName == x.ContractorShortName))
{
lstActivitiesAlreadyAdded.Add(info);
HiddenField hdnTBDBOQSubContractRateID = new HiddenField();
QuadraQuantityTextBox qqtyRateOfContractorForActivity = new QuadraQuantityTextBox();
//Setting IDs to the newly created controls
hdnTBDBOQSubContractRateID.ID = string.Format("hdnTBDBOQSubContractRateID{0}", columnIndex);
qqtyRateOfContractorForActivity.ID = string.Format("qqtyRateOfContractorForActivity{0}", columnIndex);
//Setting Enable property of Rate field to false
qqtyRateOfContractorForActivity.Enabled = false;
//Adding the newly created controls to the datarow
e.Row.Cells[columnIndex].Controls.Add(hdnTBDBOQSubContractRateID);
e.Row.Cells[columnIndex].Controls.Add(qqtyRateOfContractorForActivity);
columnIndex++;
lstContractorsAlreadyAdded.Add(x);
}
}
}
}
//columnIndex += gvSubContractorActivitiesRates.Columns.Count - 3;//Subtract first & second columns
//Adding controls to the last column "Selected Rate"
QuadraQuantityTextBox qqtytxtSelectedRate = new QuadraQuantityTextBox();
qqtytxtSelectedRate.ID = string.Format("qqtytxtSelectedRate{0}", columnIndex);
qqtytxtSelectedRate.Enabled = false;
e.Row.Cells[columnIndex].Controls.Add(qqtytxtSelectedRate);
//This hidden field is set to the last column as Value from QuadraQuantityTextBox could not read 
//from server side when the value is set at from client side!!!(work around)
HiddenField hdnSelectedRate = new HiddenField();
hdnSelectedRate.ID = string.Format("hdnSelectedRate{0}", columnIndex);
e.Row.Cells[columnIndex].Controls.Add(hdnSelectedRate);
//Keeping the lstActivitiesAlreadyAdded to the session
Session["activitiesAlreadyAdded"] = lstActivitiesAlreadyAdded;
}
#endregion
}
catch (Exception ex)
{
}
}
///
/// Setting values to the cells of the rows
///
///
///
protected void gvSubContractorActivitiesRates_RowDataBound(object sender, GridView
{
try
{
SCTActivitiesAndRatesByContractorsInfo bindingInfo = null;
if (e.Row.RowType == DataControlRowType.Header)
{
int columnIndex = 1;
CheckBox chkSelectAllLowest = (CheckBox)e.Row.FindControl("chkSelectAll");
if (chkSelectAllLowest.ClientID == hdnSelectedHeaderCheckBoxID.Value)
{
if (hdnPreviousPageCheckBoxHeaderState.Value == "checkedState")
chkSelectAllLowest.Checked = true;
else if (hdnPreviousPageCheckBoxHeaderState.Value == "unCheckedSta
chkSelectAllLowest.Checked = false;
string chkSelectAllLowestClientID = "document.getElementById("" +
Page.ClientScript.RegisterStartupScript(this.GetType(), "keyExecut
}
chkSelectAllLowest.Attributes.Add("onclick", "CheckOrUncheckLowestRate
//Dynamic no of columns starts from 2 onwards
columnIndex = 2;
List lst = (List< td=""> <>
List lstContractorsAlreadyAdde
foreach (SCTActivitiesAndRatesByContractorsInfo info in lst)
{
foreach (TBDBOQSubContractRateComparisonGetInfo x in info.LstContr
{
if (!lstContractorsAlreadyAdded.Exists(k => k.ContractorShortN
{
//===Get Controls in the header of each subcontractors===/
HiddenField hdnSCTContractorID = (HiddenField)e.Row.FindCo
CheckBox chkSelectContractor = (CheckBox)e.Row.FindControl
if (chkSelectContractor.ClientID == hdnSelectedHeaderCheck
{
if (hdnPreviousPageCheckBoxHeaderState.Value == "check
chkSelectContractor.Checked = true;
else if (hdnPreviousPageCheckBoxHeaderState.Value == "
chkSelectContractor.Checked = false;
string chkSelectContractorClientID = "document.getElem
Page.ClientScript.RegisterStartupScript(this.GetType()
}
//====Set Javascript function on selecting this subcontrac
chkSelectContractor.Attributes.Add("onclick", "SetRatesOfC
columnIndex++;
lstContractorsAlreadyAdded.Add(x);
}
}
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
int columnIndex = 0;
bindingInfo = (SCTActivitiesAndRatesByContractorsInfo)e.Row.DataItem;
//Binding values to first column [ESTSubContractActivityID,SubContract
// TBDBOQSubcontractRateComparisonID]
HiddenField hdnESTSubContractActivityID = (HiddenField)e.Row.FindContr
hdnESTSubContractActivityID.Value = bindingInfo.ESTSubContractActivity
Label lblSubContractActivity = (Label)e.Row.FindControl("lblSubContrac
lblSubContractActivity.Text = bindingInfo.SubContractActivity;
HiddenField hdnTBDBOQSubcontractRateComparisonID = (HiddenField)e.Row.
hdnTBDBOQSubcontractRateComparisonID.Value = bindingInfo.TBDBOQSubcont
columnIndex = 2;//From 2nd column onwards dynamic columns
foreach (TBDBOQSubContractRateComparisonGetInfo x in bindingInfo.LstCo
{
HiddenField hdnTBDBOQSubContractRateID = (HiddenField)e.Row.FindCo
QuadraQuantityTextBox qqtyRateOfContractorForActivity = (QuadraQua
hdnTBDBOQSubContractRateID.Value = x.TBDBOQSubContractRateID.ToStr
qqtyRateOfContractorForActivity.Text = GetFormattedRate(x.Contract
columnIndex++;
}
//columnIndex += bindingInfo.LstContractorWithRate.Count;
//Setting values for the controls in the last column
QuadraQuantityTextBox qqtytxtSelectedRate = (QuadraQuantityTextBox)e.R
qqtytxtSelectedRate.Text = GetFormattedRate(bindingInfo.Rate);
HiddenField hdnSelectedRate = (HiddenField)e.Row.FindControl("hdnSelec
hdnSelectedRate.Value = GetFormattedRate(bindingInfo.Rate);
}
}
catch (Exception ex)
{
}
}
protected void gvSubContractorActivitiesRates_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
gvSubContractorActivitiesRates.PageIndex = e.NewPageIndex;
FillGrid(int.Parse(ddlTenderNo.SelectedValue));
}
catch (Exception ex)
{
}
}

0 comments: