Tuesday, July 26, 2011

C# Code snippet, paste to DataGridView !

Often times during your journey as a C# programmer it will be necessary to copy and paste data from Excel spreadsheets.  Here's a code snippet to paste data into a DataGridView.

        private void PasteClipboard(DataGridView dgv)
        {
            try
            {
                DataGridViewRow selectedRow;
                /* Find first selected cell's row (or first selected row). */
                if (dgv.SelectedRows.Count > 0)
                    selectedRow = dgv.SelectedRows[0];
                else if (dgv.SelectedCells.Count > 0)
                    selectedRow = dgv.SelectedCells[0].OwningRow;
                else
                    return;
                /* Get clipboard Text */
                string clipText = Clipboard.GetText();
                /* Get Rows ( newline delimited ) */
                string[] rowLines = Regex.Split(clipText, "\r\n");
                foreach (string row in rowLines)
                {
                    /* Get Cell contents ( tab delimited ) */
                    string[] cells = Regex.Split(row, "\t");
                    DataGridViewRow r = new DataGridViewRow();
                    foreach (string sc in cells)
                    {
                        DataGridViewTextBoxCell c = new DataGridViewTextBoxCell();
                        c.Value = sc;
                        r.Cells.Add(c);
                    }
                    dgv.Rows.Insert(selectedRow.Index, r);
                    this.toolStripStatusLabel1.Text = "Data has been pasted.";
                }


            }
            catch (System.ArgumentException ex)
            {
                MessageBeep(MB_ICONEXCLAMATION);
                this.toolStripStatusLabel1.Text = ex.Message;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Friday, July 15, 2011

C++ Pointers and References

#include <iostream>
using namespace std;

int main()
{
int someInt = 0; //<-- memory allocated and initialized to 0.
cout << &someInt << endl;//<-- print the address of someInt
//^^ It really prints the address of the base of the allocated section of memory.
int *dynamicInt = new int(0);//<-- memory allocated (on the heap) and initalized to 0.
cout << dynamicInt << endl;//<-- print the address of dynamicInt

//Assign both sections of memory the value of 1.
someInt = 1;
*dynamicInt = 1;//<-- here the pointer is "de-referenced"

cout << "someInt = " << someInt << endl;
cout << "dynamicInt = " << *dynamicInt << endl;


//Create a reference to both.
int &someRef = someInt;//<-- disregard the syntax with regard to the address
int &otherRef = *dynamicInt;//


//Modify both with the reference.
someRef = 2;
otherRef = 2;

cout <<  "someRef = " << someRef << endl;
cout << "otherRef = " << otherRef << endl;




delete dynamicInt;//<-- free dynamically allocated memory.

cout << "Press ENTER to continue." << endl;
cin.get();
return 0;
}