Monday, May 28, 2012

Display Data into DataGridView using DataTable in C#

By the following code we can display data into Data Grid View using datatable.

private void btnShow_Click(object sender, EventArgs e)
{
        this.Cursor = Cursors.WaitCursor;
        if (myConnection.State.Equals(ConnectionState.Closed))
                myConnection.Open( ); // Here
myConnection is a SQL Connection object

    string selectQry = "SELECT P.PECODE, P.PENAME, P.PDEPNM, P.PSECNM, ";
    selectQry += "D.DDSGDS, P.SHFTNM ";
    selectQry += "FROM PIDT00V P INNER JOIN DDSG00V D ON P.PDESIGCD = D.DDSGCD ";
    selectQry += " WHERE (P.PCOMCD = '" + ComCode + "') ";
    selectQry += " ORDER BY P.PECODE";
    
    SqlCommand dbcommand = new SqlCommand(selectQry, myConnection);

    dataGridView1.DataSource = null;
    dataGridView1.Columns.Clear();
    DataTable datatable = new DataTable();

            DataColumn dcol0 = new DataColumn("PECODE", typeof(System.String));
            DataColumn dcol1 = new DataColumn("PENAME", typeof(System.String));
            DataColumn dcol2 = new DataColumn("PDEPNM", typeof(System.String));
            DataColumn dcol3 = new DataColumn("PSECNM", typeof(System.String));
            DataColumn dcol4 = new DataColumn("DDSGDS", typeof(System.String));
            DataColumn dcol5 = new DataColumn("SHFTNM", typeof(System.String));           

            datatable.Columns.Add(dcol0);
            datatable.Columns.Add(dcol1);
            datatable.Columns.Add(dcol2);
            datatable.Columns.Add(dcol3);
            datatable.Columns.Add(dcol4);
            datatable.Columns.Add(dcol5);           

            DataRow drow;
            dbreader = dbcommand.ExecuteReader();

            while (dbreader.Read())
            {
                drow = datatable.NewRow();

                drow["PECODE"] = dbreader[0];
                drow["PENAME"] = dbreader[1];
                drow["PDEPNM"] = dbreader[2];
                drow["PSECNM"] = dbreader[3];
                drow["DDSGDS"] = dbreader[4];
                    drow["SHFTNM"] = dbreader[5];
               
               datatable.Rows.Add(drow);
            }
            dbreader.Close( );

            dataGridView1.DataSource = datatable;

            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.ReadOnly = true;
            dataGridView1.RowHeadersVisible = false;

            this.Cursor = Cursors.Default;
}

Create Report Using DataSet in C#

In the following example we will see how we can send data to report file using dataset.

SqlCommand dbcommand = new SqlCommand("sp_OperatorProcessShow", myConnection);
dbcommand.CommandType = CommandType.StoredProcedure;
dbcommand.Parameters.Add(new SqlParameter("@CompCode", CompanyCode));

// I have used Stored Procedure and supplied parameter

DataSetOP myDS = new DataSetOP(); // DataSetOP is the DataSet Name
DataTable dtMyTable = myDS.Tables["tblOperatorProcs"]; // tblOperatorProcs is the DataTable
dtMyTable.Clear();
DataRow myNewRow;
int count = 0;

SqlDataReader dbreader = dbcommand.ExecuteReader();

while (dbreader.Read())
{
      myNewRow = dtMyTable.NewRow();

      myNewRow[0] = dbreader[0];
      myNewRow[1] = dbreader[1];
      .....
      .....
      myNewRow[8] = dbreader[8]; // Add as many record as you need

      dtMyTable.Rows.Add(myNewRow);
      dtMyTable.AcceptChanges();
      count++;
}
dbreader.Close();

if (count > 0)
{
                CROprProcs myDataReport = new CROprProcs(); //
CROprProcs is a Crystal Report file
                myDataReport.SetDataSource(myDS.Tables["tblOperatorProcs"]);
                myDataReport.SetParameterValue("CompName", compName);
                crystalReportViewer1.ReportSource = myDataReport;
}
else
{
                MessageBox.Show("No Data Found");
}



How to Read Remote PC file and Show in your PC window

Sometimes you may need to read a file which exists in Server PC and want to show in your Client PC. To make it simple Lets say i want to read an Excel file which exists in a remote machine and want to show in my client machine.

By following code we can do that easily.

FileInfo finfo = new FileInfo(@"\\192.168.100.20\D$\Leave information.xls");
bool exists = finfo.Exists;
 

if (finfo.Exists)
{
          System.Diagnostics.Process.Start(
finfo.FullName);
}

else
{
         MessageBox.Show("File Not Found");
}

// Here my remote PC address is 192.168.100.20 and The Excel file located in D drive 

You may place the above code into any button click event