martedì 26 febbraio 2013

Export DataGridView to Excel




public void ExportExcell(DataGridView dgw, int[] columnsToExport, int columnsWidth, string path, bool visible, bool quit)
        {
            Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
            ExcelApp.Application.Workbooks.Add(Type.Missing);


            ExcelApp.Columns.ColumnWidth = columnsWidth;

            int columnExcel = 1;
            List<int> columns = new List<int>();

            for (int a = 0; a < (columnsToExport.Length); a++)
                columns.Add(columnsToExport[a]);


            for (int i = 0; i < dgw.Columns.Count; i++)
            {
                if (columns.Contains(i))
                {
                    ExcelApp.Cells[1, columnExcel] = dgw.Columns[i].HeaderText;
                    columnExcel++;
                }
            }

            columnExcel = 1;
            for (int i = 0; i < dgw.Rows.Count; i++)
            {
                for (int j = 0; j < dgw.Columns.Count; j++)
                {
                    if (columns.Contains(j))
                    {
                        ExcelApp.Cells[i + 2, columnExcel] = dgw.Rows[i].Cells[j].Value.ToString();
                        columnExcel++;
                    }
                    //+2 because must write from second row of the excel sheet
                }
                columnExcel = 1;
            }

            ExcelApp.Visible = visible;

            if (!String.IsNullOrEmpty(path))
            {
                ExcelApp.ActiveWorkbook.SaveCopyAs(path);
                ExcelApp.ActiveWorkbook.Saved = true;
            }
           
            if(quit)
                ExcelApp.Quit();
        }




public void ExportExcell(DataGridView dgw, string[] columnsToExport, int columnsWidth, string path, bool visible, bool quit)
        {
            Microsoft.Office.Interop.Excel.Application ExcelApp = new              Microsoft.Office.Interop.Excel.Application();
            ExcelApp.Application.Workbooks.Add(Type.Missing);


            ExcelApp.Columns.ColumnWidth = columnsWidth;

            int columnExcel = 1;
            List<string> columns = columnsToExport.OfType<string>().ToList();

            for (int i = 0; i < columnsToExport.Length; i++)
            {
                if (dgw.Columns.Contains(columnsToExport[i]))
                {
                    ExcelApp.Cells[1, columnExcel] = dgw.Columns[columnsToExport[i]].HeaderText;
                    columnExcel++;
                }
            }

            columnExcel = 1;
            foreach (string item in columns)
            {
                for (int i = 0; i < dgw.Rows.Count; i++)
                {
                    ExcelApp.Cells[i + 2, columnExcel] = dgw.Rows[i].Cells[item].Value.ToString();
                }
                columnExcel++;
            }

            ExcelApp.Visible = visible;

            if (!String.IsNullOrEmpty(path))
            {
                ExcelApp.ActiveWorkbook.SaveCopyAs(path);
                ExcelApp.ActiveWorkbook.Saved = true;
            }

            if (quit)
                ExcelApp.Quit();
        }






giovedì 21 febbraio 2013

Sorting Hash Table - SortedDictionary

Today I needed to sorting HashTable.
Searching on "My Friend" Google :-) I found the C# Class SortedDictionary that represents a collection of key/value pairs that are sorted on the key.

Here some little example:

SortedDictionary<int, string> Your = new SortedDictionary<int, string>();

Your.Add(1, "…");

foreach (KeyValuePair<int, string> entry in Your)
{
//do something …
entry.Key
entry.Value
}

Go HERE for more info.



My Two Cents ...

mercoledì 20 febbraio 2013

Byte Array to Asp Image

If you have a byte array of an image, and you want to show it, you can see the following example.

C# code:


byte[] arr = //get you image byte array
string base64String = Convert.ToBase64String(arr, 0, arr.Length);
Image1.ImageUrl = "data:image/png;base64," + base64String;
Image1.Visible = true;


 Aspx Code:

<asp:Image ID="Image1" Visible = "false" runat="server" Height = "100" Width = "100" />    



My Two Cents ...

martedì 19 febbraio 2013

Expand/Collapse JQuery Accordion at Runtime by C#

The following code shows a simple way to Collapse/Expand JQuery Accordion by C#

JQuery Code to make accordion:

$(function ()
        {
            $("#yourDivID").accordion(
            {
                heightStyle: "content",
                collapsible: true
            });
        });


Html code to make the accordion:

<div id="yourDivID ">
<h3>Your DIV Title</h3>
<div>

</div>
</div>


C# code to Collapse/Expand JQuery Accordion:

if (true….)
script = "$(\"#yourDivID\").accordion('activate', 'false'); ";
else
script = "$(\"#yourDivID\").accordion('option', 'active', 0); ";
           
ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), script, true);



My Two Cents ...

Getting Value From Web.Config

Sometimes you need to put, for example, configuration keys in the web.config file.
The following code shows how getting this value of the keys in the web.config.

Web.Config:

<appSettings>
    <add key="key" value="value" />
</appSettings>


C#:

ConfigurationManager.AppSettings["key"]


My Two Cents ...

Binding DatagridView with HashTable

If you have your data into an HashTable, and you wanna bind a DataGrdiView, look at the following code as example.

private void BindYourDataGridView()
        {
            // here put data into the HashTable, but you can already have data
            // in you hashtable, in this case delete this lines -----------
            Hashtable YourData= new Hashtable();

            for (int i = 0; i < something; i++)
                YouData.Add(i, something[i]);
            //---------------------------------------------------------------------
            
            DataSet ds = new DataSet();
            DataTable dt = ds.Tables.Add("data");

            dt.Columns.Add("A", typeof(string));
            dt.Columns.Add("B"typeof(int));

            IDictionaryEnumerator enumerator = YourData.GetEnumerator();

            DataRow row = null;
            
            //adding row in the datatable
            while (enumerator.MoveNext())
            {
                int index = (int)enumerator.Key;
                string value = (string)enumerator.Value;
                row = dt.NewRow();
                row["A"] = index;
                row["B"] = value;
                dt.Rows.Add(row);
            }

      Your_DataGridView.DataSource = ds.Tables[0];
    }


My Two Cents ...