Friday, October 29, 2010

Add a DataGridViewColumn in DataGridView , C#

DataGridViewColumn dgvc = new DataGridViewColumn();


dgvc.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;

dgvc.HeaderText = st;

dgvc.Name = st;

DataGridViewCell newcell = new DataGridViewTextBoxCell();

dgvc.CellTemplate = newcell;

dgvc.Visible = true;

datagridview1.Columns.Add(dgvc);

datagridview1.Rows[0].Cells[st].Value ="500";

Split a String Seperated With \ , C#

string sSuppName = "A/B/C/D/E";
string[] str1 = sSuppName.Split('/');
  
output     A
              B
              C
              D

string sSuppName = "A\B\C\D\E";
string[] str1 = sSuppName.Split('\\');

output    A
             B
             C
             D

Thursday, October 28, 2010

Select Date From DateTime In SQL

select convert(varchar,DOB,101)   from emp


101 means “mm/dd/yyyy” format

108 will return just the time “hh:mm:ss”

SELECT CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, DOB)))  from emp

SELECT CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, DOB)))  from emp

where CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, DOB)))>'10-25-2010'

Friday, October 22, 2010

Sort String in SQL

ID      Salary    Name


1       0            AB123

2       0            AB65

3       0            AB10

4       0            AB25



Sort it on the column Name and we want Output like

this

ID   Salary    Name

1     0           AB123

2    0            AB65

4    0           AB25

3    0          AB10

select CAST(SUBSTRing(Name, 3,4) AS int) as BB,*  from A order by BB desc

Wednesday, October 20, 2010

Check Printer Status in C#

Assembly : System.Management;

     public bool CheckPrinterStatus()
        {

            ManagementScope scope = new ManagementScope(@"\root\cimv2");
            scope.Connect();

            // Select Printers from WMI Object Collections

            ManagementObjectSearcher PrinterCollection= new
            ManagementObjectSearcher("SELECT * FROM Win32_Printer");

            string printerName = "";

            foreach (ManagementObject printer in PrinterCollection.Get())
            {
                printerName = printer["Name"].ToString().ToLower();
                if (printerName.Equals(@"hp laserjet 1018"))
                {
                    MessageBox.Show("Printer = " + printer["Name"]);
                    if (printer["WorkOffline"].ToString()=="True")
                    {
                        // Printer is offline

                        MessageBox.Show("Printer is not Connected.");
                        return false;
                    }
                    else
                    {
                        // Printer is Online
                       MessageBox.Show("Printer is Connected.");
                        return true;
                    }
                }
            }
        }

Monday, October 18, 2010

Copy all Data From one Table to another with same Structure in sql

copy all Data From one Table to another  with same Structure

insert into [MyDB].[dbo].[Emp] select * from [Info].[dbo].[A]

it will copy all data in EMP table from A Table

Saturday, October 9, 2010

Export DataGridView Data in Excel C#

object misValue = System.Reflection.Missing.Value;


Excel.Application xlapp;

Excel.Workbook xlworkbook;

Excel.Worksheet xlsheets;

xlapp = new Excel.ApplicationClass();

xlworkbook = xlapp.Workbooks.Add("");

xlsheets = (Excel.Worksheet)xlworkbook.Sheets.get_Item(1);

int i = 0; int j = 0;

int x = 2;

for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)

{

for (j = 0; j <= dataGridView1.Columns.Count-1; j++)

{

DataGridViewCell dgvcell = dataGridView1[j, i];

if (i == 0)

{

string ss = "B"+(j+1);

xlsheets.Cells[i + 1, j + 1] = dataGridView1.Columns[dgvcell.ColumnIndex].Name;

//xlsheets.get_Range("A1", "C1").Interior.ColorIndex = 37;

xlsheets.get_Range("A1", "C1").Interior.Color= System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue);

xlsheets.get_Range("A1", "C1").Interior.Pattern = XlPattern.xlPatternSolid;

xlsheets.get_Range("A1", "C1").Font.Bold = true;

xlsheets.get_Range("A1", "C1").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;

//xlsheets.get_Range("A1", "C1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;



}



xlsheets.Cells[i+2, j+1] = dgvcell.Value;





}

string rng1 = "A" + (i + 2);

string rng2 = "C" + (i + 2);



xlsheets.get_Range(rng1, rng2).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Pink);

xlsheets.get_Range(rng1, rng2).Interior.Pattern = XlPattern.xlPatternSolid;



}

xlworkbook.SaveAs("d:\\MM.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
 
 
 
private void releaseObject(object obj)


{

try

{

System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);

obj = null;

}

catch (Exception ex)

{

obj = null;

MessageBox.Show("Exception Occured while releasing object " + ex.ToString());

}

finally

{

GC.Collect();

}

}
 

Monday, October 4, 2010

Set Text of Button in DataGridView

 DataGridViewButtonColumn colbtnApproved = new DataGridViewButtonColumn();

colbtnApproved .HeaderText = "Approve";


colbtnApproved .Text = "Approve";

colbtnApproved .UseColumnTextForButtonValue = true;
 
now text is visible on button

Followers

Visitor No

click for free hit counter
Download a free hit counter here.