Tuesday, 14 May 2013

Copy data copy/paste to DataGridView use C#

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Modifiers == Keys.Control)
                {
                    switch (e.KeyCode)
                    {
                        case Keys.C:
                            CopyToClipboard();
                            break;

                        case Keys.V:
                            PasteClipboard();
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Copy/paste operation failed. " + ex.Message, "Copy/Paste", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

private void CopyToClipboard()
        {
            //Copy to clipboard
            DataObject dataObj = dataGridView1.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
        }

private void PasteClipboard()
        {
            try
            {
                string s = Clipboard.GetText();
                string[] lines = s.Split('\n');

                int iRow = dataGridView1.CurrentCell.RowIndex;
                int iCol = dataGridView1.CurrentCell.ColumnIndex;
                DataGridViewCell oCell;
                if (iRow + lines.Length > dataGridView1.Rows.Count - 1)
                {
                    bool bFlag = false;
                    foreach (string sEmpty in lines)
                    {
                        if (sEmpty == "")
                        {
                            bFlag = true;
                        }
                    }

                    int iNewRows = iRow + lines.Length - dataGridView1.Rows.Count;
                    if (iNewRows > 0)
                    {
                        if (bFlag)
                            dataGridView1.Rows.Add(iNewRows);
                        else
                            dataGridView1.Rows.Add(iNewRows + 1);
                    }
                    else
                        dataGridView1.Rows.Add(iNewRows + 1);
                }
                foreach (string line in lines)
                {
                    if (iRow < dataGridView1.RowCount && line.Length > 0)
                    {
                        string[] sCells = line.Split('\t');
                        for (int i = 0; i < sCells.GetLength(0); ++i)
                        {
                            if (iCol + i < this.dataGridView1.ColumnCount)
                            {
                                oCell = dataGridView1[iCol + i, iRow];
                                oCell.Value = Convert.ChangeType(sCells[i].Replace("\r", ""), oCell.ValueType);
                            }
                            else
                            {
                                break;
                            }
                        }
                        iRow++;
                    }
                    else
                    {
                        break;
                    }
                }
                Clipboard.Clear();
            }
            catch (FormatException)
            {
                MessageBox.Show("The data you pasted is in the wrong format for the cell");
                return;
            }
        }

Monday, 29 April 2013

Get list of network computer Names and IP Address using C#

using System.Net.NetworkInformation;
using System.Net;
NetworkBrowser nb1 = new NetworkBrowser();
string[] PCIP = new string[250];
string[] PCIP1 = new string[250];
int i = 0;
foreach (string pc in nb1.getNetworkComputers())
{
    PCIP[i] = pc;
   IPAddress[] addresslist = Dns.GetHostAddresses(pc);
    foreach (IPAddress address in addresslist)
   {
        PCIP1[i] = address.ToString();                       
    }
    i++;
}

Get list of network computer names using C#

//get Network Computers Name
// Use using System.Net.NetworkInformation;


using System.Net.NetworkInformation;
NetworkBrowser nb1 = new NetworkBrowser();
string[] PCIP = new string[250];
int i = 0;
foreach (string pc in nb1.getNetworkComputers())
{
    PCIP[i] = pc;
    i++;
}

Thursday, 14 March 2013

Calling code behind function from a html button

HTML button click call code behind in asp.net



<input type="button" id="btnuplod" runat="server" value="Upload" onclick="__doPostBack('btnSubmit','Submit');">

Code Behind
 
protected void Page_Load(object sender, EventArgs e)
{
   if (IsPostBack)
   {
      if (Convert.ToString(Request.Form["__EVENTARGUMENT"]) == "Submit")
      {
          btnuplod_Click();//submit action here
      }
   }
}

Your Function


Public void btnuplod_Click()
{
      if (FileUploadPDF.HasFile)
        {
            string fileName = Server.HtmlEncode(FileUpload1.FileName);
            string extension = System.IO.Path.GetExtension(fileName);
            if (extension == ".pdf")
            {

            }
            else
            {
                lblerror.InnerText = "Please upload PDF Files Only";
            }
        }
}
 

Thursday, 7 March 2013

Disable Browser Back Button Functionality using JavaScript in ASP.Net

Solutions 1:  
 
Suppose there are two pages Main1.aspx and Main2.aspx and Main1.aspx redirects
 to Main2.aspx

Hence to prevent user from visiting Main1.aspx using Back Button you will need
to place the above script in the head section of Main1.aspx as shown below. 

<script type = "text/javascript" >
         function preventBack() { window.history.forward(); }
         setTimeout("preventBack()", 10);
         window.onunload = function() { null };
 

    </script>


I have tested the script in the following browsers:

1.     Internet Explorer 5.55
2.     Internet Explorer 6
3.     Internet Explorer 7
4.     Mozilla Firefox 19.0
5.     Opera 9
6.     Safari 4
7.     Google Chrome

Friday, 1 March 2013

Dynamically add HTML (div / table / all HTML ) element using ASP.Net and C#

Solutions 1:  

HtmlGenericControl mydiv=new System.Web.UI.HtmlControls.HtmlGenericControl("div");
mydiv.Style.Add(HtmlTextWriterStyle.MarginLeft,"360px"); 

How do I get current index in “for each” loop

Solutions 1:  
foreach (DataRow row in dtGroups.Rows)
{
    string grpTitle = row["MainCategory"].ToString();
    int  id = dtGroups.Rows.IndexOf(row);   // (row no)
}