Sunday 17 February 2013

Use of Generic Dictionary<>

Dictionary contains a key and a value. Each key is correspond to one value. It is very useful, when u have some value according to another value, and u need to keep track of which one is correspond to which value.
I had certain situations where Dictionary was valuable for me. here I would post one of those:

Declare:

public Dictionary<string, string> GridVal = new Dictionary<string, string>();


Use To Filter Data :

The Design is as follows:

According to the selected item we need to filter the data and show it in the gridview. here I have used Dictionary to form the condition, to select the data from the database, according the the section user makes.

Code:

protected void btnFilter_Click(object sender, EventArgs e){

try{
DataSet ds = new DataSet();
DataTable dt = new DataTable();GetSelectedValues();
GetConditions();

//ds= TMBL.GetGridValue(Convert.ToInt32(ddlGenerateYear.Text.Trim()));//ViewState["Dataset"] = ds;ds = (DataSet)ViewState["Dataset"];
if (ds.Tables[0].Select(TMUtil.queryCondition).Any()){
dt = ds.Tables[0].Select(TMUtil.queryCondition).CopyToDataTable();
gvTMInitialForecast.DataSource = dt;
gvTMInitialForecast.DataBind();
}

else{
gvTMInitialForecast.DataBind();
LblError.Visible =
true;LblError.Text =
"No Data Found As Per Your Search";}
}

catch{
//gvTMInitialForecast.Visible = false;LblError.Visible = true;LblError.Text =
"No Data Found As Per Your Search";}
}



private void GetConditions(){
TMUtil.queryCondition =
"";
foreach( KeyValuePair<string, string> value in TMUtil.GridVal){

if (TMUtil.queryCondition != "")TMUtil.queryCondition = TMUtil.queryCondition +
" and ";TMUtil.queryCondition = TMUtil.queryCondition+value.Key +
" = '"+ value.Value +"'";
}
}
// Get the values selected by the user and store it in Dictionary<>


private void GetSelectedValues(){
if (ddlProjectLOB.Text != "")TMUtil.GridVal[
"ProjectLOB"] = ddlProjectLOB.Text.Trim();//Gridval is the dictionary having "ProjectLOB" as key and the ddl text as its value
if (ddlPortfolio.Text != "")TMUtil.GridVal[
"Portfolio"] = ddlPortfolio.Text.Trim();
  if (ddlProjectID.Text != "")
TMUtil.GridVal[
"ProjectID"] = ddlProjectID.Text.Trim();
  if(ddlLocation.Text!="")
TMUtil.GridVal[
"Region"] = ddlLocation.Text.Trim();
  if (ddlRole.Text != "")
TMUtil.GridVal[
"[Role]"] = ddlRole.Text.Trim();
  if(ddlEmployeeID.Text!="")
TMUtil.GridVal[
"ResourceID"] = ddlEmployeeID.Text.Trim();
  if (ddlYear.Text != "")
TMUtil.GridVal[
"ForecastYear"] = ddlYear.Text.Trim();
  if(ddlMonth.Text!="")
TMUtil.GridVal[
"Month"] = ddlMonth.Text.Trim();
}



The Viewstate already contains the dataset having the total value from the database, and then the filter is done on this dataset,according to the condition. the condition is framed with the help of DICTIONARY<>. Dictionary contails the name of the column as its key and the value selected by the user as its value. now while framing the condition, we use the key and the value both. this condition is just use to filter data from the dataset.

No comments:

Post a Comment