Thursday, March 3, 2011

How to export gridview content to csv in asp.net C#?

Here I'm showing you how to export a GridView content to csv (Comma separated values) in asp.net using C#.

So here I have a GridView gvResult and a button btnExportToCsv . On btnExportToCsv click we want to export gvResults data to export in csv file. So here is the code bellow hope this helps you.

protected void btnExportToCsv_Click(object sender, EventArgs e)
{
try
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=GridViewExport.csv");
Response.Charset = string.Empty;
Response.ContentType = "application/text";
gvResult.AllowPaging = false;
gvResult.DataBind();
StringBuilder sb = new StringBuilder();
string coloumns = string.Empty;
for (int k = 1; k < gvResult.Columns.Count - 1; k++)
{
//add ',' separator

coloumns += gvResult.Columns[k].HeaderText + ',';
sb.Append(gvResult.Columns[k].HeaderText + ',');

}

//This portion is to replace the header text of the gvResult to the binding data source data table's columns //name so can we get the correct data from the datasource ,because we formated the gvResult header as per //our reqirement and the header text doesn't match with the datasource table
coloumns = coloumns.Replace("Coupon ID", "ID_Coupon");
coloumns = coloumns.Replace("Employee ID", "Customer_ID");
coloumns = coloumns.Replace("Created On", "CreatedDate");
coloumns = coloumns.Replace("Valid Until", "ValidUntil");
coloumns = coloumns.Replace("Activated", "ActivatedDate");
coloumns = coloumns.Replace("Cashed", "CashedDate");
coloumns = coloumns.Replace("Euro", "EuroValue");
//here we remove the last ',' from the string 'coloumns'

coloumns = string.IsNullOrEmpty(coloumns) ? string.Empty : coloumns.Substring(0, coloumns.LastIndexOf(","));
string[] clmnList = coloumns.Split(',');
DataTable dt = new DataTable();

//Here takes the required fields from the gridview(gvResult's datasource)
dt = gvResult.DefaultView.ToTable(false, clmnList.ToArray());
//append new line
sb.Append("\r\n");

//Here put the data to the particular column
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int k = 0; k < dt.Columns.Count; k++)
{
//add separator
sb.Append(Convert.ToString(dt.Rows[i][k]) + ',');
}
//append new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
}
catch (Exception ex) { }
}

Hope this will help!

Wednesday, February 16, 2011

How to select multiple value of a checkboxlist in asp.net C#?

Here I am showing how to select multiple value of a checkboxlist.
The SetValueCheckBoxList methode doing this where the StringToArrayList methode return the arrylist for the sValues parameter of SetValueCheckBoxList
    public void SetValueCheckBoxList(CheckBoxList cbl, string sValues)
    {
        if (!string.IsNullOrEmpty(sValues))
        {
            ArrayList values = StringToArrayList(sValues);
            foreach (ListItem li in cbl.Items)
            {
                if (values.Contains(li.Value))
                    li.Selected = true;
                else
                    li.Selected = false;
            }
        }
    }
==================================================

    private ArrayList StringToArrayList(string value)
    {
        ArrayList _al = new ArrayList();
        string[] _s = value.Split(new char[] { ',' });
        foreach (string item in _s)
            _al.Add(item);
        return _al;
    }
=========================================
Call like this bellow
SetValueCheckBoxList(chkTypeHome, Convert.ToString(dtProperty.Rows[0]["TypeOfhome"]));
here chkTypeHome= checkboxlist
and Convert.ToString(dtProperty.Rows[0]["TypeOfhome"]) has data with comma separated sting like "1,2,4,5"

Thursday, December 23, 2010

How to disable the ajax calender dates which are less than the current day?

 Hi All,
            I got so much help form the link bellow to solve some of issue which i need to implement on one of my project. That's why i thought to share with you so can you also get some help from here...

Disable the dates which are less than the current day