Wednesday, January 26, 2011

DropDownList - Set selected value


The following code snippent shows how to set the selected item of a
DropdownList


 


DropDownList1.ClearSelection();
ListItem li = DropDownList1.Items.FindByValue("20");
DropDownList1.Items.FindByText(li.Text).Selected = true;


I have seen many beginners are struggling to set the selected value  dynamically,
Hope this is very helpful.

Sunday, January 16, 2011

The report you requested requires further information

This issue is a common one who starts working on Crystal Reports with Dataset (.Net)

loginerror

 

Ill show a sample code snippet where you can generate this issue.

                 ReportDocument report = new ReportDocument();    

BAgent bAgent = new BAgent();
DataSet dsStatus = new DataSet();
dsStatus = bAgent.GetStatus();

string reportPath = Server.MapPath("/Agent/Reports/GetStatusRpt.rpt");
report.Load(reportPath);

report.SetDataSource(dsStatus);// this is the line cause for this issue
CrystalReportViewer1.ReportSource = report;
CrystalReportViewer1.DataBind();
CrystalReportViewer1.RefreshReport();
 



Here is the solution, use the datset table instead of the dataset


 

                 ReportDocument report = new ReportDocument();    

BAgent bAgent = new BAgent();
DataSet dsStatus = new DataSet();
dsStatus = bAgent.GetStatus();

string reportPath = Server.MapPath("/Agent/Reports/GetStatusRpt.rpt");
report.Load(reportPath);
                 report.SetDataSource(dsStatus.Tables[0]); 
CrystalReportViewer1.ReportSource = report;
CrystalReportViewer1.DataBind();
CrystalReportViewer1.RefreshReport();

 


Hope you got rid from this issue. Enjoy coding.

Thursday, January 13, 2011

GridView Hidden Column Data Access

Again I faced an issue, as usual before .net version 2.0 I set a column's visibility property of a gridview to false.
If you are a developer by now you know why I did that. I know you guessed it. I just wanted to use that column's value
in one of my calculations, and didn't want to show it to the user.

Grid is working fine it hides the column I wanted to hide, I am happy!., Suddenly I realized that I don't get the value from
the hidden (visibility= false) column's cell value in the following method.

 

 protected void grd_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = grd.SelectedRow;
string aValue = row.Cells[8].Text; //this is the column I set false visibility
}


I was looking my coding deeply I am sure this is correct 100%, then what has gone wrong?

With 2.0 and above versions on .Net when you set a column of a gridview to visibility false


it doesn't  bind the data to this hidden column. You must be really disappointed hearing this


news, even I did. Wait you don't have to worry so long.



Don't set your column's property visibility to false in the design view. If you do so it will

call before the databind. When there is no column databing is not take place and ignores this hidden column.


Obvious we  don't get data (you are lucky only you didn't get the data, worse case this might throw an error)



Trick to overcome this issue,



RowCreated event of a GridView takes place after the data binding, so we can do something like following

code snippet.



 protected void grd_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[8].Visible = false;
}



This hides the column, still you can access the cell value,



 protected void grd_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = grd.SelectedRow;
string aValue = row.Cells[8].Text; //I get the cell value, I am happy!
}


 


Enjoy working on Gridview control,

Tuesday, January 11, 2011

ASP.Net Gridview column totals

Most of the developers use repeater control, in case where they have to show the totals of a list  instead gridview control.
This is not that difficult to show the totals on the gridview itself on footer template. I will show the steps how to do this in this post.

 

Drag and drop a Gridview on your .aspx and bind the data.

eg: 

grd.DataSource = ds;
grd.DataBind();

on above code snippet grd is the my Gridview name and ds is my dataset. (you can use any datasource as your convenience)

Set the ShowFooter property to True of the GridView, this will show the footer on the Gridview.

Write the RowDataBound  event as follows, you can change the way you want. (in the following scenario I have not showed the declaration of chkAmount and transCharge 
decimal type variables, these two are class level varibles)

 

 

        protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
chkAmount += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ChkAmount"));
transCharge += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "TransCharge"));
}
else if (e.Row.RowType == DataControlRowType.Footer) // this is the footer identification
{
e.Row.Cells[4].Text = "Totals:";
// display totals on cells (in my case I have selected last 3 columns)
e.Row.Cells[6].Text = chkAmount.ToString("N2");
e.Row.Cells[5].Text = transCharge.ToString("N2");

}
}



 



Hope this is really easier when it comes to show totals on Gridview footer area . Please refer the following image which is the output of my above coding effort.



gridview-column-totals

My Achievements

Member of

Blog Archive

Followers